gpt4 book ai didi

Java Jxl 读/写工作簿

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:44 25 4
gpt4 key购买 nike

我有一个带有小数位数(即 30.50)的 Assets 电子表格(excel),但如果单元格格式设置为“无小数位”(30),则只能运行下面的代码,否则会出现以下错误。该错误将 float 显示为带角的 30,50,而不是原始的 30.50!!!

需要将字符串rCell转换为float。

rCell = readCell.getContents();
float testnumber = Float.parseFloat(rCell);

////////***** 读取 Assets EXCEL 文件 ****////////////

AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("MyFile.xls");
} catch (IOException e) {
e.printStackTrace();
}
try {
readAsset = Workbook.getWorkbook(inputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
Sheet s = readAsset.getSheet(0);

////////////////////////////////******** 将 Excel 写入 SD ***//////////////

    Fnamexls="testfile"  + ".xls";
File sdCard = Environment.getExternalStorageDirectory();
directory = new File (sdCard.getAbsolutePath() + "/MyFolder");
directory.mkdirs();
sdFile = new File(directory, Fnamexls);

wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
try {
wrWorkbook = Workbook.createWorkbook(sdFile, wbSettings);
writeSheet = wrWorkbook.createSheet("First Sheet", 0);
int row = s.getRows();
int col = s.getColumns();
//xx = "";
for (int c = 0; c < col; c++) {
for (int r = 0; r < row; r++) {
Cell z = s.getCell(c, r);
xx = z.getContents();
Label label4 = new Label(c, r, String.valueOf(xx));
try {
writeSheet.addCell(label4);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
}
wrWorkbook.write();
try {
wrWorkbook.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
//createExcel(excelSheet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

readTest();

public void readTest(){
Workbook m_workBook = null;
try {
m_workBook = Workbook.getWorkbook(sdFile);
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
readSheet = m_workBook.getSheet(0);
int rRow = readSheet.getRows();
int rCol = readSheet.getColumns();
Cell readCell = readSheet.getCell(2, 0);
rCell = readCell.getContents();

float testnumber = Float.parseFloat(rCell); //Line error

}

控制台错误“由以下原因引起:java.lang.NumberFormatException:对于输入字符串:“30,50” 在 java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)”

最佳答案

找到了。我在 Main() 中设置了区域设置Locale.setDefault(new Locale("en", "US"));

关于Java Jxl 读/写工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com