• Java 实现前端数据的导出操作


    前端

    1. class="export" type="primary" icon="el-icon-download" @click="exportData()">导出
    2. exportData() {
    3. //data 操作data 变成后端需要的格式
    4. let data = {
    5. capacityVos: resultVo
    6. }
    7. this.$confirm("是否确认导出曲线信息?", "警告", {
    8. confirmButtonText: "确定",
    9. cancelButtonText: "取消",
    10. type: "warning",
    11. }).then(response => {
    12. exportExcelData(data).then(res => {
    13. if (res.code == 200) {
    14. this.jnpf.downloadFile(res.data.url)
    15. }
    16. })
    17. })
    18. }

    前端跳后端

    1. // 导出
    2. export function exportExcelData(data) {
    3. return request({
    4. url: define.api + '/user/export',
    5. method: 'post',
    6. data:data,
    7. });
    8. }

    模板excel

    后端接口

    1. //xAxixAndData 导出的数据
    2. @ApiOperation("导出Excel")
    3. @PostMapping("/export")
    4. public ActionResult export(@RequestBody XAxixAndData xAxixAndData){
    5. Map map = new HashMap<>();
    6. List> vos = hisCapacityService.getByOne(xAxixAndData);
    7. List> names = hisCapacityService.getDataName(xAxixAndData);
    8. map.put("data", vos);
    9. map.put("names", names);
    10. //1.获取导出模板地址Curve
    11. ClassPathResource classPathResource = new ClassPathResource("static/CurveModel.xlsx");
    12. String path = classPathResource.getPath();
    13. TemplateExportParams templateExportParams = new TemplateExportParams(path);
    14. // 2.执行excel导出
    15. Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
    16. //3.获取到第0个sheet
    17. Sheet sheet = workbook.getSheetAt(0);
    18. //4.创建标题行 数据有几列--标题合并居中有几列
    19. List> names1 = (List) map.get("names");
    20. int num = names1.size();
    21. Row titleRow = sheet.createRow(0);
    22. Cell titleCell = titleRow.createCell(0);
    23. if(ObjectUtil.isNotNull(xAxixAndData.getVos())){
    24. //风电
    25. titleCell.setCellValue("风电出力曲线");
    26. }else if(ObjectUtil.isNotNull(xAxixAndData.getCapacityVos())){
    27. //光伏
    28. titleCell.setCellValue("光伏出力曲线");
    29. }else if(ObjectUtil.isNotNull(xAxixAndData.getCurveVos())){
    30. //曲线目录
    31. titleCell.setCellValue(xAxixAndData.getCurveVos().get(0).getName());
    32. }
    33. CellRangeAddress range = new CellRangeAddress(
    34. 0, // first row (0-based)
    35. 0, // last row
    36. 0, // first column (0-based)
    37. num // last column
    38. );
    39. sheet.addMergedRegion(range);
    40. Font font = workbook.createFont();
    41. Font font2 = workbook.createFont();
    42. font.setFontHeightInPoints((short) 14); // 设置字体大小
    43. font.setBold(true); // 设置加粗
    44. font2.setBold(true);
    45. CellStyle style = workbook.createCellStyle();
    46. CellStyle style2 = workbook.createCellStyle();
    47. style.setAlignment(HorizontalAlignment.CENTER);// 设置单元格格式为居中
    48. style2.setAlignment(HorizontalAlignment.CENTER);
    49. style.setFont(font);
    50. style2.setFont(font2);
    51. titleCell.setCellStyle(style);
    52. //5.创建列名所在的行
    53. Row columnRow = sheet.createRow(1);
    54. Cell columnRowCell = columnRow.createCell(0);
    55. columnRowCell.setCellValue("时刻");
    56. columnRowCell.setCellStyle(style2);
    57. for (int i = 1; i <= names1.size(); i++) {
    58. Cell columnRowCell1 = columnRow.createCell(i);
    59. columnRowCell1.setCellValue(names1.get(i-1).get("name"));
    60. columnRowCell1.setCellStyle(style2);
    61. }
    62. //6.循环row设置行高自适应
    63. for(int i = 0;i <= sheet.getLastRowNum();i ++){
    64. XSSFRow xssfRow = (XSSFRow) sheet.getRow(i);
    65. CTRow ctRow = xssfRow.getCTRow();
    66. ctRow.setCustomHeight(false);
    67. }
    68. //7.设置列宽
    69. List> data = (List) map.get("data");
    70. for (int i = 1; i < data.size(); i++) {
    71. sheet.setColumnWidth(i,10 * 256);
    72. }
    73. //8.重命名第0个sheet名称
    74. workbook.setSheetName(0, "曲线数据");
    75. DownloadVO vo = this.creatModelExcel(fileApi.getPath(FileTypeEnum.TEMPORARY), userInfo, workbook);
    76. return ActionResult.success(vo);
    77. }

    后端--导出表格

    1. /***
    2. * 导出表格
    3. * @Author lixi
    4. * @date 2023-09-14
    5. * @return jnpf.base.vo.DownloadVO
    6. **/
    7. public DownloadVO creatModelExcel(String path, UserInfo userInfo, Workbook workbook) {
    8. DownloadVO vo = DownloadVO.builder().build();
    9. String sheetName = "名字";
    10. ExportParams exportParams = new ExportParams(null, sheetName);
    11. exportParams.setType(ExcelType.XSSF);
    12. try {
    13. String name = sheetName + ".xlsx";
    14. @Cleanup ByteArrayOutputStream output = new ByteArrayOutputStream();
    15. workbook.write(output);
    16. byte[] barray = output.toByteArray();
    17. //将Excel文件二进制转为inputstream对象
    18. @Cleanup InputStream ins = new ByteArrayInputStream(barray);
    19. //上传文件
    20. GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).uploadFile(path.toLowerCase(), FileTypeEnum.TEMPORARY, name, ins);
    21. vo.setName(name);
    22. vo.setUrl(UploaderUtil.uploaderFile(userInfo.getId() + "#" + name + "#" + "Temporary"));
    23. } catch (Exception e) {
    24. log.error("信息导出Excel错误:{}", e);
    25. }
    26. return vo;
    27. }

    前端将导出的结果显示在网页上

    1. exportExcelData(data).then(res => {
    2. if (res.code == 200) {
    3. //调用this.jnpf.downloadFile方法即可
    4. this.jnpf.downloadFile(res.data.url)
    5. }
    6. })

  • 相关阅读:
    重用Playbook
    jwt_拦截器_校验token
    自然语言处理 | WordNet
    Stable Diffusion 模型下载:A-Zovya RPG Artist Tools(RPG 大师工具箱)
    [NepCTF2022] 复现
    一张图理解MITRE ATT&CK框架
    量化投资 日周月报 2024-06-28
    Vue中的混入(mixin)
    超硬核java工程师秋招,为了BAT的研发offer,做了那些准备?
    STM32速成笔记—SPI通信
  • 原文地址:https://blog.csdn.net/xy58451921/article/details/133386216