Apache Poi单元数据正在复制

时间:2021-08-08 20:24:54

I'm not sure why but when I output my excel file all the data is the same. When I print in the for loop before I output my file all the values are not equal. I've been looking at this for 1.5 hours and I can't for the life of me figure it out. Any ideas ?

我不知道为什么,但是当我输出excel文件时,所有的数据都是一样的。当我在输出文件之前打印for循环时,所有的值都不相等。我已经看了一个半小时了,我怎么也搞不清楚。什么好主意吗?

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 个解决方案

#1


1  

Your have one too many loops,

你有太多的循环,

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();
} 

#1


1  

Your have one too many loops,

你有太多的循环,

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();
}