简体   繁体   English

使用java将数据导出到excel - 没有创建任何行

[英]exporting data to excel using java - no rows created

I need help in exporting data to an excel file. 我需要帮助将数据导出到excel文件。

The cells for row 12 is working fine but the cells for rowItems don't display anything at all. 第12行的单元格工作正常,但rowItems的单元格根本不显示任何内容。

The codes should export a list of items and its attributes from the database. 代码应从数据库中导出项目列表及其属性。

    Row row12 = sheet.createRow(11);
    Cell c12[] = new Cell[maxCols];

    for(int i = 0; i <c12.length; i++){
        c12[i] = row12.createCell(i);
        c12[i].setCellStyle(tableHeader2Bottom);
    }

    c12[0].setCellValue("1");
    c12[1].setCellValue("2");
    c12[2].setCellValue("3");
    c12[3].setCellValue("");
    c12[4].setCellValue("4");
    c12[5].setCellValue("5");
    c12[6].setCellValue("6");
    c12[7].setCellValue("7");
    c12[8].setCellValue("8");
    c12[9].setCellValue("9");
    c12[10].setCellValue("10");
    c12[11].setCellValue("11");
    c12[12].setCellValue("13");
    c12[13].setCellValue("14");
    c12[14].setCellValue("15");
    c12[15].setCellValue("16");

    Row rowItems[] = new Row[items.size()];
    Cell cItems[][] = new Cell[items.size()][maxCols];
    int startRow = 11;
    int endRow = 0;
    if(items.size() == 0){
        endRow = startRow;
    }
    for(int i = 0; i < items.size(); i++){
        rowItems[i] = sheet.createRow(i+startRow);
        for(int j = 0; j < cItems[i].length; j++){
            cItems[i][j] = rowItems[i].createCell(j);
            cItems[i][j].setCellStyle(borderedCell);
        }
        cItems[i][0].setCellValue(items.get(i).getName());
        cItems[i][1].setCellValue(1);
        cItems[i][2].setCellValue(items.get(i).getUnitCost());
        cItems[i][3].setCellValue(items.get(i).getUnitCost());
        cItems[i][4].setCellValue(items.get(i).getClassNumber());
        cItems[i][5].setCellValue(items.get(i).getPropertyNumber());
        cItems[i][6].setCellValue(items.get(i).getDateAcquired());
        cItems[i][7].setCellValue("");
        cItems[i][8].setCellValue("");
        cItems[i][9].setCellValue("");
        cItems[i][10].setCellValue("");
        cItems[i][11].setCellValue("");
        cItems[i][12].setCellValue("");
        cItems[i][13].setCellValue("");
        cItems[i][14].setCellValue("");
        cItems[i][15].setCellValue("");
        endRow = (i+startRow);
    }

screenshot of what it should look like 应该是什么样的截图

cItems[0][0] = A13, cItems[0][1 ] = B13, . cItems [0] [0] = A13,cItems [0] [1] = B13 ,. . .

The loop should get each item in the database and display it starting from row 13. 循环应该获取数据库中的每个项目并从第13行开始显示它。

By the way, I'm using Apache POI. 顺便说一下,我正在使用Apache POI。 :) :)

Thanks! 谢谢!

Just guessing. 只是猜测。

May be you should call sheet.addRow(row12) in the end. 也许你最后应该调用sheet.addRow(row12)。 The same is with columns row12.addColumn(column). 列row12.addColumn(列)也是如此。 And in the end sheet.close(). 最后是sheet.close()。

All of these are just guessings, I might be completely wrong ;). 所有这些只是猜测,我可能完全错了;)。

If you tell me what excel lib you are using, then I might be more specific :) 如果你告诉我你正在使用什么excel lib,那么我可能会更具体:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM