简体   繁体   中英

Reading numbers from excel file - Java - Swing

I created a tool that I can upload and get some numbers from excel file

public static void main( String[] args ) throws Exception {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        App.file = fc.getSelectedFile();
        //This is where a real application would open the file.
        System.out.println("Opening: " + App.file.getName() + ".");
    } else {
        System.out.println("Open command cancelled by user.");
    }       


    workbook = Workbook.getWorkbook(App.file);
      Sheet sheet = workbook.getSheet(1);

      Cell cell1 = sheet.getCell(4, 27);
      System.out.println(cell1.getContents());
      Cell cell2 = sheet.getCell(4, 28);
          System.out.println(cell11.getContents());




      App.eStop = false;
     int i = 0 ;
    while (App.eStop == false) {
         try {


          cell3 = sheet.getCell(9, i);
          System.out.println(cell3.getContents());  
         } catch (Exception e) {
             App.eStop = true; 
         };
        i++;


    }


    System.out.println("done");

and i pasting this values to some other tool using the below command

psess.GetPS().SendKeys(cell1.getContents(), 8, 18);

All the values from the excel file are numbers. Sometimes i get decimal numbers (like 2.02).

Because i can't change the excel file, its possible to tell to my app to ignore the .02 and keep only the 2 for example?

If the Excel file has proper formats set for the cells that you are trying to read, you can try to apply the format and thus get the values in the same format as Excel displays them.

See eg the following example from the documentation at http://poi.apache.org/spreadsheet/eval.html

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

// suppose your formula is in B3
CellReference cellReference = new CellReference("B3"); 
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); 

CellValue cellValue = evaluator.evaluate(cell);

switch (cellValue.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        System.out.println(cellValue.getBooleanValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        System.out.println(cellValue.getNumberValue());
        break;
    case Cell.CELL_TYPE_STRING:
        System.out.println(cellValue.getStringValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        break;
    case Cell.CELL_TYPE_ERROR:
        break;

    // CELL_TYPE_FORMULA will never happen
    case Cell.CELL_TYPE_FORMULA: 
        break;
}

Have-you try a Math.round() ? Or a new BigDecimal(cell3.getContents()).setScale(1) ?(if you want to keep the first digit)

Thank you for the help.

Finally i solve my problem using the bellow:

 Cell cell4 = sheet.getCell(3, 7);
          String number3 = cell4.getContents().split("\\.")[0].replaceAll("\\,","");         
          System.out.println(number3);
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.math.BigDecimal;

Row currentRow = rowIterator.next();
currentRow.getCell(4).setCellType(XSSFCell.CELL_TYPE_NUMERIC);
Double doubleValue = currentRow.getCell(4).getNumericCellValue();
BigDecimal bd = new BigDecimal(doubleValue.toString());
long lonVal = bd.longValue();
String actualNumber = Long.toString(lonVal).trim();

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