java将数据导出,带有合并单元格的excel--jxls技术

        jxls技术可以生成各种样式的报表,非常好用,深深地喜欢上了这个。说起用这个还是比较有意思的,当时项目有个导出表格的功能,但是没能合并单元格,客户不是很满意,当时项目中大家都说弄不了,我想着自己网上查查吧,就查到了这个,试了一下午完成了,很有成就感哪,哈哈。。。。好了,开始说正事吧。

        maven项目(其他的也类似)的使用步骤:

1.在pom.xml中引入jar

  <dependency> 
      <groupId>net.sf.jxls</groupId> 
      <artifactId>jxls-core</artifactId> 
      <version>1.0.5</version> 
  </dependency>

2.写导出方法

createExcel(String templateFileName, List<KhPeopleAttendance> list, String resultFileName){

    //templateFileName--模板名称,list--要导出的数据,resultFileName--导出后生成的excel的名称

   String absolutePathString = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

   path=absolutePathString.substring(0, index+7)+"src/main/webapp/WEB-INF/exports/";

   //得到模板文件路径
   String srcFilePath = path + templateFileName;

   //导出文件路径
   String destFilePath ="D://" + resultFileName;

    Map<String, List<KhPeopleAttendance>> beanParams = new HashMap<String, List<KhPeopleAttendance>>();

   beanParams.put("list", list); 
   //创建XLSTransformer对象
   XLSTransformer transformer = new XLSTransformer();

  //生成Excel文件--合并单元格
       InputStream is = new FileInputStream(srcFilePath);
    HSSFWorkbook workBook = (HSSFWorkbook) transformer.transformXLS(is, beanParams);
    HSSFSheet sheet = workBook.getSheetAt(0);

    if("支撑类".equals(orgtype)){
     if(indexcommon==hx){
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+zcindex),(2+indexcommon+zcindex+a-1),1,1));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+zcindex),(2+indexcommon+zcindex+a-1),2,2));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+zcindex),(2+indexcommon+zcindex+a-1),9,9));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+zcindex),(2+indexcommon+zcindex+a-1),10,10));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+zcindex),(2+indexcommon+zcindex+a-1),13,13));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+zcindex),(2+indexcommon+zcindex+a-1),14,14));
     }else{
      sheet.addMergedRegion(new CellRangeAddress((2+zcindex),(2+zcindex+a-1),1,1));
      sheet.addMergedRegion(new CellRangeAddress((2+zcindex),(2+zcindex+a-1),2,2));
      sheet.addMergedRegion(new CellRangeAddress((2+zcindex),(2+zcindex+a-1),9,9));
      sheet.addMergedRegion(new CellRangeAddress((2+zcindex),(2+zcindex+a-1),10,10));
      sheet.addMergedRegion(new CellRangeAddress((2+zcindex),(2+zcindex+a-1),13,13));
      sheet.addMergedRegion(new CellRangeAddress((2+zcindex),(2+zcindex+a-1),14,14));
     }
    }
    if("核心类".equals(orgtype)){
     if(indexcommon==zc){
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+hxindex),(2+indexcommon+hxindex+a-1),1,1));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+hxindex),(2+indexcommon+hxindex+a-1),2,2));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+hxindex),(2+indexcommon+hxindex+a-1),9,9));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+hxindex),(2+indexcommon+hxindex+a-1),10,10));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+hxindex),(2+indexcommon+hxindex+a-1),13,13));
      sheet.addMergedRegion(new CellRangeAddress((2+indexcommon+hxindex),(2+indexcommon+hxindex+a-1),14,14));
     }else{
      sheet.addMergedRegion(new CellRangeAddress((2+hxindex),(2+hxindex+a-1),1,1));
      sheet.addMergedRegion(new CellRangeAddress((2+hxindex),(2+hxindex+a-1),2,2));
      sheet.addMergedRegion(new CellRangeAddress((2+hxindex),(2+hxindex+a-1),9,9));
      sheet.addMergedRegion(new CellRangeAddress((2+hxindex),(2+hxindex+a-1),10,10));
      sheet.addMergedRegion(new CellRangeAddress((2+hxindex),(2+hxindex+a-1),13,13));
      sheet.addMergedRegion(new CellRangeAddress((2+hxindex),(2+hxindex+a-1),14,14));
     }
    }

    OutputStream os = new FileOutputStream(destFilePath);
          workBook.write(os); 
         
          is.close(); 
          os.flush(); 
          os.close();
    return resultFileName;

   }

3.创建导出模板

4.在模板里写标签

5.导出结果




你可能感兴趣的:(java将数据导出,带有合并单元格的excel--jxls技术)