poi无法将数据写入.xlsx

时间:2015-12-18 01:15:11

标签: 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_TYPE_STRING);
    machine.setCellValue(names.get(i).toString());
}

names[]是一个包含名称列表的数组。

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

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

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

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

harry | (row 3, col 0) kate | (row 4, col 0) jim | (row 5, col 0) aaron | (row 6, col 0) ... ...

但现在的情况是:

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

3 个答案:

答案 0 :(得分:2)

看起来您在进一步(未发布)代码中多次按相同索引创建行。

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:

  •   

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

    CellUtil.getRow(rowIndex, sheet);
    
  •   

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

    CellUtil.getCell(row, columnIndex);
    

答案 1 :(得分:1)

您尚未发布文件开始和结束代码。根据描述,您似乎没有将数据写回Excel文件。做这样的事情:

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

执行完整代码后,检查excel文件以获取生成的输出。

答案 2 :(得分:-1)

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