java - 使用 Apache-POI 创建的 Excelsheet 没有标题

标签 java excel apache-poi

下面的代码片段将结果集数据导出到 excel 文件,但它不会创建标题。 我用过

HSSFRow rowhead = sheet.createRow((short) 0);创建标题,但创建的 Excel 工作表仅包含没有标题的数据。可能是什么问题?请指导。

public boolean prepareExcelFilefromQuery(Collection<List> queryDataRowWise,HttpServletResponse response)        
    HSSFRow row = null;
                HSSFCell cell=null;
                Integer rowCounter=0;
                Integer colCounter;
                boolean success=false;
                try {

                    Iterator<List> rowIterator = queryDataRowWise.iterator();

                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet sheet = workbook.createSheet("Zero_Report_N");

                    HSSFRow rowhead = sheet.createRow((short) 0);

                      rowhead.createCell((short) 0).setCellValue("APPLN_RECD_DT");
                    rowhead.createCell((short) 1).setCellValue("POLICY_NO");
                    rowhead.createCell((short) 2).setCellValue("APPLN_NO");
                    rowhead.createCell((short) 3).setCellValue("OR_NUMBER");


                    while(rowIterator.hasNext())
                    {
                        colCounter=0;

                        queryDataColWise=(List)rowIterator.next();

                        Iterator colIterator = queryDataColWise.iterator();

                        row = sheet.createRow(rowCounter++);
                        while(colIterator.hasNext())
                        {
                            cell = row.createCell(colCounter++);


                            Object o = colIterator.next();

                            if(o instanceof java.lang.String )
                            {   
                                String s=(String)o;
                                cell.setCellValue(s);
                            }
                            else if(o instanceof java.lang.Double )
                            {
                                Double d=(Double)o;
                                cell.setCellValue(d);
                            }
                            else if(o instanceof java.lang.Integer )
                            {
                                Integer i=(Integer)o;
                                cell.setCellValue(i);
                            }
                            else if(o instanceof java.util.Date )
                            {   
                                Date date=(Date)o;

                                SimpleDateFormat FORMATTER;              

                                FORMATTER = new SimpleDateFormat("MM/dd/yyyy");  
                                String date11 = FORMATTER.format(date);     

                                HSSFCellStyle cellStyle = workbook.createCellStyle();
                                cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
                                cell.setCellStyle(cellStyle);
                                cell.setCellValue(FORMATTER.parse(date11));
                            }
                        }
                    }

        workbook.write(response.getOutputStream());
                success=true;
    catch (Exception e) {
            logger.debug("Exception caught in prepareExcelFilefromQuery class ", e);
                }
        return success;
    }

其中 expReportDetailCol 包含结果集数据 Collection<List> expReportDetailCol = new ArrayList<List>();

最佳答案

您需要 ++rowCounter 而不是 rowCounter++rowCounter++ 表示给我当前值,然后递增。您的 rowCounter 值从零开始,因此您的第一组数据会覆盖它

要么在处理完标题后增加 rowCounter,要么改用++rowCounter

关于java - 使用 Apache-POI 创建的 Excelsheet 没有标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11667589/

相关文章:

java - 如何为java对象生成校验和

excel - Excel表格中的移动范围/数组引用

java - 使用 SXSSF 写入大 Excel 文件时出现内存不足异常

java - setCellType(HSSFCELL.CELL_TYPE_NUMERIC) 在 apache poi 中不起作用

java - Apache POI 只写入一条记录

java - 在 Firebase 和 Retrieve 中保存数据

java - 我正在尝试从android studio内部存储中的.txt文件中读取文本

java - 正确管理 JavaFX 中的线程

c# - asp.net TreeView 导出到 excel

excel - 基于文本字段的 Excel 数据透视表中的条件格式