java导出数据到excel模板_springboot+jxls 根据Excel模板 填写数据并导出
生活随笔
收集整理的這篇文章主要介紹了
java导出数据到excel模板_springboot+jxls 根据Excel模板 填写数据并导出
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
項(xiàng)目結(jié)構(gòu)
pom.xml
net.sf.jxls jxls-core 1.0.6compile學(xué)生信息表模板:
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 表格中數(shù)據(jù) * @param response 響應(yīng) * @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)); //根據(jù)模板生成新的excel File excelFile = createNewFile(map, file, path,excelName); //瀏覽器端下載文件 try { downloadFile(response, excelFile,excelName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //刪除服務(wù)器生成文件 deleteFile(excelFile); } /** * 根據(jù)excel模板生成新的excel * @param beans 表格中的數(shù)據(jù) * @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; } /** * 將服務(wù)器新生成的excel從瀏覽器下載 * @param response 響應(yīng) * @param excelFile 表格文件 * @param excelName 表格名稱 * @throws UnsupportedEncodingException */ private static void downloadFile(HttpServletResponse response, File excelFile,String excelName) throws UnsupportedEncodingException { /* 設(shè)置文件頭:最后一個(gè)參數(shù)是設(shè)置下載文件名 */ response.setHeader("Content-type","application/vnd.ms-excel"); // 解決導(dǎo)出文件名中文亂碼 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(); } } /** * 瀏覽器下載完成之后刪除服務(wù)器生成的文件 * 也可以設(shè)置定時(shí)任務(wù)去刪除服務(wù)器文件 * * @param excelFile */ private static void deleteFile(File excelFile) { excelFile.delete(); } //如果目錄不存在創(chuàng)建目錄 存在則不創(chuàng)建 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","學(xué)生信息表"); //獲取學(xué)生信息 map.put("studentList",studentService.getStudent()); //"classpath:static/excel/學(xué)生表格.xlsx" 表格模板保存路徑,classpath:代表resources路徑 ExcelUtiles.outExcel(map,response,"學(xué)生信息表","classpath:static/excel/學(xué)生表格.xlsx"); }原文:https://my.oschina.net/u/3535099/blog/4268971
作者:冥焱
總結(jié)
以上是生活随笔為你收集整理的java导出数据到excel模板_springboot+jxls 根据Excel模板 填写数据并导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MyBatisPlus自动生成代码spr
- 下一篇: [leetcode] Restore I