java - 无法使用 apache POI 在工作簿中创建新的 Excel 工作表

标签 java excel apache-poi

我正在尝试将多个文件复制到一个 Excel 文件中。该 Excel 文件中的每个工作表都将包含一个文件的内容。我需要复制大约 6 个文件。因此生成的文件应包含 6 张。但是当我运行代码时,单个文件只生成一张表。我尝试调试它但无法找出原因。

这是我的代码。

 public static void main(String[] args) throws IOException {

    // TODO Auto-generated method stub
    CreateSingleExcelFile cef = new CreateSingleExcelFile();
    cef.fileIterator();
 }

 public void fileIterator() throws IOException{

    File dir = new File("path for files to copy");
    File[] dir_listing = dir.listFiles();
    HSSFWorkbook my_wb = new HSSFWorkbook();                        
    //creating an output stream to copy all files in combined.xls
    BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("path of resultant excel sheet"));
    //traversing through a list of files
    for(File file: dir_listing){
        //file: file to be copied.
        add_in_excel(my_wb,bos,file);
        System.out.println("In file :" + file.getName());
    }
    bos.close();
    System.out.println("Files are copied");
}


 private void add_in_excel(HSSFWorkbook copy_wb, BufferedOutputStream bos,File file) throws IOException {
    // TODO Auto-generated method stub
    //creating a new sheet in the copy workbook
    HSSFSheet mySheet =  copy_wb.createSheet(file.getName());

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    HSSFWorkbook workbook = new HSSFWorkbook(bis);
    CellStyle cs = copy_wb.createCellStyle();
    cs.setWrapText(true);   

    HSSFSheet sheet = null;
    HSSFRow row = null;
    HSSFCell cell = null;               
    HSSFRow myRow = null;
    HSSFCell myCell = null;
    int sheets = workbook.getNumberOfSheets();          
    int fRow = 3;
    int lRow = 0;
    int count_row=0;

    //traversing through sheets in the 'file'
    for (int iSheet = 0; iSheet < sheets; iSheet++) {                   
        sheet = workbook.getSheetAt(iSheet);                
        if (sheet != null) {                                      
            lRow = sheet.getLastRowNum();
            for (int iRow = fRow; iRow <= lRow; iRow++) {
                row = sheet.getRow(iRow);
                //creating row in the new sheet
                myRow = mySheet.createRow(count_row++);
                if (row != null) {                           
                    for (int iCell = 0; iCell < 4; iCell++) {  
                        //creating a column in the new sheet
                        cell = row.getCell(iCell);
                        myCell = myRow.createCell(iCell);                                
                        myCell.setCellStyle(cs);
                        //setting cell type and adding data in each cell
                        if (cell != null ) {                                
                            myCell.setCellType(cell.getCellType());
                            switch (cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_BLANK:                                      
                               myCell.setCellValue("");
                                break;

                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                myCell.setCellValue(cell.getBooleanCellValue());
                                break;

                            case HSSFCell.CELL_TYPE_ERROR:
                                myCell.setCellErrorValue(cell.getErrorCellValue());
                                break;

                            case HSSFCell.CELL_TYPE_FORMULA:
                                myCell.setCellFormula(cell.getCellFormula());
                                break;

                            case HSSFCell.CELL_TYPE_NUMERIC:
                                if(HSSFDateUtil.isCellDateFormatted(cell))
                                    myCell.setCellValue(cell.getDateCellValue());
                                else
                                    myCell.setCellValue(cell.getNumericCellValue());
                                break;

                            case HSSFCell.CELL_TYPE_STRING:
                                myCell.setCellValue(cell.getStringCellValue());
                                break;
                            default:
                                myCell.setCellFormula(cell.getCellFormula());
                            }                                   
                        }
                    }
                }
            }
        }
    }
    bis.close();           
    copy_wb.write(bos);        
}

最佳答案

由于您没有提到任何异常或堆栈跟踪,因此我将进行疯狂猜测 - 对于目录中的每个新 excel 文件,您正在执行

HSSFWorkbook 工作簿 = new HSSFWorkbook(bis);

在阅读每张工作表(所有行和单元格)结束时,您将继续阅读下一张工作表,依此类推,直到所有工作表都已创建并存储在内存中。然后,您通过

将工作簿本身写到输出流

copy_wb.write(bos);

[我知道这些你都知道,但万一将来有人来,这将使他们更容易了解正在发生的事情,而无需花费时间]

我在想,当你第一次说workbook.write(outputstream)时,内容就被写入了。但你还没有关闭流,并且你已经写完了一整本工作簿。下次您想要将另一个工作簿写入同一流时,我真的不知道会发生什么。不应该是向当前工作簿添加工作表(而不是将多个工作簿写入同一输出流)吗?

我建议创建目标工作簿(如果不存在)并编写源工作簿的工作表(而不是工作簿本身)。这可能是一种解决方法,但除非我可以将多个工作簿调试到同一输出流,否则我无法真正建议解决当前问题。

关于java - 无法使用 apache POI 在工作簿中创建新的 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647746/

相关文章:

java - 如何绘制文本上标-1(unicode)?

java - Android 应用程序无法在 Eclipse 中的设备上启动

excel - 使用开放文档和网络服务

python - 在Python xlrd中读取Excel文件

Excel:如果一个单元格包含两个单词,我将如何使用第一个单词的第一个字母和第二个单词的前两个字母?

eclipse - 使用 Java 11 在 Eclipse 中混合模块化和非模块化开发

java - Web 应用程序密码安全

java - 谁能告诉我为什么这在 Eclipse 中有效但在命令提示符中不起作用?

java - 读取空白单元格 Apache POI 3.17

java - 获取 POI 中特定行中填充的单元格数量的计数