EasyExcel 实现写入多个sheet数据进excel模板并下载
生活随笔
收集整理的這篇文章主要介紹了
EasyExcel 实现写入多个sheet数据进excel模板并下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 說明
說明
場景說明:對數據庫或者其他數據源讀取獲取到數據,需要寫入到excel完成下載功能,其中一個sheet是固定模板,只需要填充值,另一個sheet是動態的表頭和數據需要填充。模板如下圖,模板提前放在項目里,免得有問題。
在這個案例前還寫過一個批量excel打包成zip包下載的,這里的案例和代碼注釋說明比較清晰,重復的在這里不再多說,建議先看完上一個案例實現再來看這個實現,避免踩坑出不來,跳轉鏈接:EasyExcel 實現 批量生成多sheet多Excel打包zip下載
不多說 直接上代碼:
@GetMapping("/upload-xls")public void uploadXls() {//初始化excel的數據體List<Map<String, String>> maps = new LinkedList<>();for (int i = 6; i < 13; i++) {HashMap<String, String> map = new HashMap<>();map.put("name", "小" + i);map.put("age", "" + i);maps.add(map);}ExcelWriter excelWriter;try {// 文件名(這里URLEncoder.encode可以防止中文亂碼)String fileName = URLEncoder.encode("四(二)班-數據導出", "UTF-8");// 設置返回文件信息response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");} catch (Exception e) {log.info(e.getMessage(), e);}//獲取模板文件,模板文件需放在 resource下String templateExcelFileName = "upload.xlsx";InputStream templateExcelInputStream = this.getClass().getClassLoader().getResourceAsStream(templateExcelFileName);if (null == templateExcelInputStream) {log.error("模板文件不存在!");}ByteArrayOutputStream outputStream = null;ByteArrayOutputStream bos = cloneInputStream(templateExcelInputStream);//動態sheet1 Excel表頭List<String> heads = Arrays.asList("name", "age");List<List<String>> excelHeaders = heads.stream().map(Collections::singletonList).collect(Collectors.toList());//動態sheet1 Excel數據List<List<String>> excelDatas = excelDatas(heads, maps);try {InputStream copyInputStream = new ByteArrayInputStream(bos.toByteArray());outputStream = new ByteArrayOutputStream();excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(copyInputStream).excelType(ExcelTypeEnum.XLSX).build();WriteSheet writeSheet = EasyExcel.writerSheet(0, "學校信息").build();Map<String, Object> map = new HashMap<String, Object>();map.put("schoolName", "新民小學");map.put("className", "四(二)班");map.put("count", maps.size());map.put("uploadDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));excelWriter.fill(map, writeSheet);//動態填充表頭和內容WriteSheet attrSheet = EasyExcel.writerSheet(1, "班級學生信息").build();attrSheet.setHead(excelHeaders);excelWriter.write(excelDatas, attrSheet);excelWriter.finish();outputStream.flush();outputStream.close();copyInputStream.close();} catch (Exception e) {log.error(e.getMessage(), e);} finally {if (null != outputStream) {try {outputStream.flush();outputStream.close();} catch (IOException e) {log.error("導出 excel 時關閉 outputStream 出現異常", e);}}}}
就先說到這 \color{#008B8B}{ 就先說到這} 就先說到這
在下 A p o l l o \color{#008B8B}{在下Apollo} 在下Apollo
一個愛分享 J a v a 、生活的小人物, \color{#008B8B}{一個愛分享Java、生活的小人物,} 一個愛分享Java、生活的小人物,
咱們來日方長,有緣江湖再見,告辭! \color{#008B8B}{咱們來日方長,有緣江湖再見,告辭!} 咱們來日方長,有緣江湖再見,告辭!
總結
以上是生活随笔為你收集整理的EasyExcel 实现写入多个sheet数据进excel模板并下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 列表标题一/两行垂直居中展示
- 下一篇: 抓取网站数据入库详解,附图文