在文件之间克隆表

时间:2011-07-16 12:40:18

标签: java excel apache-poi xssf

我有三个或更多excel文件,其中包含不同的工作表,我需要创建一个新的空白文件,其中包含一个复制(或克隆),并将其放入新文件并按照我需要的顺序放置,以便我可以填写各种形式的数据。

如何使用Jakarta POI(XSSFWorkbook)?

来完成此操作

3 个答案:

答案 0 :(得分:3)

首先,我认为你的意思是Apache POI - 它已经有几年没有Apache Jakarta POI ......

在将工作表从一个工作簿复制到另一个工作簿方面,可以完成,但需要进行一些编码。首先,您需要确定您使用的单元格样式,并克隆它们。确保您跟踪Cell Style到哪个目标Cell Style的源,因为您不想继续重新创建或者您将达到限制! CellStyle.cloneStyleFrom(CellStyle)是你想要的方法。

然后,对于每个源表,在目标工作簿中创建工作表。遍历所有源行,创建新的目标行。然后循环遍历单元格,按单元格类型切换,获取适当的值并进行设置。冲洗并重复!

答案 1 :(得分:0)

FileOutputStream os = new FileOutputStream(" differnetFileName")         readWorkbook.write(OS);

猜测我们可以使用带有diff文件名的操作系统的写操作。

答案 2 :(得分:0)

这是我将工作表从一个工作簿复制到另一个工作簿的实现。我按照Gagravarr的描述做了一切。这个解决方案适合我。如果工作表没有表格等,此代码将起作用。如果工作表包含简单文本(String,boolean,int等),公式,此解决方案将起作用。

Workbook oldWB = new XSSFWorkbook(new FileInputStream("C:\\input.xlsx"));
Workbook newWB = new XSSFWorkbook();
CellStyle newStyle = newWB.createCellStyle(); // Need this to copy over styles from old sheet to new sheet. Next step will be processed below
Row row;
Cell cell;
for (int i = 0; i < oldWB.getNumberOfSheets(); i++) {
    XSSFSheet sheetFromOldWB = (XSSFSheet) oldWB.getSheetAt(i);
    XSSFSheet sheetForNewWB = (XSSFSheet) newWB.createSheet(sheetFromOldWB.getSheetName());
    for (int rowIndex = 0; rowIndex < sheetFromOldWB.getPhysicalNumberOfRows(); rowIndex++) {
        row = sheetForNewWB.createRow(rowIndex); //create row in this new sheet
        for (int colIndex = 0; colIndex < sheetFromOldWB.getRow(rowIndex).getPhysicalNumberOfCells(); colIndex++) {
            cell = row.createCell(colIndex); //create cell in this row of this new sheet
            Cell c = sheetFromOldWB.getRow(rowIndex).getCell(colIndex, Row.CREATE_NULL_AS_BLANK ); //get cell from old/original WB's sheet and when cell is null, return it as blank cells. And Blank cell will be returned as Blank cells. That will not change.
                if (c.getCellType() == Cell.CELL_TYPE_BLANK){
                    System.out.println("This is BLANK " +  ((XSSFCell) c).getReference());
                }
                else {  //Below is where all the copying is happening. First It copies the styles of each cell and then it copies the content.              
                CellStyle origStyle = c.getCellStyle();
                newStyle.cloneStyleFrom(origStyle);
                cell.setCellStyle(newStyle);            

                 switch (c.getCellTypeEnum()) {
                    case STRING:                            
                        cell.setCellValue(c.getRichStringCellValue().getString());
                        break;
                    case NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {                             
                            cell.setCellValue(c.getDateCellValue());
                        } else {                              
                            cell.setCellValue(c.getNumericCellValue());
                        }
                        break;
                    case BOOLEAN:

                        cell.setCellValue(c.getBooleanCellValue());
                        break;
                    case FORMULA:

                        cell.setCellValue(c.getCellFormula());
                        break;
                    case BLANK:
                        cell.setCellValue("who");
                        break;
                    default:
                        System.out.println();
                    }
                }
            }
        }

    }
    //Write over to the new file
    FileOutputStream fileOut = new FileOutputStream("C:\\output.xlsx");
    newWB.write(fileOut);
    oldWB.close();
    newWB.close();
    fileOut.close();

如果您的要求是在不离开或添加任何内容的情况下复制整张纸。我认为消除过程比上面的代码更好,更快。而且您不必担心丢失公式,图纸,表格,样式,字体等。

XSSFWorkbook wb = new XSSFWorkbook("C:\\abc.xlsx");
for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) {
        if (!wb.getSheetName(i).contentEquals("January")) //This is a place holder. You will insert your logic here to get the sheets that you want.  
            wb.removeSheetAt(i); //Just remove the sheets that don't match your criteria in the if statement above               
}
FileOutputStream out = new FileOutputStream(new File("C:\\xyz.xlsx"));
wb.write(out);
out.close();