之前随笔使用的是1.x的比较古老的版本了,已经不再维护,接下来使用较新的2.x的版本进行导出

    之前一直按照其他的博客与官网的随笔进行导出,发现一直报错,后面更换了POI的版本为3.16(因为jxls也是使用的最新的版本),就解决了jar包的冲突问题

    jar冲突参考的是:http://www.cnblogs.com/mvilplss/p/6101676.html

一、概述

  2.x更新后,比较大的不同是模板,之前的模板是直接将标签写在模板中:

   新版的模板使用的是批注(单元格右键——插入批注即可)的形式(开始不熟悉excel,还找了半天这是啥):

  还有xml这种稍微不那么直观的形式待后续更新

  更多的概述介绍可以参见官网:http://jxls.sourceforge.net/

  入门程序参考:http://www.cnblogs.com/klguang/p/6425422.html

二、HelloWorld入门

  依赖直接根据官网起步提示进行引入:

<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.4.1</version>
</dependency>

  //若实际maven项目中采用此依赖,请将版本分离为properties统一管理

  当然,还要的依赖包括(NosuchMethod引起的异常请检查依赖的冲突,版本的问题)

  依赖的介绍可以参见官网:

<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.6</version>
</dependency>

  由于jxls使用的是最新的版本,这里建议POI也调成新的版本(经测试:2.4.1配3.16完美使用)

<poi.version>3.16</poi.version>
        <!-- poi office -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>

  //jar有待测试具体应该引入哪些版本可以兼容,哪些jar是最小引入

  项目结构如下:

  //其中,测试类位于上,模板文件位于下(关于这里模板文件的读取,请参见:http://www.cnblogs.com/jiangbei/p/6744882.html

    这里存在一些命名与规范等问题,实际使用时请规范相关命名与文件位置

  模板文件

  先看模板文件(temp2.xlsx),随笔开头已经对比过新老模板的区别。这里必须指出:批出中的标签只能在一行,不能换行!

  

  注意看有两处批注,其中第一处批注信息为:  jx:area(lastCell = "E4")

    

            第二处批注信息为:    jx:each(items="reclist" var="receiver" lastCell="E4")  ——  请勿将标签换行!

          导出的后端代码:

    @RequestMapping(value = "/export2")
public String testExport2() throws Exception{
List<receiver> reclist = reSe.findall();
System.out.println("导出前查询list:=====================");
System.out.println(reclist);
InputStream in = receiverController.class.getClassLoader().getResourceAsStream("temp2.xlsx");
OutputStream out = new FileOutputStream("F:/output.xlsx");
Context context = new Context();
context.putVar("reclist",reclist);
JxlsHelper.getInstance().processTemplate(in, out, context);
SysoUtils.print("导出完毕!");
return null;
}

  导出内容如下:(实际操作时请规范使用)

  //其中,时间格式可以修改模板处单元格格式,详见jxls1.x随笔

  再修改为导出给浏览器下载:

@RequestMapping(value = "/export2")
public String testExport2(HttpServletResponse response) throws Exception{
List<receiver> reclist = reSe.findall();
System.out.println("导出前查询list:=====================");
System.out.println(reclist);
InputStream in = receiverController.class.getClassLoader().getResourceAsStream("temp2.xlsx");
OutputStream out = null;
response.setHeader("Content-Disposition", "attachment;filename=export.xlsx");
/*response.setContentType("application/ms-excel;charset=UTF-8");*/
response.setContentType("application/vnd..ms-excel;charset=UTF-8");
out = response.getOutputStream();
Context context = new Context();
context.putVar("reclist",reclist);
JxlsHelper.getInstance().processTemplate(in, out, context);
SysoUtils.print("导出完毕!");
return null;
}

最新文章

  1. Cordova webapp实战开发:(2)认识一下Cordova
  2. android键盘弹出头部上移处理
  3. poj2528(线段树)
  4. WinFrom中使用WPF的窗体
  5. 深拷贝/浅拷贝之Js / AngularJs
  6. Example016实现下拉框
  7. 一次日语翻译的Chrome插件开发经历
  8. alpha-咸鱼冲刺day8
  9. angular.module()
  10. C语言博客作业05——指针
  11. js获取复选框checkbox选中的多个值
  12. 莫烦theano学习自修第九天【过拟合问题与正规化】
  13. 编码标准:ASCII、GBK、Unicode(UTF8、UTF16、UTF32)
  14. static final 内部类
  15. 【2014年12月6日】HR交流会
  16. Andrew Ng-ML-第十六章-异常检测
  17. spark mllib和ml类里面的区别
  18. hbase优缺点
  19. MySQL GTID (四)
  20. 路飞学城知识点3缓存知识点之二redis

热门文章

  1. Java基础之二维数组的回顾
  2. PhoneGap 的存储 API_Web Sql
  3. sort给文件按照大小排序
  4. tmux 后台运行程序
  5. luogu P3941 入阵曲
  6. C# 密封类sealed
  7. Haroopad 中文不显示
  8. 在CentOS7上安装MySQL5.7-YUM源方式
  9. oracle 的replace()
  10. 键盘录入6个int类型的数据存入数组arr中,将arr数组中的内容反转...