Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java poi无法将数据写入.xlsx_Java_Excel_Apache Poi - Fatal编程技术网

Java poi无法将数据写入.xlsx

Java poi无法将数据写入.xlsx,java,excel,apache-poi,Java,Excel,Apache Poi,问题是,当我试图将数据写入单元格时,单元格要么尚未创建,要么就是没有显示其中的数据 比如说, Row[] rownames = new Row[names.size()]; for(int i = 0; i < names.size(); i++){ rownames[i] = sheet.createRow(i+3); Cell machine = rownames[i].createCell(0); machine.setCellType(Cell.CELL_T

问题是,当我试图将数据写入单元格时,单元格要么尚未创建,要么就是没有显示其中的数据

比如说,

Row[] rownames = new Row[names.size()];
for(int i = 0; i < names.size(); i++){
    rownames[i] = sheet.createRow(i+3);
    Cell machine = rownames[i].createCell(0);
    machine.setCellType(Cell.CELL_TYPE_STRING);
    machine.setCellValue(names.get(i).toString());
}
Row[]rownames=新行[names.size()];
对于(int i=0;i
names[]
是一个包含名称列表的数组

Cell machine=rownames[i].createCell(0)在(i+3,0)处创建一个单元格,其中i表示行

machine.setCellValue(names.get(i.toString())将单元格值设置为相应的名称[i]

我尝试打印
names[]
machine.getStringCellValue()
,它们都可以返回完全正确的数据(如输出到控制台)。但xlsx文件中没有任何内容。非常感谢

编辑: 让我更清楚地解释一下,如果一切顺利,这个xlsx文件的每一部分都应该是这样的:

FileOutputStream out = new FileOutputStream(new File("path of excel file"));
    wb.write(out);
    wb.close();
   out.close();

哈利(第三排第0列)
凯特(第4排第0列)
吉姆(第5排第0列)
亚伦(第6行第0列)
...
…

但现在的情况是:


|(第3行第0列)
|(第4行第0列)
|(第5行第0列)
|(第6行第0列)
...
...
现在xlsx是4KB。它包含一些其他信息,这些信息是通过这个程序放在那里的。这些部件没有这个问题

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
这是我的密码

        HSSFFont boldFont; 
        HSSFFont bodyFont; 

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("namefile");
        HSSFRow row = sheet.createRow((short)0);

        //HSSFCellStyle cellStyle = workbook.createCellStyle();      
        //cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

       //cellStyle.setFillForegroundColor(HSSFColor.LAVENDER.index);
        //cellStyle.setFillPattern(HSSFCellStyle.ALIGN_CENTER);


        row.setHeightInPoints(23);

        sheet.setColumnWidth((short)0, (short)2000);
        sheet.setColumnWidth((short)1, (short)3000);
        sheet.setColumnWidth((short)2, (short)3000);
        sheet.setColumnWidth((short)3, (short)3000);
        sheet.setColumnWidth((short)4, (short)3000);
        sheet.setColumnWidth((short)5, (short)3000);
        sheet.setColumnWidth((short)6, (short)3000);
        sheet.setColumnWidth((short)7, (short)3000);
        sheet.setColumnWidth((short)8, (short)3000);
        sheet.setColumnWidth((short)9, (short)3000);
        sheet.setColumnWidth((short)10, (short)3000);
        sheet.setColumnWidth((short)11, (short)3000);

        row.createCell((short)0).setCellValue("FLT NO");
        row.createCell((short)1).setCellValue("LEG");
        row.createCell((short)2).setCellValue("DATE");
        row.createCell((short)3).setCellValue("AC-REG");
        row.createCell((short)4).setCellValue("SCHED DEP");
        row.createCell((short)5).setCellValue("OFFBLK");
        row.createCell((short)6).setCellValue("AIRBORNE");
        row.createCell((short)7).setCellValue("LANDING");
        row.createCell((short)8).setCellValue("ONBLK");
        row.createCell((short)9).setCellValue("SCHED ARR");
        row.createCell((short)10).setCellValue("FUEL_USED_PILOT");
        row.createCell((short)11).setCellValue("ACTUAL FUEL");      


        ProofSheetFPRModel model = new ProofSheetFPRModel();
        int rownum = 1;
        for (int i=0; i<DataList.size(); i++) 
        {
            model = DataList.get(i);
            row = sheet.createRow((short)rownum);
            row.createCell((short)0).setCellValue(model.getFlightNo());
            row.createCell((short)1).setCellValue(model.getLEG());
            row.createCell((short)2).setCellValue(model.getDate());
            row.createCell((short)3).setCellValue(model.getAC_REG());
            row.createCell((short)4).setCellValue(model.getSCHED_DEP());
            row.createCell((short)5).setCellValue(model.getOFF_BLK());
            row.createCell((short)6).setCellValue(model.getAIRBORNE());
            row.createCell((short)7).setCellValue(model.getLANDG());
            row.createCell((short)8).setCellValue(model.getON_BLK());
            row.createCell((short)9).setCellValue(model.getSCHED_ARR());
            row.createCell((short)10).setCellValue(model.getFUEL_USED_PILOT());
            row.createCell((short)11).setCellValue(model.getACTUAL_FUEL());
            rownum++;
        }

        try 
        {
            FileOutputStream out =  new FileOutputStream(new File(filePath));
            workbook.write(out);
            out.close();
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
hssfont粗体字体;
HSSFFont字体;
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet sheet=workbook.createSheet(“名称文件”);
HSSFRow行=sheet.createRow((短)0);
//HSSFCellStyle cellStyle=工作簿.createCellStyle();
//cellStyle.setFillBackgroundColor(HSSFColor.Gray\u 25%指数);
//cellStyle.setFillForegroundColor(HSSFColor.LAVENDER.index);
//cellStyle.setFillPattern(HSSFCellStyle.ALIGN_CENTER);
行。设置高度为点(23);
表1.setColumnWidth((短)0,(短)2000);
表.设置柱宽((短)1,(短)3000);
表1.设置柱宽((短)2,(短)3000);
表.设置柱宽((短)3,(短)3000);
表.设置柱宽((短)4,(短)3000);
表.设置柱宽((短)5,(短)3000);
表.设置柱宽((短)6,(短)3000);
表.设置柱宽((短)7,(短)3000);
表1.设置柱宽((短)8,(短)3000);
表.设置柱宽((短)9,(短)3000);
表.设置柱宽((短)10,(短)3000);
活页.设置柱宽((短)11,(短)3000);
row.createCell((短)0.setCellValue(“FLT编号”);
row.createCell((短)1.setCellValue(“LEG”);
row.createCell((短)2.setCellValue(“日期”);
row.createCell((短)3.setCellValue(“AC-REG”);
row.createCell((短)4.setCellValue(“SCHED DEP”);
row.createCell((短)5.setCellValue(“OFFBLK”);
行createCell((短)6).setCellValue(“机载”);
row.createCell((短)7.setCellValue(“LANDING”);
row.createCell((短)8.setCellValue(“ONBLK”);
row.createCell((短)9.setCellValue(“SCHED ARR”);
行.createCell((短)10).setCellValue(“使用的燃料和飞行员”);
行createCell((短)11).setCellValue(“实际燃料”);
ProofSheetFPRModel model=新的ProofSheetFPRModel();
int rownum=1;

对于(int i=0;i您尚未发布文件打开和关闭代码。根据描述,您似乎没有将数据写回Excel文件。请执行以下操作:

FileOutputStream out = new FileOutputStream(new File("path of excel file"));
    wb.write(out);
    wb.close();
   out.close();

执行完整个代码后,检查excel文件中生成的输出。

看起来像是在后续(未过账)代码中多次按同一索引创建行

poi如何创建
XSSFRow

public XSSFRow createRow(int rownum) {
    CTRow ctRow;
    XSSFRow prev = _rows.get(rownum);
    if(prev != null){
        // the Cells in an existing row are invalidated on-purpose, in order to clean up correctly, we
        // need to call the remove, so things like ArrayFormulas and CalculationChain updates are done 
        // correctly. 
        // We remove the cell this way as the internal cell-list is changed by the remove call and 
        // thus would cause ConcurrentModificationException otherwise
        while(prev.getFirstCellNum() != -1) {
            prev.removeCell(prev.getCell(prev.getFirstCellNum()));
        }

        ctRow = prev.getCTRow();
        ctRow.set(CTRow.Factory.newInstance());
    }
    ...
}
所以,若行存在并且包含单元格,则所有包含数据的单元格都将被删除

要避免这种情况,请使用
CellUtil
class:

  • 从电子表格中获取一行,如果该行不存在,则创建该行

  • 从行中获取特定单元格。如果该单元格不存在,则创建它


到底是什么问题?您是说生成的.xslx文件长度为零字节吗?如果是这个问题,您需要显示保存.xlsx文件的代码。@ErwinBolwidt我更新了这个问题,如果我理解了,请看一下。非常感谢。您如何重用
行名[]
array?@dlopatin是的,我使用了相同的行索引,而不是确切的
rownames[]
,这是相同的。您的解决方案完全解决了这个问题。这如何解决OP存在的问题?@CherryShappy您的意思是我不应该使用行数组来创建单元格,或者我应该使用
(简短)
?我以前尝试过这些,但都没有用。
工作簿wb=new XSSFWorkbook();outputdata(wb);File outputfile=new File(outputfilename);FileOutputStream fileout=new FileOutputStream(outputfile);wb.write(fileout);fileout.close();
这是要输出的代码。我不认为这是导致其他信息正确输出的原因。@mk08
CellUtil.getCell(row, columnIndex);