java导出数据到excel模板_springboot+jxls 根据Excel模板 填写数据并导出

项目结构
70dcf3f6ad3a1324b04fc945d8c58bb2.png
pom.xml
                net.sf.jxls            jxls-core            1.0.6compile
学生信息表模板:
eb100769b9c41fb0897cde65a64ab968.png
ExcelUtiles
package cn.bdqn.utils;import net.sf.jxls.transformer.XLSTransformer;import org.apache.poi.ss.usermodel.Workbook;import org.springframework.util.ResourceUtils;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.util.Map;/** * @ProjectName: Student * @Author: huat * @Date: 2020/5/7 8:53 * @Version: 1.0 */public class ExcelUtiles {    /**     * 输出表格     * @param map 表格中数据     * @param response 响应     * @param excelName 表格名称     * @param excelPath 表格模板保存的路径     */    public static void outExcel(Map map,HttpServletResponse response,String excelName,String excelPath){        File file=null;        try {            file= ResourceUtils.getFile(excelPath);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        //配置下载路径        String path = "/download/";        createDir(new File(path));        //根据模板生成新的excel        File excelFile = createNewFile(map, file, path,excelName);        //浏览器端下载文件        try {            downloadFile(response, excelFile,excelName);        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        //删除服务器生成文件        deleteFile(excelFile);    }    /**     * 根据excel模板生成新的excel     * @param beans 表格中的数据     * @param file 文件     * @param path 生成文件的位置     * @param excelName 文件名称     * @return     */    private static File createNewFile(Map beans, File file, String path,String excelName) {        XLSTransformer transformer = new XLSTransformer();        File newFile = new File(path + excelName+".xlsx");        try (InputStream in = new BufferedInputStream(new FileInputStream(file));             OutputStream out = new FileOutputStream(newFile)) {            Workbook workbook = transformer.transformXLS(in, beans);            workbook.write(out);            out.flush();            return newFile;        } catch (Exception e) {            System.out.println(e.getMessage());        }        return newFile;    }    /**     * 将服务器新生成的excel从浏览器下载     * @param response 响应     * @param excelFile 表格文件     * @param excelName 表格名称     * @throws UnsupportedEncodingException     */    private static void downloadFile(HttpServletResponse response, File excelFile,String excelName) throws UnsupportedEncodingException {        /* 设置文件头:最后一个参数是设置下载文件名 */        response.setHeader("Content-type","application/vnd.ms-excel");        // 解决导出文件名中文乱码        response.setCharacterEncoding("UTF-8");        response.setHeader("Content-Disposition","attachment;filename="+new String(excelName.getBytes("UTF-8"),"ISO-8859-1")+".xlsx");        try (                InputStream ins = new FileInputStream(excelFile);                OutputStream os = response.getOutputStream()        ) {            byte[] b = new byte[1024];            int len;            while ((len = ins.read(b)) > 0) {                os.write(b, 0, len);            }        } catch (IOException ioe) {            ioe.printStackTrace();        }    }    /**     * 浏览器下载完成之后删除服务器生成的文件     * 也可以设置定时任务去删除服务器文件     *     * @param excelFile     */    private static void deleteFile(File excelFile) {        excelFile.delete();    }    //如果目录不存在创建目录 存在则不创建    private static void createDir(File file) {        if (!file.exists()) {            file.mkdirs();        }    }}
Controller
 @RequestMapping("downExcel")    public void downExecl(HttpServletResponse response){        Map map=new HashMap();        map.put("name","学生信息表");       //获取学生信息        map.put("studentList",studentService.getStudent());       //"classpath:static/excel/学生表格.xlsx"   表格模板保存路径,classpath:代表resources路径        ExcelUtiles.outExcel(map,response,"学生信息表","classpath:static/excel/学生表格.xlsx");    }

原文:https://my.oschina.net/u/3535099/blog/4268971

作者:冥焱

  • 1
    点赞
  • 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、付费专栏及课程。

余额充值