java导出Excel(Jxls模板)

Jxls工具类

package cn.microvideo.aud.report.jxls.util;

import cn.microvideo.aud.report.export.rep.TruckTrafficRep;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;


public class JxlsUtil {
    /**
     *
     * @param templatePath
     * @param os
     * @param datasource
     * @throws IOException
     */
    public static void exportExcel(String templatePath, OutputStream os,  List datasource) throws IOException{
        try(InputStream is = new FileInputStream(templatePath)) {
            Context context = new Context();
            context.putVar("strategyHandled", datasource);
            JxlsHelper.getInstance().processTemplate(is, os, context);
        }
    }

    /**
     *
     * @param is   模板地址
     * @param os    导出的空白文件
     * @param datasource  数据集合
     * @throws IOException
     */
    public static void exportExcel(InputStream is, OutputStream os,  List datasource) throws IOException{
        Context context = new Context();
        context.putVar("strategyHandled", datasource);
        JxlsHelper.getInstance().processTemplate(is, os, context);
    }

    public static void exportExcel(InputStream is, HttpServletResponse response, List datasource,Map<String,String> map,String fileName) throws IOException{
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName,"utf-8"));
        Context context = new Context();
        context.putVar("strategyHandled", datasource);
        if(map!=null){
            for (String key : map.keySet()) {
                context.putVar(key,  map.get(key));
            }
        }

        JxlsHelper.getInstance().processTemplate(is, response.getOutputStream(), context);
    }
    public static void exportExcel(InputStream is, OutputStream os, Map<String, Object> model) throws IOException{
        Context context = new Context();
        if (model != null) {
            for (String key : model.keySet()) {
                context.putVar(key, model.get(key));
            }
        }
        JxlsHelper jxlsHelper = JxlsHelper.getInstance();
        Transformer transformer  = jxlsHelper.createTransformer(is, os);
        JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
        Map<String, Object> funcs = new HashMap<String, Object>();
        jxlsHelper.processTemplate(context, transformer);
    }
    public static void main(String[] args) throws Exception {
        // 模板路径和输出流
        OutputStream os = new FileOutputStream("D:/gandong/project/svn_project/audreport/src/main/resources/jxls/out.xlsx");
        TruckTrafficRep rep = new TruckTrafficRep();
        rep.setNumber(225L);
        rep.setTime("2012-02-05");
        List<TruckTrafficRep> list = new ArrayList<>();
        list.add(rep);
        JxlsUtil.exportExcel(JxlsUtil.class.getResourceAsStream("/jxls/number_of_trucks_passing.xlsx"),os,list);
    }

   /* public static void main(String[] args) throws Exception  {
        // 模板路径和输出流
        OutputStream os = new FileOutputStream("D:/gandong/project/svn_project/audreport/src/main/resources/jxls/车辆入口通行数量每日统计.xlsx");
        TruckTrafficRep rep1 = new TruckTrafficRep();
        rep1.setNumber(1245L);
        rep1.setTime("2020-03-05");
        TruckTrafficRep rep2 = new TruckTrafficRep();
        rep2.setNumber(124445L);
        rep2.setTime("2020-03-06");
        List<TruckTrafficRep> list = new ArrayList<>();
        list.add(rep1);
        list.add(rep2);
        String templatePath = "D:/gandong/project/svn_project/audreport/src/main/resources/jxls/number_of_trucks_passing.xlsx";
        JxlsUtil.exportExcel(templatePath,os,list);
    }*/



}

service类

 
  /**
   * 
   * @param req  请求参数
   * @return
   */
void  exportData(Req req, HttpServletResponse response) throws Exception;

serviceImpl

     private String 模板地址="/jxls/file.xlsx";


    @Override
    public void exportData(Req req, HttpServletResponse response) throws Exception {
      
        List<Rep> list=selectRep(req);
        String name="导出文件名称";
        export( list, response, name, "模板地址");
    }

    /**
     * 导出
     * 
     * @param
     * @param rep
     * @param response
     * @param name
     * @throws Exception
     */
    private void export( List rep, HttpServletResponse response, String name, String url) throws Exception {
        String fileName = URLEncoder.encode(name + ".xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        JxlsUtil.exportExcel(JxlsUtil.class.getResourceAsStream(url), response.getOutputStream(), rep);
    }

 

pom依赖

  <!-- jxls@excel -->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
            <version>2.6.0</version>
        </dependency>
     <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-jexcel</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-reader</artifactId>
            <version>2.0.5</version>
        </dependency>

目录结构

示例模板

百度网盘
链接:https://pan.baidu.com/s/1afqUDNxee8vr4JI-4llkmw 
提取码:aqua 

模板图片(要添加2个批注(修改批注位置:excel->工具栏->审阅->批注))

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jxls是一个开源的Java工具,可以根据Excel模板文件生成Excel文件。jxls支持复杂的Excel模板,可以在模板中包含多个工作表、多个单元格样式、公式等。 下面是使用jxls导出Excel的步骤: 1. 创建Excel模板文件,可以使用Excel或者其他电子表格软件创建,也可以使用jxls提供的Excel模板文件样例。 2. 在Java代码中使用jxls API读取Excel模板文件,并将要填充到Excel文件中的数据传递给jxls。 3. 在Excel模板文件中,使用jxls提供的标记语言标记待填充的单元格或区域。 4. 使用jxls API将填充好数据的Excel文件输出到指定位置。 下面是一个简单的示例: 1. 创建Excel模板文件,假设文件名为template.xlsx,包含两个工作表Sheet1和Sheet2,每个工作表中包含一个表格,表格中包含两个单元格A1和B1,A1单元格中填充姓名,B1单元格中填充年龄。 2. 在Java代码中,使用jxls API读取Excel模板文件,准备要填充到Excel文件中的数据: ```java InputStream is = new FileInputStream(new File("template.xlsx")); OutputStream os = new FileOutputStream(new File("output.xlsx")); Map<String, Object> model = new HashMap<String, Object>(); List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Alice", 25)); persons.add(new Person("Bob", 30)); model.put("persons", persons); ``` 3. 在Excel模板文件中,使用jxls提供的标记语言标记待填充的单元格或区域。在A1单元格中插入${person.name},在B1单元格中插入${person.age},表示在Excel文件中填充persons集合中的每个Person对象的name和age属性。 4. 使用jxls API将填充好数据的Excel文件输出到指定位置: ```java XLSTransformer transformer = new XLSTransformer(); Workbook workbook = transformer.transformXLS(is, model); workbook.write(os); os.flush(); os.close(); is.close(); ``` 这样,就可以根据复杂模板导出Excel文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值