Apache Poi单元格数据正在复制

时间:2014-06-14 01:35:19

标签: java excel apache-poi

我不确定为什么,但是当我输出我的excel文件时,所有数据都是一样的。当我在输出文件之前在for循环中打印时,所有值都不相等。我已经看了1.5个小时了,我不能为我的生活搞清楚。有什么想法吗?

public void excellGenerator(String toexcel,String oldData2){

            //System.out.println("THis is the excel file data:\n\n"+toexcel);

            String[] excel = toexcel.split("EOL\n");
            String[] dataToArray = oldData2.split("\\|");           
            String[][] finalExcel =new String[excel.length][];
            int l=0;

            for (int i=0; i<excel.length; i++) {        
                finalExcel[i]= excel[i].split("\\|");
            }   




        try{
            //+System.out.println("im Lost O_O oh no");
            String filename="LOA_"+output+".xls" ;
            HSSFWorkbook workbook=new HSSFWorkbook();
            HSSFSheet sheet =  workbook.createSheet("LOA Results");  
            HSSFRow rowhead=   sheet.createRow(0);

            for(int i=0; i < dataToArray.length ;i++){  

                rowhead.createCell(i).setCellValue(dataToArray[i]);
            }

            for(int i=1; i < excel.length+1 ;i++){  

                HSSFRow row =   sheet.createRow(i);

                for(int j=0; j < excel.length ;j++){    

                    for(int k=0; k < finalExcel[j].length ;k++){

                        row.createCell(k).setCellValue(finalExcel[j][k]);
                        System.out.print(finalExcel[j][k]+" ");
                    }
                    System.out.println();
                } 
            }

            FileOutputStream fileOut =  new FileOutputStream(filename);
            workbook.write(fileOut);
            fileOut.close();
            System.out.println("Your excel file has been generated!");

            } catch ( Exception ex ) {
                System.out.println(ex);

            }
        }

1 个答案:

答案 0 :(得分:1)

你有一个太多的循环,

for(int i=1; i <= excel.length; i++){  
  HSSFRow row = sheet.createRow(i);
  final int j = i - 1; // <-- it's actually the row index into finalExcel (0 based).
  for(int k=0; k < finalExcel[j].length; k++){
    row.createCell(k).setCellValue(finalExcel[j][k]);
    System.out.print(finalExcel[j][k]+" ");
  }
  System.out.println();
}