原 easyexcel(十三):easyexcel导出excel并将其压缩成zip
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1438
@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);
}
}
public interface AgencyInfoService {
void testZip(HttpServletResponse response, AgencyInfoVO av) throws Exception;
}
@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);
}
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());
}
}
2023-06-20 17:11:22 阅读(727)
名师出品,必属精品 https://www.91mszl.com
博主信息