前端操作
请求设置
downloadFiles(financeExcelDto) {
return http.post({
// 请求路径 修改为真是的路径
url: '/crm/standardFile/downloadFiles',
data:financeExcelDto,
//返回类型要为 biob
responseType: 'blob'
})
}
对接口返回的数据进行操作
下载后端返回的文件
let res= await standardFileApi.downloadFiles(this.form)
const blob = new Blob([res.data])
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob)
link.target = '_blank'
link.download = decodeURI('线下财务报表模板.zip')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
后端操作
引入依赖
<!--excel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.0</version>
</dependency>
对文件进行填充 并将多个文件打包成一个压缩包返回
//引入
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deepoove.poi.XWPFTemplate;
import com.ppz.erp.crm.dto.FinanceExcelDto;
import com.ppz.erp.crm.entity.StandardFile;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 对文件进行填充 并将多个文件打包成一个压缩包返回
* @param financeExcelDto
*/
public void downloadFiles(FinanceExcelDto financeExcelDto) {
StandardFile standardFile = getById(financeExcelDto.getId());
List<StandardFile> list = list(new LambdaQueryWrapper<StandardFile>().eq(StandardFile::getFileGroupType, standardFile.getFileGroupType()));
ByteArrayOutputStream output = new ByteArrayOutputStream();
ZipOutputStream zipOS = new ZipOutputStream(output);
list.forEach(l -> {
try {
String finalUrl = qiniuUtil.getFinalUrl(l.getFileUrl());
String[] split = finalUrl.split("(._.)");
String fileName = split[1].substring(1, split[1].indexOf("?"));
// 填充的文件为文档时 且文档后缀必须为.docx
if (l.getFileUrl().indexOf(".docx") > -1) {
URL url = new URL(java.net.URLDecoder.decode(finalUrl, "UTF-8"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为6秒
conn.setConnectTimeout(6 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
XWPFTemplate.compile(inputStream).render(financeExcelDto).writeToFile(fileName);
zipOS.putNextEntry(new ZipEntry(fileName)); // 这个是压缩包里子文件的名字,必要有
int n = 0;
File file = new File(fileName);
FileInputStream fileInputStream = new FileInputStream(file);
//缓冲区大小
byte[] byteBuffer = new byte[Integer.valueOf(file.length()+"")];
while (-1 != (n = fileInputStream.read(byteBuffer))) {
zipOS.write(byteBuffer, 0, n);
}
fileInputStream.close();
file.delete();
zipOS.closeEntry();
inputStream.close();
} else {
URL url = new URL(java.net.URLDecoder.decode(finalUrl, "UTF-8"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为6秒
conn.setConnectTimeout(6 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
ExcelReader excelReader = EasyExcel.read(inputStream).build();
List<ReadSheet> readSheets = excelReader.excelExecutor().sheetList();
HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
//设置超时间为6秒
conn2.setConnectTimeout(6 * 1000);
//防止屏蔽程序抓取而返回403错误
conn2.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream2 = conn2.getInputStream();
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(inputStream2).build();
readSheets.forEach(x -> {
WriteSheet writeSheet = EasyExcel.writerSheet(x.getSheetName()).build();
excelWriter.fill(financeExcelDto, writeSheet);
});
// 填充完之后关闭
excelWriter.close();
// 读取完之后关闭
excelReader.close();
zipOS.putNextEntry(new ZipEntry(fileName)); // 这个是压缩包里子文件的名字,必要有
int n = 0;
File file = new File(fileName);
FileInputStream fileInputStream = new FileInputStream(file);
//缓冲区大小
byte[] byteBuffer = new byte[Integer.valueOf(file.length()+"")];
while (-1 != (n = fileInputStream.read(byteBuffer))) {
zipOS.write(byteBuffer, 0, n);
}
zipOS.closeEntry();
fileInputStream.close();
inputStream.close();
inputStream2.close();
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
});
// 将压缩包传给前端
HttpServletResponse response = response();
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment; filename=线下财务报表模板.zip");
try {
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
bos.write(output.toByteArray());
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
zipOS.finish();
} catch (IOException e) {
e.printStackTrace();
}
try {
zipOS.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}