简体   繁体   中英

Java Jxl read/writing to workbook

I have an asset spreadsheet(excel) with decimal mumbers (ie. 30.50), but can only run the code below if the cells are formatted to "no decimal places" (30) otherwise it gives me the error below. The error displays the float as 30,50 with comer instead the original 30.50 !!!

Need to convert string rCell to float.

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

////////****** Read asset EXCEL File ****///////////

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);

////////////////////////////////****** WRITE Excel TO 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   

}

Console Error “Caused by: java.lang.NumberFormatException: For input string: "30,50" at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)”

Found it. I set the Locale in Main() Locale.setDefault(new Locale("en", "US"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM