2011-05-04 205 views
4

我正在使用apache poi創建一個excel文檔。要在工作簿中創建新工作表,我寫下一個代碼:Apache POI。複製工作表

Workbook wb = new HSSFWorkbook(); 
Sheet sh = wb.createSheet(); 

此代碼創建並將工作表添加到工作簿。但我想先創建工作表,然後將其添加到工作簿。水木清華這樣的:

Sheet sh = new HSSFSheet(); 
wb.addSheet(sh); 

我需要這樣的東西,因爲我想將數據從一個工作簿中的一個表複製到另一個工作簿中的另一個工作表(工作簿接口有方法Sheet cloneSheet(int))。但是工作簿界面沒有像addSheet(Sheet sh)這樣的方法。 另外HSSFWorkbook是final類,所以我不能擴展它來實現添加方法 我該怎麼做?

回答

8

您不能只從一個工作簿獲取Sheet對象,並將其添加到不同的工作簿。

您需要做的是同時打開舊工作簿和新工作簿,並在新工作簿中創建工作表。接下來,將您在舊工作表中使用的所有樣式克隆到新工作表中(HSSFCellStyle具有將樣式從一個工作簿克隆到另一個工作簿的方法)。最後,遍歷所有的單元格並複製它們。

1

好吧我試着去做Gagravarr上面說的。這個解決方案適用於我。如果工作表沒有表格等,如果工作表包含簡單的文本(字符串,布爾值,整型等),公式,該解決方案將起作用。

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(); 
相關問題