导出带图片的execl

这篇博客介绍了如何在前端使用js-table2excel库将包含图片的表格数据导出为Excel文件。首先,通过npm安装库,然后在需要导出的页面导入并使用该库。导出时,需要定义表格的列结构,包括标题、键和类型(对于图片列,类型设为'image'并指定宽度和高度)。最后,调用table2excel方法,传入列定义、数据列表和文件名即可完成导出。示例代码展示了具体的实现过程。
摘要由CSDN通过智能技术生成

第一步安装:npm install js-table2excel
第二步引入:在需要导出表格的页面引入 import table2excel from 'js-table2excel'
第三步使用 :

没有简化有点麻烦要多写几个表格装图片:

  exportExcel() {

      /** column数据的说明 */

      //1.title为column的标题

      //2.key为column对应的key

      //3.type默认是text,若为图片格式type为image , 并且可以设置图片的宽高

      

      const column = [

        {

          title: "部门名称",

          key: "department_name",

          type: "text",

        },

        {

          title: "封闭次数",

          key: "toll_gate_closed_frequency",

          type: "text",

        },

        {

          title: "最后一次封闭时间",

          key: "close_time",

          type: "text",

        },

        {

          title: "限行/封闭原因",

          key: "tollgate_class_id_name",

          type: "text",

        },

        {

          title: "封闭原因图片",

          key: "picture10",

          type: "image",

          width: 279,

          height: 107,

        }, {

          title: "封闭原因图片2",

          key: "picture11",

          type: "image",

          width: 279,

          height: 107,

        }, {

          title: "封闭原因图片3",

          key: "picture12",

          type: "image",

          width: 279,

          height: 107,

        }, {

          title: "封闭原因图片4",

          key: "picture13",

          type: "image",

          width: 279,

          height: 107,

        }, {

          title: "封闭原因图片5",

          key: "picture14",

          type: "image",

          width: 279,

          height: 107,

        }, {

          title: "封闭原因图片6",

          key: "picture15",

          type: "image",

          width: 279,

          height: 107,

        },

      ];

      //this.newsList是从后端接口中获取数组列表

      //第三个参数是表格名

      table2excel(column, this.list, "收费站封闭数据表.xls");

    },

    getList() {

      let tempData = this.listQuery;

     

      tempData.department_id = tempData.department_id || -1;

      if (tempData.timeArr) {

        if (tempData.timeArr[0]) {

          tempData.StartTime = this._dateFormat(

            tempData.timeArr[0],

            "yyyy/MM/dd HH:mm:ss"

          );

        } else {

          delete tempData.StartTime;

        }

        if (tempData.timeArr[1]) {

          tempData.endTime = this._dateFormat(

            tempData.timeArr[1],

            "yyyy/MM/dd HH:mm:ss"

          );

        } else {

          delete tempData.timeArr;

        }

      }

      this.listLoading = true;

      this.$ajax

        .post("/api/back/Platform.ashx?par=TollGateClosedReportForm", tempData)

        .then((data) => {

          console.log(data);

          this.total = data.data.total;

          const tempData = data.data;

          this.list = tempData.rows;

         

            this.list.forEach(i=>{

              var  imageArr=i.picture1.split(',')

           

              imageArr.forEach((j,ind)=>{

                i[`picture1${ind}`]=j

               

                i.picture1=j

              })

            })

          

          this.listLoading = false;

        });

    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 提供了多种方式来导出图片的 Excel 文件。 一种常用的方法是使用 Apache POI 库来处理 Excel 文件。首先,您需要添加 Apache POI 的依赖项到项目的 pom.xml 文件: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>VERSION</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>VERSION</version> </dependency> ``` 然后,创建一个 Excel 导出的服务类,例如 `ExcelExportService`。在该类,您可以使用 Apache POI 的 API 创建一个新的工作簿,并将数据图片添加到工作簿。例如: ```java public class ExcelExportService { public static void exportWithImage(List<YourDataObject> dataList, String imagePath, String outputFilePath) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 添加数据到工作簿 for (int i = 0; i < dataList.size(); i++) { Row row = sheet.createRow(i); YourDataObject data = dataList.get(i); // 在每一行添加数据,可以通过 data 对象的方法获取数据 // 例如,row.createCell(0).setCellValue(data.getField1()); } // 添加图片到工作簿 InputStream imageInputStream = new FileInputStream(imagePath); byte[] imageBytes = IOUtils.toByteArray(imageInputStream); int pictureIndex = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG); CreationHelper creationHelper = workbook.getCreationHelper(); Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = creationHelper.createClientAnchor(); anchor.setCol1(1); // 图片在第二列 anchor.setRow1(1); // 图片在第二行 Picture picture = drawing.createPicture(anchor, pictureIndex); FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath); workbook.write(fileOutputStream); fileOutputStream.close(); workbook.close(); } } ``` 然后,您可以在您的控制器方法调用这个导出服务类的方法来导出 Excel 文件。例如: ```java @RestController public class ExcelController { @GetMapping("/export") public void exportExcelWithImage(HttpServletResponse response) throws IOException { List<YourDataObject> dataList = getData(); // 获取要导出数据 String imagePath = "path/to/image.png"; // 图片的路径 String outputFilePath = "path/to/output.xlsx"; // 输出的文件路径 ExcelExportService.exportWithImage(dataList, imagePath, outputFilePath); File outputFile = new File(outputFilePath); String fileName = "output.xlsx"; response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); ServletOutputStream outputStream = response.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(outputFile); IOUtils.copy(fileInputStream, outputStream); outputStream.flush(); outputStream.close(); fileInputStream.close(); outputFile.delete(); // 删除临时文件 } } ``` 在上述代码,首先调用 `ExcelExportService.exportWithImage()` 方法来导出 Excel 文件。然后,使用 Spring Boot 的 `ServletResponse` 将 Excel 文件写入 HTTP 响应流,最后通过设置响应的内容类型和文件名,使浏览器下载该文件。 通过上述方法,您可以使用 Spring Boot 导出图片的 Excel 文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值