首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >poi不能将数据写入.xlsx

poi不能将数据写入.xlsx
EN

Stack Overflow用户
提问于 2015-12-18 01:15:10
回答 3查看 1.9K关注 0票数 0

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

例如,

代码语言:javascript
复制
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)处创建一个单元格,其中我的意思是行。

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

我尝试了print 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) ... ...

但现在的情况是:

代码语言:javascript
复制
     `|    (row 3, col 0)         |    (row 4, col 0)         |    (row 5, col 0)         |    (row 6, col 0) ... ...`  Right now the xlsx is 4KB. it contains some other information, which have been put there via this very program. Those parts don't have this problem.
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-18 08:13:46

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

poi如何创建XSSFRow

代码语言:javascript
复制
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类:

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

CellUtil.getRow(rowIndex,sheet);

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

CellUtil.getCell(行,columnIndex);

票数 2
EN

Stack Overflow用户

发布于 2015-12-18 05:53:54

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

代码语言:javascript
复制
FileOutputStream out = new FileOutputStream(new File("path of excel file"));
    wb.write(out);
    wb.close();
   out.close();

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

票数 1
EN

Stack Overflow用户

发布于 2015-12-18 02:25:57

代码语言:javascript
复制
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;

这是我的密码..。

代码语言:javascript
复制
        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();
        }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34347089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档