0

我正在创建一个 Java 应用程序,我需要它来读取特定的单元格,文档上的单元格将包含字符串和一些整数。你能帮忙吗,我在下面找到了这个示例代码,但现在我被卡住了。我需要将电子表格上的数据保存到数据库中。谢谢你

InputStream inp = new FileInputStream("workbook.xls");
    //InputStream inp = new FileInputStream("workbook.xlsx");

    Workbook wb = WorkbookFactory.create(inp);
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(2);
    Cell cell = row.getCell(3);
4

1 回答 1

3

我将您的代码更改为工作示例:

    InputStream inp = new FileInputStream("workbook.xls");
    HSSFWorkbook wb = new HSSFWorkbook(inp);
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(2);
    Cell cell = row.getCell(3);
    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        System.out.println("string: " + cell.getStringCellValue());
    }
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        System.out.println("numeric: " + cell.getNumericCellValue());
    }
    System.out.println("any: " + cell.toString());

我希望它有帮助!

于 2012-09-04T13:17:01.810 回答