如何在java中使用hssf创建多个工作表

时间:2018-06-14 12:26:15

标签: java servlets apache-poi

当我尝试使用java代码在单个文件中创建多个样式表时

global $created_by

错误是 - 资源类型HSSFSheet未实现java.lang.AutoCloseable

2 个答案:

答案 0 :(得分:0)

您是如何创建输出文件的?

以下示例创建一个包含两个工作表(“Sheet0”和“登录列表”)的Excel文件:

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet();
HSSFSheet worksheet1 = workbook.createSheet("List of logins");

// set value at "List of logins:A1"
Row row = worksheet1.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Some text");

//   write workbook to file 
try (FileOutputStream outputStream = new FileOutputStream("output.xls")) {
    workbook.write(outputStream);
    workbook.close();
} catch (IOException ioe) {
    ioe.printStackTrace(System.err);
}

答案 1 :(得分:0)

       HSSFSheet customerSheet = workbook.createSheet("Customer List");
        HSSFSheet partnerSheet = workbook.createSheet("Partner List");
        HSSFSheet hdtSheet = workbook.createSheet("HDT List");
        HSSFSheet adminSheet = workbook.createSheet("Admin List");
        HSSFCellStyle boldStyle = getBoldStyle(workbook);
        HSSFCellStyle dateTimeStype = getDateTimeStyle(workbook);

        HSSFRow rowOne = customerSheet.createRow(0);
        HSSFRow rowTwo = partnerSheet.createRow(0);
        HSSFRow rowThree = hdtSheet.createRow(0);
        HSSFRow rowFour = adminSheet.createRow(0);
        HSSFCell cellOne = rowOne.createCell(1);

        cellOne.setCellValue("UserID");
        cellOne.setCellStyle(boldStyle);
        cellOne = rowOne.createCell(2);
        cellOne.setCellValue("User name");
        cellOne.setCellStyle(boldStyle);
        cellOne = rowOne.createCell(3);
        cellOne.setCellValue("Last Login");
        cellOne.setCellStyle(boldStyle);
        cellOne.setCellStyle(dateTimeStype);
        int rownumOne = 2;

        HSSFCell cellTwo = rowTwo.createCell(1);
        cellTwo.setCellValue("UserID");
        cellTwo.setCellStyle(boldStyle);
        cellTwo = rowTwo.createCell(2);
        cellTwo.setCellValue("User name");
        cellTwo.setCellStyle(boldStyle);
        cellTwo = rowTwo.createCell(3);
        cellTwo.setCellValue("Last Login");
        cellTwo.setCellStyle(boldStyle);
        cellTwo.setCellStyle(dateTimeStype);
        int rownumTwo = 2;

        HSSFCell cellThree = rowThree.createCell(1);
        cellThree.setCellValue("UserID");
        cellThree.setCellStyle(boldStyle);
        cellThree = rowThree.createCell(2);
        cellThree.setCellValue("User name");
        cellThree.setCellStyle(boldStyle);
        cellThree = rowThree.createCell(3);
        cellThree.setCellValue("Last Login");
        cellThree.setCellStyle(boldStyle);
        cellThree.setCellStyle(dateTimeStype);
        int rownumThree = 2;

        HSSFCell cellFour = rowFour.createCell(1);
        cellFour.setCellValue("UserID");
        cellFour.setCellStyle(boldStyle);
        cellFour = rowFour.createCell(2);
        cellFour.setCellValue("User name");
        cellFour.setCellStyle(boldStyle);
        cellFour = rowFour.createCell(3);
        cellFour.setCellValue("Last Login");
        cellFour.setCellStyle(boldStyle);
        cellFour.setCellStyle(dateTimeStype);
  

Blockquote

        int rownumFour =2;