u010224911
靑雨
采纳率100%
2015-04-24 13:45 阅读 5.4k

java poi 导出excel 把一样的数据动态合并单元格

急求,有一个list数据 导出成图片说明
这种格式 公司和年份两列是要有动态合并功能 并且年份合并的时候不能跨公司 有没有大神帮帮忙...

   HSSFWorkbook workbook = new HSSFWorkbook();
  String[] columnNames = new String[]{"编号","公司", "年份","性质", "名称","议案审核","召开时间","我方董监事","开展情况","决议"};
  String[] columnKey = new String[]{"BIANHAO", "SGLWDW" ,"SGND","SGHYXZ","TITLENAME","BILLAUDIT","SGZKSJ","SGWFDS","DEVELOPMENT","DECISION"};
  Map resMap = new HashMap();
  try {
   if(list.size()==0){
    resMap=new HashMap<String, String>();
    resMap.put("tsBy1", "没有数据");
    list.add(resMap);
   }
     HSSFSheet sheet = workbook.createSheet("sheet"+1);
     //首先,我们读取list中的第一个元素,根据它来确定工作表的列名,以及输出数据所对应的方法数组
     Map<String,String> map2 = list.get(0);

     HSSFRow row = sheet.createRow(0); //创建第1行,也就是输出表头
     HSSFCell cell;
     for(int i=0;i<columnNames.length;i++){
      cell = row.createCell(i); //创建第i列
      cell.setCellValue(new HSSFRichTextString(columnNames[i]));
     }

     //下面是输出各行的数据
     for (int i = 0; i < list.size(); i++) {
      map2=(Map<String,String>)list.get(i);
      row=sheet.createRow(i+1);//创建第i+1行

      for ( int j = 0; j < columnKey.length; j ++){
       cell = row.createCell(j);//创建第j列
       if(columnKey[j]==null || "".equals(columnKey[j])){
        cell.setCellValue(i+1);
       }else{
        Object obj = map2.get(columnKey[j]);
        cell.setCellValue(obj==null?"":obj.toString());
       }
      }
     }
   row=sheet.createRow((short)(sheet.getLastRowNum()+1)); //在现有行号后追加数据  
   Region range = new Region(sheet.getLastRowNum(), (short)0, sheet.getLastRowNum(), (short)9);
         sheet.addMergedRegion(range);
         row.createCell(0).setCellValue(convoke+"家企业“已召开”,"+submit+"家企业议案“已提交”,"+archive+"家企业决议“已归档”,"+audit+"家企业议案“已审核”"); //设置第一个(从0开始)单元格的数据  
   OutputStream out = null;
   HttpServletResponse response = this.getResponse();
   response.setContentType("octets/stream");
   response.addHeader("Content-Disposition", "attachment;filename=_directorsDetail.xls");
   try {
    out = response.getOutputStream();
    workbook.write(out);
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐