Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
XLSX excel(POI Java)。行和列上不显示数据_Java_Apache Poi_Xlsx - Fatal编程技术网

XLSX excel(POI Java)。行和列上不显示数据

XLSX excel(POI Java)。行和列上不显示数据,java,apache-poi,xlsx,Java,Apache Poi,Xlsx,这是我的密码: try { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=" + ReportID + ".xlsx"); String excel

这是我的密码:

try {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename="
                + ReportID + ".xlsx");

        String excelFileName = "C:\\Test.xlsx";
        XSSFWorkbook w = new XSSFWorkbook();
        System.out.println("w: " + w);
        XSSFSheet s = w.createSheet(ReportID);
        System.out.println("s: " + s);

        // Report Title
        s.createRow(0).createCell(0).setCellValue(Title);
        System.out.println("Title: " + Title);

        // Populate the worksheet
        int _col_cnt = HeadersLabel.length;
        XSSFRow row = s.createRow(_col_cnt);
        System.out.println("HeadersLabel: " + _col_cnt);

        for (int c = 0; c < _col_cnt; c++) {
            // Construct the header row
            String _h = HeadersLabel[c];
            System.out.println("_h: " + _h);
            //XSSFRow row = s.createRow(0);

            if (_h != null) {
                XSSFCell hd = row.createCell(c);
                hd.setCellValue(_h);                    
            }       


            int r = 5;          
            for (Iterator iter = Cells.iterator(); iter.hasNext();) {
                Object[] _o = (Object[]) iter.next();
                XSSFRow rowData = s.createRow(r);
                XSSFCell data = rowData.createCell(c);

                if (CellDataType[c].equals("STRING")
                        || CellDataType[c].equals("VARCHAR")) {
                    String _l = (String) _o[c];
                    if (_l != null) {
                        // Label label = new Label(c, r, (String) _o[c]);
                        //XSSFCell data = rowData.createCell(c);
                        data.setCellValue(_l);
                    }

                } else if (CellDataType[c].equals("DOUBLE")) {
                    Double _D = (Double) _o[c];
                    if (_D != null) {
                        // Number number = new Number(c, r,
                        // _D.doubleValue());
                        // s.addCell(number);
                        //XSSFCell data = rowData.createCell(c);
                        data.setCellValue(_D);
                    }

                } else if (CellDataType[c].equals("INTEGER")) {
                    Integer _I = (Integer) _o[c];
                    if (_I != null) {
                        // Number number = new Number(c, r,
                        // _I.doubleValue());
                        // s.addCell(number);
                        //XSSFCell data = rowData.createCell(c);
                        data.setCellValue(_I);

                    }
                } else if (CellDataType[c].equals("DATE")) {
                    Date _aDate = (Date) _o[c];
                    if (_aDate != null) {
                        // DateTime dateCell = new DateTime(c, r, _aDate,
                        // dateFormat);
                        // s.addCell(dateCell);
                        //XSSFCell data = rowData.createCell(c);
                        data.setCellValue(_aDate);
                    }
                } else if (CellDataType[c].equals("TIMESTAMP")) {
                    Timestamp _aTimestamp = (Timestamp) _o[c];
                    Date _aDate = Timestamp2Date(_aTimestamp);
                    if (_aDate != null) {
                        // DateTime dateCell = new DateTime(c, r, _aDate,
                        // dateFormat);
                        // s.addCell(dateCell);
                        //XSSFCell data = rowData.createCell(c);
                        data.setCellValue(_aDate);
                    }
                }
试试看{
setContentType(“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”);
setHeader(“内容处置”、“附件;文件名=”
+ReportID+“.xlsx”);
字符串excelFileName=“C:\\Test.xlsx”;
XSSFWorkbook w=新XSSFWorkbook();
System.out.println(“w:+w”);
XSSFSheet s=w.createSheet(ReportID);
System.out.println(“s:+s”);
//报告标题
s、 createRow(0).createCell(0).setCellValue(标题);
System.out.println(“标题:”+标题);
//填充工作表
int\u col\u cnt=HeadersLabel.length;
XSSFRow row=s.createRow(_col_cnt);
System.out.println(“标题标签:”+\u col\u cnt);
对于(int c=0;c<\u col\u cnt;c++){
//构造标题行
字符串_h=标题标签[c];
System.out.println(“\u h:”+\u h);
//XSSFRow row=s.createRow(0);
如果(_h!=null){
XSSFCell hd=row.createCell(c);
hd.setCellValue(_h);
}       
int r=5;
for(迭代器iter=Cells.Iterator();iter.hasNext();){
Object[]_o=(Object[])iter.next();
XSSFRow rowData=s.createRow(r);
XSSFCell data=rowData.createCell(c);
if(CellDataType[c].equals(“字符串”)
||CellDataType[c]。等于(“VARCHAR”)){
字符串_l=(字符串)_o[c];
如果(_l!=null){
//Label Label=新标签(c,r,(字符串)_o[c]);
//XSSFCell data=rowData.createCell(c);
data.setCellValue(_l);
}
}else if(CellDataType[c].equals(“DOUBLE”)){
Double _D=(Double)_o[c];
如果(_D!=null){
//编号=新编号(c,r,
//_D.doubleValue());
//s.addCell(编号);
//XSSFCell data=rowData.createCell(c);
data.setCellValue(_D);
}
}else if(CellDataType[c].equals(“INTEGER”)){
整数_I=(整数)_o[c];
如果(_I!=null){
//编号=新编号(c,r,
//_I.doubleValue());
//s.addCell(编号);
//XSSFCell data=rowData.createCell(c);
data.setCellValue(_I);
}
}else if(CellDataType[c].等于(“日期”)){
日期_aDate=(日期)_o[c];
如果(_aDate!=null){
//DateTime dateCell=新的日期时间(c,r,_aDate,
//日期格式);
//s.addCell(dateCell);
//XSSFCell data=rowData.createCell(c);
data.setCellValue(_aDate);
}
}else if(CellDataType[c].equals(“时间戳”)){
时间戳_aTimestamp=(时间戳)_o[c];
日期_aDate=时间戳2日期(_aTimestamp);
如果(_aDate!=null){
//DateTime dateCell=新的日期时间(c,r,_aDate,
//日期格式);
//s.addCell(dateCell);
//XSSFCell data=rowData.createCell(c);
data.setCellValue(_aDate);
}
}
XLSX excel无法捕获某些数据。前两列为空,但假设有数据出现。只有第三列有数据

现在的样子:


假设是这样的:

当您编写单元格内容时,对于每个单元格迭代,您都在创建新行,这实际上删除了您的前一行,因此在最后,您只获取最后一个单元格的数据。下面是您的代码

for (Iterator iter = Cells.iterator(); iter.hasNext();) {
            Object[] _o = (Object[]) iter.next();
            XSSFRow rowData = s.createRow(r);
            s.createRow(0).createCell(0).setCellValue(Title);
在进入此循环之前,您需要为每一行只调用一次create row。创建行后,您只需要创建列,并且必须为该行的每一列创建列,如下所示

    XSSFRow rowData = s.createRow(r);
 for (Iterator iter = Cells.iterator(); iter.hasNext();) {
            Object[] _o = (Object[]) iter.next();
            rowData..createCell(0).setCellValue(Title);

请注意这是我对你的代码的第一次思考,我在系统中没有尝试过。所以只要把它当作提示,在需要的地方就纠正它。然后再检查。< /P>嗨。谢谢。我明天会在办公室再试一次。我没有POI java的基础,我被要求做这个> @ USS36400,以添加到这个A。nswer,看看你的代码,你确实创建了许多似乎相互覆盖的单元格和行。你应该确保创建行和单元格一次且仅一次。@Sankumarsingh-代码不起作用。尝试后,我的excel只打印标题,标题为3列,第3列下只打印一个数据。没有h第1列和第2列的标题和数据。(与此类似,只是3r5d列上的数据不是很多,而是只有一个。)我已经设法编辑了代码并解决了标题标题的问题。但现在,前两列上的数据仍然存在问题。(已更新图像链接).你介意帮忙吗?@JonathanDrapeau我该如何重写它?很抱歉,因为我在编码方面真的很弱:/我已经设法编辑了代码并解决了标题标题的问题。但是现在,我在前两栏中显示的数据仍然有问题。(已更新图像链接)