首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache-POI创建的Excelsheet没有标题

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

Stack Overflow用户
提问于 2012-07-26 18:43:50
回答 2查看 4.3K关注 0票数 1

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

要创建标题的HSSFRow rowhead = sheet.createRow((short) 0);,但创建的excel工作表只包含没有标题的数据。什么是issue?.Please指南。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-27 04:14:14

你想要++rowCounter而不是rowCounter++rowCounter++意味着给我当前的值,然后递增。开始时,rowCounter值为零,因此第一组数据会覆盖该值

或者在处理完标头后增加rowCounter,或者改为使用++rowCounter

票数 4
EN

Stack Overflow用户

发布于 2012-07-27 18:49:17

HSSFSheet sheet = workbook.createSheet("Zero_Report_N");//创建工作表后

你可以通过放入arraylist来添加头部

代码语言:javascript
复制
  HSSFCell cell = row.createCell(i);//u can pass values of headers
    cell.setCellValue(value);
    sheet.autoSizeColumn(i);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11667589

复制
相关文章

相似问题

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