永发信息网

java poi合并单元表格(求帮助啊)

答案:4  悬赏:30  手机版
解决时间 2021-04-01 15:59
  • 提问者网友:却不属于对方
  • 2021-04-01 04:56
java poi合并单元表格(求帮助啊)
最佳答案
  • 五星知识达人网友:思契十里
  • 2021-04-01 05:56
去年哥也写过这么个东东。

new Region 这个已经过时了,现在是用addMergedRegion(new CellRangeAddress(row,row,col,col);
追问呵呵,你的这个我喜欢,求源码。。。594689303追答我这个是根据一颗树生成的excel。我是用递归将这些数据封装成树,然后再写入到excel的,这个可能跟你的不大一样。而且我这个是边写数据,边合并的。你的是写完了数据,然后再考虑合并。。现在合并的方法是这个addMergedRegion(new CellRangeAddress(row,row,col,col);
全部回答
  • 1楼网友:青尢
  • 2021-04-01 09:05
  
 private void outExcelFile(HttpContext context,
   IContextDictionary dictionary, DataRowCollections rows,
   String fileName, List fields, List fieldsName) {
  int cellSize = fields.size();
  if(cellSize == 0){
   LogManager.debug("没有指定列头信息,无法导出Excel文件!");
   return;
  }
  //============创建样式    start
  HSSFWorkbook workbook = new HSSFWorkbook();
  HSSFSheet sheet = workbook.createSheet();
  //列头字体样式
        HSSFFont headerFont = workbook.createFont();
        headerFont.setFontName("宋体");
        headerFont.setFontHeightInPoints((short) 12);
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //列头样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFont(headerFont);
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        headerStyle.setLocked(true);
        headerStyle.setWrapText(true);
        //标题样式
        HSSFFont titleFont = workbook.createFont();
        titleFont.setFontName("宋体");
        titleFont.setFontHeightInPoints((short) 15);
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        
        HSSFCellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setFont(titleFont);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setLocked(true);
        titleStyle.setWrapText(true);
        
        //普通单元格字体样式
        HSSFFont cellFont = workbook.createFont();
        cellFont.setFontName("宋体");
        cellFont.setFontHeightInPoints((short)12);
        //普通单元格样式
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(cellFont);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        cellStyle.setLocked(true);
        cellStyle.setWrapText(true);
        //============创建样式    end
        
        //设置序号列、列宽和标题行     start
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.setHeightInPoints(50);
        HSSFCell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(new HSSFRichTextString(fileName));
        titleCell.setCellStyle(titleStyle);
        //sheet.addMergedRegion(new Region(0,(short)0,0,(short)cellSize));//合并单元格--方法过时                                                                                                                                                                                                                                                                          
        //CellRangeAddress  起始行 结束行 起始列 结束列
        sheet.addMergedRegion(new CellRangeAddress(0,0,(short)0,(short)cellSize));//合并单元格
       
        //显示总的数据个数 start
        HSSFRow countRow = sheet.createRow(1);
        countRow.setHeightInPoints(40);
        HSSFCell countCell = countRow.createCell(0);
        countCell.setCellValue(new HSSFRichTextString("共计专项检查("+rows.size()+")项"));
        countCell.setCellStyle(headerStyle);
        sheet.addMergedRegion(new CellRangeAddress(1,1,(short)0,(short)cellSize));//合并单元格
  //显示总的数据个数 end 
        
        HSSFRow headerRow = sheet.createRow(2);
        headerRow.setHeightInPoints(35);
        HSSFCell headerCell = null;
        
        //序号
        int startIndex = 0 ;
     headerCell = headerRow.createCell(0);
  sheet.setColumnWidth(0, 2000);
  headerCell.setCellValue(new HSSFRichTextString("序号"));
  
  headerCell.setCellStyle(headerStyle);
  startIndex ++ ;
  
  //列头
  for(int i = 0; i < cellSize; i ++){
   sheet.setColumnWidth(startIndex + i, 7000);
   headerCell = headerRow.createCell(startIndex + i);
   headerCell.setCellValue(new HSSFRichTextString(fields.get(i)));
   headerCell.setCellStyle(headerStyle);
  }
  //设置序号列、列宽和标题行   end
  
  //文件正文 start
  int rowNum = 1;
  int rowIndex = 0;
  HSSFRow row = null;
  List cellRangeLst = new ArrayList(0);
  Integer [] arr = null;
  int l = 0;
  String orgName = ""; 
  for(int j = 2; j   DataRow dataRow = rows.get(rowIndex); //对应数据库字段
   HSSFCell cell = null;
   row = sheet.createRow(j + 1);
         row.setHeightInPoints(55);
   //序号
   cell = row.createCell(0);
   cell.setCellValue(rowNum++);
      cell.setCellStyle(cellStyle);
      
      if(StringHelper.isNullOrEmpty(orgName)){
       arr = new Integer[2];
       arr[0] = j + 1;
       l =j + 1;
       orgName = dataRow.getString("ORGNAME");
      }else{
       if(!orgName.equals(dataRow.getString("ORGNAME"))){
        arr[1] = j;
        cellRangeLst.add(arr);
        sheet.addMergedRegion(new CellRangeAddress(l,j,1,1));
        arr = new Integer[2];
        l = j+1;
        orgName = dataRow.getString("ORGNAME");
       }
       
       if(rowIndex == rows.size() - 1){
        arr[1] = j+1;
        cellRangeLst.add(arr);
        sheet.addMergedRegion(new CellRangeAddress(l,j+1,1,1));
       }
      }
      
      for(int k = 0; k < cellSize; k++){
       cell = row.createCell(k + startIndex);
       String column = fieldsName.get(k); //对应数据库字段
       String value = "";
       if("APSJ".equals(column)){
        value = getAPSJValue(dataRow.getString(column));
       }else{
        value = dataRow.getString(column);
       }
       cell.setCellValue(new HSSFRichTextString(value));
          cell.setCellStyle(cellStyle);
      }
      rowIndex ++;
  }
  //文件正文 end
  
  //for(Integer[] te : cellRangeLst){
  // sheet.addMergedRegion(new CellRangeAddress(te[0],te[1],1,1));//合并处室单元格
  //}
  
  //下载
  HttpServletResponse response = context.getResponse();
  
  response.setContentType("application/x-download;charset=UTF-8");
  String title = "export";
  try {
   title = java.net.URLEncoder.encode(fileName, "UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  response.addHeader("Content-Disposition", "attachment;filename=" + title + ".xls");
  try {
   OutputStream out = response.getOutputStream();
   workbook.write(out);
   out.flush();
   out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
//参考一下吧追问谢谢你的帮助
  • 2楼网友:山河有幸埋战骨
  • 2021-04-01 08:18
你是想用JAVA生成这样一个文档么?是的话留下邮箱,我写了一个工具类,发给你。这个不是一两句代码就搞定的。追问要啊!594689303
  • 3楼网友:上分大魔王
  • 2021-04-01 07:04
poi打印Excel中你可以用你定义的sheet里面的自带合并单元格方法。
EG:sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 7));
sheet就是你自己定义的sheet啦,里面的参数是行,列,行,列。刚刚我写的那个就是合并第一行从
第0个单元格到底7个单元格的合并语句。
另外poi打印过程中系统是不是识别自动合并那几个单元格,需要你手动提前设置好。追问谢谢你的回答追答不客气
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯