Java Check工作簿包含特定的电子表格或不使用Apache POI

时间:2017-04-26 22:33:13

标签: java excel apache-poi

我正在使用apache POI创建一个包含多个电子表格的excel文件。首先,此方法检查其存在后应检查工作簿是否为空。如果为空,则创建新工作表名称“sheet1”;如果没有,请检查此工作簿中名为“sheet1”的任何工作表。如果是,则向工作表添加新行;如果没有,请创建工作表名称“sheet1”。以下是我目前的代码,我认为这不是实现目标的正确方法:

    File file = new File("Main Data.xlsx");
    // Retrieve the workbook for the main report
    XSSFWorkbook workbook;
    // Check file existence 
    if (file.exists() == false) {
        // Create new file if it does not exist
        workbook = new XSSFWorkbook();
    } else {
        try ( 
            // Make current input to exist file
            InputStream is = new FileInputStream(file)) {
                workbook = new XSSFWorkbook(is);
            }
    }
    XSSFSheet spreadsheet;
    // Check if the workbook is empty or not
    if (workbook.getNumberOfSheets() != 0) {
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
           if (workbook.getSheetName(i).equals(" Enc " + deviceLabel.getText() + " Info. ")) {
                spreadsheet = workbook.getSheet(" Enc " + deviceLabel.getText() + " Info. ");
            } else spreadsheet = workbook.createSheet(" Enc " + deviceLabel.getText() + " Info. ");
        }
    }
    else {
        // Create new sheet to the workbook if empty
        spreadsheet = workbook.createSheet(" Enc " + deviceLabel.getText() + " Info. ");
    }

    // Error here (variable spreadsheet may not have been initialized
    spreadsheet.shiftRows(0, spreadsheet.getLastRowNum(), 23);   

我遇到的问题。

  1. for循环显然不符合我的目标。有没有办法在一个if语句中同时检查空电子表格或命名电子表格?
  2. e.g。

      if ((workbook.getNumberOfSheets() == 0) && !workbook.getSheet("sheet1").exists()) {
        workbook.createSheet("sheet1");
      } else workbook.getSheet("sheet1")
    
    1. 我不断收到电子表格未初始化的错误,但如果删除for循环,错误就会消失。

4 个答案:

答案 0 :(得分:1)

我用以下代码解决了我的问题:

    File file = new File("Main Data.xlsx");
    // Retrieve the workbook for the main report
    XSSFWorkbook workbook;
    // Check file existence 
    if (file.exists() == false) {
        // Create new file if it does not exist
        workbook = new XSSFWorkbook();
    } else {
        try ( 
            // Make current input to exist file
            InputStream is = new FileInputStream(file)) {
                workbook = new XSSFWorkbook(is);
            }
    }
    XSSFSheet spreadsheet = workbook.getSheet("sheet1");
    // Check if the workbook is empty or not
    if (workbook.getNumberOfSheets() != 0) {
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
           if (workbook.getSheetName(i).equals("sheet1")) {
                spreadsheet = workbook.getSheet("sheet1");
            } else spreadsheet = workbook.createSheet("sheet1");
        }
    }
    else {
        // Create new sheet to the workbook if empty
        spreadsheet = workbook.createSheet("sheet1");
    }

    // Shift rows down if exist
    spreadsheet.shiftRows(0, spreadsheet.getLastRowNum(), 23);   

答案 1 :(得分:1)

您可以使用

简单地检查是否创建了工作表
workbook.getSheetIndex(sheetName)

如果工作表不在工作表中,则返回-1;如果工作表中存在工作表,则返回Index。

答案 2 :(得分:0)

您已将工作簿声明为final,它不能是工作的首字母!

答案 3 :(得分:0)

您的代码先验是错误的。您可能会为自己的答案加分?

public XSSFSheet factorySheet(final String name) {
         return Optional.ofNullable((XSSFSheet) workbook.getSheet(name))
                .orElseGet(()->(XSSFSheet) workbook.createSheet(name));
    }