Java Jxl读/写到工作簿

时间:2019-05-18 14:53:19

标签: java excel jxl

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

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

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

//////// ******读取资产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)”

1 个答案:

答案 0 :(得分:0)

找到了。我在Main()中设置语言环境 Locale.setDefault(new Locale(“ en”,“ US”));