91名师指路-头部
91名师指路

easyexcel(十三):easyexcel导出excel并将其压缩成zip

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

需求:产品需要把excel导出,并将相关的图片一起导出,并将excel和图片打成一个压缩包,然后一并导出。


1)controller

@Slf4j
@RestController
@RequestMapping("/project/bcc")
public class AgencyInfoController {

@Autowired
private AgencyInfoService agencyInfoService;

@GetMapping("/test")
public void test(HttpServletResponse response, @Validated AgencyInfoVO av) throws Exception {
agencyInfoService.testZip(response, av);
}


}


2)service

public interface AgencyInfoService {

void testZip(HttpServletResponse response, AgencyInfoVO av) throws Exception;

}


3)serviceImpl

@Slf4j
@Service
public class AgencyInfoServiceImpl implements AgencyInfoService {

@Autowired
private GtmBccAgencyTargetMapper agencyTargetMapper;

@Override
public void testZip(HttpServletResponse response, AgencyInfoVO av) throws Exception {
// 1 查询数据
List<AgencyTargetDto> agencyTargetList = agencyTargetMapper.selectAgencyTarget(av);

List<List<String>> agencyHeadList=new ArrayList<>();
agencyHeadList.add(Lists.newArrayList("编码"));
agencyHeadList.add(Lists.newArrayList("名称"));

// 2 导出
String ExcelFileName= DateUtils.dateToYymmddhhmmss()+".xlsx";
String zipName="明细及截图-"+DateUtils.dateToYymmddhhmmss()+".zip";
zipName = URLEncoder.encode(zipName, "utf-8"); // 这里URLEncoder.encode可以防止中文乱码
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=\"" + zipName + "\"");
OutputStream os=response.getOutputStream();
ByteArrayOutputStream out=new ByteArrayOutputStream();
ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcel.write(out).build();
WriteSheet topWriteSheet = EasyExcel.writerSheet(1, "区域")
.head(agencyHeadList)
.registerWriteHandler(new CustomizeColumnWidth())
.build(); // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
excelWriter.write(agencyTargetList, topWriteSheet);

WriteSheet bottomWriteSheet = EasyExcel.writerSheet(2, "乡镇")
.head(agencyHeadList)
.registerWriteHandler(new CustomizeColumnWidth())
.build(); // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
excelWriter.write(agencyTargetList, bottomWriteSheet);
} finally {
if (excelWriter != null) { // 关闭流
excelWriter.finish();
}
}

// 这里模拟获取到了图片路径,实际中应该是一个list
String imageUrl="https://cdn.91mszl.com/cdn/images/logo.png";

// 3 压缩
InputStream[] ins = new InputStream[2]; // 有多少个文件就定义多大的长度
ins[0] = ImagesUtils.outputStream2InputStream(out);
ins[1] = ImagesUtils.stringToInputStream(imageUrl);
String[] paths = {ExcelFileName, "logo.png"}; // 所有需要压缩的文件都需要放到paths数组中, 注意这里只放文件名称不要前缀,如:https://cdn.91mszl.com/cdn/images
ZipUtil.zip(os, paths, ins);
}


4)ImagesUtils工具类

package com.gaia.common.util;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

public class ImagesUtils {

/**
* 功能:将图片转为输入流
* @Author: zxb
* @Date: 2023-06-19 17:24:50
*/
public static InputStream stringToInputStream(String image){
URL urlfile = null;
try {
urlfile = new URL(image);
} catch (MalformedURLException e) {
e.printStackTrace();
}

InputStream inStream = null;
try {
inStream = urlfile.openStream();
} catch (IOException e) {
e.printStackTrace();
}

return inStream;
}

/**
* 输出流转输入流;数据量过大请使用其他方法
*/
public static ByteArrayInputStream outputStream2InputStream(OutputStream out) {
Objects.requireNonNull(out);
ByteArrayOutputStream bos;
bos = (ByteArrayOutputStream) out;
return new ByteArrayInputStream(bos.toByteArray());
}


}


5)请求接口,然后效果图如下







2023-06-20 17:11:22     阅读(727)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部