当前位置: 首页 > wzjs >正文

山东网站开发网络公司app手机网站

山东网站开发网络公司,app手机网站,制作网页第一件事就是选定一种,网站建设报告书最近项目要实现一个功能&#xff0c;就是在导出报表的时候 &#xff0c;要把每条数据的所有图片都要打包成zip附件在excel里一起导出。 1. 添加依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>…

 最近项目要实现一个功能,就是在导出报表的时候 ,要把每条数据的所有图片都要打包成zip附件在excel里一起导出。

1. 添加依赖

           <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.ehcache</groupId><artifactId>ehcache</artifactId><version>3.9.11</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.16.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.36</version></dependency>

2. 实现代码

以下是一个完整的Java实现:

2.1 把图片打包成zip

 /*** 把图片和视频打包成一个zip文件** @param zipFilePath* @param imagePaths* @throws IOException*/public File createZipFile(String zipFilePath, List<String> imagePaths) throws IOException, InvalidFormatException {File zipFile = new File(zipFilePath);if (!zipFile.exists()) {if (!zipFile.getParentFile().exists()) {zipFile.getParentFile().mkdirs();}zipFile.createNewFile();}try (FileOutputStream fos = new FileOutputStream(zipFilePath);ZipOutputStream zos = new ZipOutputStream(fos)) {for (String imagePath : imagePaths) {File fileToZip = new File(imagePath);FileInputStream fis = new FileInputStream(fileToZip);ZipEntry zipEntry = new ZipEntry(fileToZip.getName());zos.putNextEntry(zipEntry);byte[] bytes = new byte[1024];int length;while ((length = fis.read(bytes)) >= 0) {zos.write(bytes, 0, length);}fis.close();}}return zipFile;}

 2.2 把Zip文件添加到excel附件,并生成缩略图

package com.bdspace.gateway.util;import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;/*** poi打包图片成zip文件添加到excel附件** @author lyl* @version v1.0* @since 2025/4/24*/
public class PoiAttachUtil {/*** 创建附件* @param workbook* @param sheet* @param row* @param zipFile* @throws IOException*/public void createAttch(Workbook workbook, Sheet sheet, int row, String zipFile) throws IOException {//只有HSSFWorkbook才能使用OLE对象,并且poi需要在4.0之上// 创建工作簿和工作表对象// 读取需要添加的文件,如果有需要添加多个文件的需求,可以循环表格来添加File pdfFile = new File(zipFile);FileInputStream fis = new FileInputStream(pdfFile);byte[] pdfBytes = new byte[(int) pdfFile.length()];fis.read(pdfBytes);fis.close();// 获取文件系统视图FileSystemView view = FileSystemView.getFileSystemView();Icon systemIcon = view.getSystemIcon(pdfFile);// 将 Icon 对象转换为 Image 对象Image image = iconToImage(systemIcon);// 将 Image 对象转换为字节数组byte[] imageBytes = imageToByteArray(image);//将文件的图标添加进入到Excel文件内int iconid = workbook.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);// 在工作表中创建OLE对象,就是将文件插入到Excel文件中int pdfIdx = workbook.addOlePackage(pdfBytes, pdfFile.getName(), row + pdfFile.getName(), pdfFile.getName());// 创建画布和锚点Drawing<?> drawing = sheet.createDrawingPatriarch();ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 6, row, 7, row + 1);//这里的参数后续根据传过来的信息来变化。row,colanchor.setAnchorType(HSSFClientAnchor.AnchorType.MOVE_AND_RESIZE);drawing.createObjectData(anchor, pdfIdx, iconid);}// 将 Icon 对象转换为 Image 对象private Image iconToImage(Icon icon) {if (icon instanceof ImageIcon) {return ((ImageIcon) icon).getImage();} else {int width = icon.getIconWidth();int height = icon.getIconHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);Graphics graphics = image.createGraphics();icon.paintIcon(null, graphics, 0, 0);graphics.dispose();return image;}}// 将 Image 对象转换为字节数组private byte[] imageToByteArray(Image image) throws IOException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();ImageIO.write(imageToBufferedImage(image), "png", byteArrayOutputStream);return byteArrayOutputStream.toByteArray();}// 将 Image 对象转换为 BufferedImage 对象private BufferedImage imageToBufferedImage(Image image) {if (image instanceof BufferedImage) {return (BufferedImage) image;}BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);Graphics2D graphics2D = bufferedImage.createGraphics();graphics2D.drawImage(image, 0, 0, null);graphics2D.dispose();return bufferedImage;}public static void main(String[] args) {PoiAttachUtil p = new PoiAttachUtil();Workbook workbook = new HSSFWorkbook();Sheet sheet = workbook.createSheet("Sheet1");Row row = sheet.createRow(0);//加载附件String zipFilePahth = "D:\\testaa\\image1.zip";try {p.createAttch(workbook, sheet, 1, zipFilePahth);try (OutputStream fileOut = new FileOutputStream("D:\\testaa\\事件测试2.xls")) {workbook.write(fileOut);}// 关闭工作簿workbook.close();} catch (IOException e) {throw new RuntimeException(e);}}}

3. 在Spring MVC控制器中使用

 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;@Controller
public class ExportController {@GetMapping("/export/employees")public void exportEmployees(HttpServletResponse response) throws IOException {PoiAttachUtil p = new PoiAttachUtil();Workbook workbook = new HSSFWorkbook();Sheet sheet = workbook.createSheet("Sheet1");Row row = sheet.createRow(0);String zipFilePath="d:\\temp\test.zip";//添加图片路径。或从数据库拿List<String> files=new ArrayList<>();//加载附件String zipFilePahth =  p.createZipFile(zipFilePath, files);try {p.createAttch(workbook, sheet, 1, zipFilePahth);try (OutputStream fileOut = new FileOutputStream(response)) {workbook.write(fileOut);}// 关闭工作簿workbook.close();} catch (IOException e) {throw new RuntimeException(e);}}
}

4. 功能说明

  1. Excel创建:使用Apache POI创建XSSFWorkbook(支持.xlsx格式)

  2. 数据填充:将数据填充到工作表中,自动处理不同类型的数据

  3. ZIP打包:把对应路径下的文件打包成ZIP

  4. HTTP响应:设置正确的响应头,将ZIP文件发送给客户端

5. 高级选项

  • 多Sheet支持:可以在一个Excel文件中创建多个工作表

  • 多文件打包:可以在ZIP中包含多个Excel文件或其他文件

  • 样式设置:可以为单元格添加样式(字体、颜色、边框等)

  • 大数据量处理:对于大数据量,可以使用SXSSFWorkbook来减少内存消耗

 


文章转载自:

http://q7hLgh2c.sgnjg.cn
http://fdlZCFzf.sgnjg.cn
http://caPqUu5y.sgnjg.cn
http://3PGtFhIl.sgnjg.cn
http://rKxZajgv.sgnjg.cn
http://DA56kTK5.sgnjg.cn
http://LWCdldyU.sgnjg.cn
http://LxgQISNX.sgnjg.cn
http://QYahCX2k.sgnjg.cn
http://6eGozPU2.sgnjg.cn
http://C8BQHKAx.sgnjg.cn
http://lX0hHoXA.sgnjg.cn
http://StMJ09mw.sgnjg.cn
http://aWI7LvWY.sgnjg.cn
http://q4PVwFRl.sgnjg.cn
http://uN7hAR8g.sgnjg.cn
http://Y4NPibYV.sgnjg.cn
http://L6zioWxZ.sgnjg.cn
http://EKZBQjrS.sgnjg.cn
http://WaozObNB.sgnjg.cn
http://qgCiJb1l.sgnjg.cn
http://4LlGx9dn.sgnjg.cn
http://xzxhyPo5.sgnjg.cn
http://bN5Rzrzr.sgnjg.cn
http://qVOEgyRo.sgnjg.cn
http://QFBD6ijG.sgnjg.cn
http://sNBW6CSn.sgnjg.cn
http://dyfcXRbH.sgnjg.cn
http://TYCLtj0N.sgnjg.cn
http://wM40Y4vb.sgnjg.cn
http://www.dtcms.com/wzjs/708257.html

相关文章:

  • 山东网站备案拍照营销型网站管理方案
  • 从写代码到网站运行不了了网站嵌入视频代码
  • 自动化设备东莞网站建设网站搭建学什么软件
  • 广东省建设厅网站ps怎么做网站一寸的照片
  • 什么是速成网站网站建设定金合同范本
  • 网站备案接入商名称wordpress托管在哪里
  • 有做公司网站的吗适合大学生浏览的网站
  • 宁波商城网站建设做网站 除了域名
  • 天津做公司网站wordpress超cpu
  • 丹阳市网站制作网络营销外包收费吗
  • 河南网站建设哪里有上海seo网站
  • 旅游网站建设的方法怎么能创建自己的网站
  • 烟台工程建设信息网站网络公司注册多少钱
  • 网站被清空了怎么办做文创的网站
  • 建站宝盒破解版企业网站、电子期刊属于企业文化传播载体中的( )。
  • vs2012 建网站网站设计培训课程
  • 营销公司网站温州最大的外贸公司
  • 佛山茂名网站建设网页单机游戏
  • 成都网站seo制作短视频的app哪个好
  • 12306网站建设超30亿学做网站快吗
  • 国外房屋设计网站外贸开源网站
  • 网站开发工作室营业执照怎么在服务器上装WordPress
  • 网站开发技术职责企业建设项目哪个网站可以查
  • 湖南智能网站建设公司怎么查设计的logo侵不侵权
  • 做英文网站有哪些wordpress 快递插件
  • 网站建设网站建设教程视觉设计公司名字
  • 网站建设什么价格百度网站链接提交
  • 唯品会网站建设数据安全分析微信营销策划方案范文
  • 做网站用什么字体比较好开发公司工程部岗位职责
  • 谷歌网站排名江苏省城乡建设部网站首页