当前位置:   article > 正文

springboot post请求 实现数据分批导出到excel_easyexcel可以用post请求导出吗

easyexcel可以用post请求导出吗

依赖:

  1. api group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.1.4.RELEASE'
  2. api group: 'com.alibaba', name: 'easyexcel', version: '1.1.2-beta5'

 

Controller:
  1. @RequestMapping(path ="export")
  2. public void fileExport(@RequestBody PageFilterSearchModel searchModel, HttpServletResponse response) throws IOException {
  3. //解析元数据
  4. PageResultModel pageResultModel =new PageResultModel<>((Collection) dispatchMetadataServer.doService(searchModel));
  5. //生成excel
  6. fileExportAdapter.prase(pageResultModel,response);
  7. }
  8. @RequestMapping(path ="index")
  9. public String fileExport1(@RequestHeader(value = "X-XSK-Token", defaultValue = "4242342") String token) {
  10. return "1.html";
  11. }

上面的token 时项目请求拦截器里需要带的

FileExportAdapter:
  1. package com.softium.xsk.property.export;
  2. import com.softium.xsk.cooperated.model.PageResultModel;
  3. import javax.servlet.http.HttpServletResponse;
  4. import java.io.IOException;
  5. public interface FileExportAdapter {
  6. void prase(PageResultModel pageResultModel, HttpServletResponse response) throws IOException;
  7. }

导出excel:

  1. package com.softium.xsk.property.export;
  2. import com.alibaba.excel.metadata.Table;
  3. import com.softium.xsk.cooperated.model.PageResultModel;
  4. import javax.servlet.http.HttpServletResponse;
  5. import java.io.IOException;
  6. import java.util.*;
  7. public class ExcelExport implements FileExportAdapter {
  8. @Override
  9. public void prase(PageResultModel pageResultModel, HttpServletResponse response) throws IOException {
  10. //待处理结果集
  11. List<Map<String,Object>> list = (List) pageResultModel.getList();
  12. //excel每一列标题
  13. List<List<String>> titles = new ArrayList<List<String>>();
  14. List<String> cloum = new ArrayList();
  15. // 设置标题
  16. Table table = new Table(1);
  17. for (int i = 0; i < list.size(); i++) {
  18. Map<String, Object> map = list.get(i);
  19. for (Map.Entry<String, Object> entry : map.entrySet()) {
  20. String key = entry.getKey();
  21. titles.add(Arrays.asList(key));
  22. cloum.add(key);
  23. }
  24. break;
  25. }
  26. table.setHead(titles);
  27. List<List<String>> exportList = new ArrayList<>();
  28. List<String> valueList = null;
  29. //设置每行每列的值
  30. for (int i = 0; i < list.size(); i++) {
  31. valueList = new ArrayList<>();
  32. for(int j=0;j<cloum.size();j++){
  33. Map<String, Object> stringObjectMap = list.get(i);
  34. Object o = stringObjectMap.get(cloum.get(j));
  35. if(o == null){
  36. valueList.add("");
  37. }else{
  38. valueList.add(o.toString());
  39. }
  40. }
  41. exportList.add(valueList);
  42. }
  43. try {
  44. ExcelUtil.writeExcel(response, exportList, table);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

excel工具类:实现分批导出

  1. package com.softium.xsk.property.export;
  2. import com.alibaba.excel.ExcelWriter;
  3. import com.alibaba.excel.metadata.BaseRowModel;
  4. import com.alibaba.excel.metadata.Sheet;
  5. import com.alibaba.excel.metadata.Table;
  6. import com.alibaba.excel.support.ExcelTypeEnum;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.net.URLEncoder;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. public class ExcelUtil {
  14. //每次输出行数
  15. private static final int PAGESIZE=500;
  16. //excel sheet名
  17. private static String SHEETNAME="sheet1";
  18. //导出的文件名
  19. private static String FILENAME="人员信息";
  20. /**
  21. * 导出 Excel :一个 sheet,带表头
  22. * @param response HttpServletResponse
  23. * @param list 数据 list,每个元素为一个 BaseRowModel
  24. */
  25. public static void writeExcel(HttpServletResponse response, List<List<String>> list, Table table)throws Exception {
  26. ExcelWriter writer = new ExcelWriter(getOutputStream(response), ExcelTypeEnum.XLSX);
  27. Sheet sheet = new Sheet(1, 0);
  28. sheet.setSheetName(SHEETNAME);
  29. for(int i=0;i<2000;i++){
  30. List<String> a = new ArrayList<>();
  31. for(int j=0;j<5;j++) {
  32. a.add(String.valueOf(j));
  33. }
  34. list.add(a);
  35. }
  36. if(list.size()> PAGESIZE){
  37. averageAssign(list,writer, sheet,table);
  38. }else{
  39. writer.write0(list, sheet,table);
  40. }
  41. writer.finish();
  42. }
  43. /**
  44. * 导出文件时为Writer生成OutputStream
  45. *
  46. * @param response
  47. * @return
  48. */
  49. private static OutputStream getOutputStream(HttpServletResponse response) throws Exception {
  50. try {
  51. //response.setContentType("application/vnd.ms-excel"); xls格式
  52. //xlsx格式
  53. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  54. response.setCharacterEncoding("utf8");
  55. //response.addHeader("Content-Disposition", "attachment;filename=fileName" + ".xlsx");
  56. response.setHeader("fileName", ""+FILENAME+".xlsx");
  57. response.setHeader("Pragma", "public");
  58. response.setHeader("Cache-Control", "no-store");
  59. response.addHeader("Cache-Control", "max-age=0");
  60. return response.getOutputStream();
  61. } catch (IOException e) {
  62. throw new Exception("导出excel表格失败!", e);
  63. }
  64. }
  65. public static void averageAssign(List<List<String>> list,ExcelWriter writer,Sheet sheet, Table table) {
  66. List<List<String>> targetList;
  67. int start = 0;
  68. int end = PAGESIZE;
  69. while (end > start){
  70. if (end >= list.size()){
  71. end = list.size();
  72. targetList = list.subList(start, end);
  73. writer.write0(targetList, sheet,table);
  74. break;
  75. }else{
  76. targetList = list.subList(start, end);
  77. writer.write0(targetList, sheet,table);
  78. start = end+1;
  79. end = start+PAGESIZE;
  80. }
  81. }
  82. }
  83. }

index页面:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. </body>
  10. <script type="text/javascript">
  11. var data = {
  12. "filterKey":"staff-page-filter",
  13. "listKey":"staff-page-export",
  14. "params":{
  15. }
  16. };
  17. var xhr = new XMLHttpRequest();
  18. xhr.open('post', 'http://localhost:8080/export', true);
  19. xhr.responseType = 'blob';
  20. xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
  21. xhr.onload = function () {
  22. if (this.status == 200) {
  23. var blob = this.response;
  24. var a = document.createElement('a');
  25. var url = window.URL.createObjectURL(blob);
  26. a.href = url;
  27. //设置文件名称
  28. a.download = 'data.xlsx';
  29. a.click();
  30. }
  31. }
  32. xhr.send(JSON.stringify(
  33. data
  34. ));
  35. </script>
  36. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/536803
推荐阅读
相关标签
  

闽ICP备14008679号