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

你有一个太多的循环,

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