2014-02-20 46 views
1

我有follwing表中的Excel電子表格如何使用apache poi將excel分割爲多個excel?

manager salary 
puk   2 
puk   3 
puk   4 
puk   5 
ser   3 
ser   4 
ser   5 
sos   23 
sos   24 
sos   25 
sos   26 
sos   27 

我需要這個電子表格拆分爲使用FileOutputStream三個不同shpreadsheets。第一個應包含經理puk,第二個ser的所有條目。 。 。

我不能想出一個很好的邏輯來分割它。我應該創建原始電子表格的臨時副本,然後刪除所有額外的行並保存它?比如何刪除行?

(這可能已與CSVReader很容易做到,但我需要保留格式)

+0

而不是刪除...轉移細胞將是更好的選擇。試試這個http://stackoverflow.com/a/19951595/624003 – Sankumarsingh

+0

你可能想要劃分模板和數據電子表格。該模板只有格式。從電子表格中讀取數據並將其放入臨時列表中。當數據輸出時,複製模板電子表格。 –

回答

2

這裏是如何做到你的要求,我已經測試了它的整個程序和它完美的作品:) 告訴我你的想法:

package additives; 
import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.xssf.usermodel.*; 

import java.util.*; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 

import java.io.*; 
public class StackOverflow { 

    public static Workbook readWorkbook(){ 

     Workbook wb=null; 
     try { 
      wb = WorkbookFactory.create(new File("stackOverflow.xlsx")); 
     } catch (InvalidFormatException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return wb; 
    } 

    public static void writeToSheet(List<String> name, List<Double> salary, int sheetCounter, List<Sheet> outputSheets){ 


     for(int i=0;i<salary.size();i++){ 

      Row row=outputSheets.get(sheetCounter).createRow(i); 

      Cell nameCell=row.createCell(0); 
      nameCell.setCellValue(name.get(i)); 

      Cell salaryCell=row.createCell(1); 
      salaryCell.setCellValue(salary.get(i)); 
     } 
    } 

    public static void writeToWorkbook(Workbook wb){ 

     try{ 
      FileOutputStream out=new FileOutputStream("new StackOverflow.xlsx"); 
      wb.write(out); 
      out.close(); 
     } catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args){ 

     Workbook inputWb=readWorkbook(); 
     Sheet inputWs=inputWb.getSheet("sheet1"); 

     List<String> name=new ArrayList<>(); 
     List<Double> salary=new ArrayList<>(); 

     Workbook outputWb=new XSSFWorkbook(); 
     List<Sheet> outputSheets=new ArrayList<>(); 

     int rowIndex=inputWs.getLastRowNum()+1; 
     int sheetCounter=0; 

     for(int i=1; i<rowIndex-1; i++){ 
      Row outerRow=inputWs.getRow(i); 
      Row innerRow=null; 
      Cell outerCell=outerRow.getCell(0); 
      Cell innerCell=null; 

      int j=0; 
      for(j=i+1;j<rowIndex;j++){ 

       innerRow=inputWs.getRow(j); 
       innerCell=innerRow.getCell(0); 

       if(outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){ 
        name.add(innerRow.getCell(0).getStringCellValue()); 
        salary.add(innerRow.getCell(1).getNumericCellValue()); 
       } 


       if(!outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){ 

        break; 
       } 


      } 

      name.add(outerRow.getCell(0).getStringCellValue()); 
      salary.add(outerRow.getCell(1).getNumericCellValue()); 
      i=j; 
      outputSheets.add(outputWb.createSheet("sheet"+sheetCounter)); 
      writeToSheet(name,salary,sheetCounter, outputSheets); 
      sheetCounter++; 
      name.clear(); 
      salary.clear(); 

      Row tempRow=inputWs.getRow(i); 
      try{ 
       name.add(tempRow.getCell(0).getStringCellValue()); 
       salary.add(tempRow.getCell(1).getNumericCellValue()); 
      }catch(Exception e){ 
       continue; 
      } 
     } 
     writeToWorkbook(outputWb); 
    } 
} 
+0

謝謝安尼斯,它是完美的 – CHEBURASHKA