jxls-2.x导出excel入门——基本操作

  之前随笔使用的是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;
    }

 

posted @ 2017-08-14 11:10  ---江北  阅读(3006)  评论(0编辑  收藏  举报
TOP