java 代码
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
/**
* @ClassName ExcelPoiUtil
* @Description
* @Author yangyao
* @Date 2022/9/11
**/
public class ExcelPoiUtil {
/**
* 生成 stream流 到前端浏览器
* @param filePath 存储路径 ,如:data\DocumentCenter\WorkDocument\digitalTooling\1832a49d4de.xlsx
**/
public static void downStream(String filePath,HttpServletResponse response){
InputStream in = null;
ServletOutputStream outputStream = null;
response.reset();
response.setContentType("application/ostet-stream");
try {
in = new FileInputStream(filePath);
outputStream = response.getOutputStream();
//创建存放文件内容的数组
byte[] buff =new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while((n=in.read(buff))!=-1){
//将字节数组的数据全部写入到输出流中
outputStream.write(buff,0,n);
}
//强制将缓存区的数据进行输出
outputStream.println();
} catch (IOException e) {
e.printStackTrace();
}finally {
CloseableUtils.close(outputStream, in);
}
}
/**
* 生成 stream流 到前端浏览器
* @param filePath 存储路径 ,如:data\DocumentCenter\WorkDocument\digitalTooling\1832a49d4de.xlsx
**/
public static void downStream2(String filePath,HttpServletResponse response){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
File file = new File(filePath);
ExceptionVerifyUtil.verify(!file.exists(), ApiCode.BUSINESS_EXCEPTION,"附件路径不存在");
inputStream = new FileInputStream(file);
outputStream = new BufferedOutputStream(response.getOutputStream());
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1){
response.getOutputStream().write(bytes,0,len);
}
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
CloseableUtils.close(outputStream, inputStream);
}
}
/**
* 生成 excel 到前端浏览器
* @param filePath 存储路径 ,如:data\DocumentCenter\WorkDocument\digitalTooling\1832a49d4de.xlsx
* @param fileName 导出的文件名字 1832a49d4de.xlsx
**/
public static void downExcel(String filePath,String fileName,HttpServletResponse response){
FileInputStream in;
try {
in = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
throw new BusinessException("file does not exist.");
}
downExcel2(in,fileName, response);
}
/**
* 生成 excel 到前端浏览器
* @param in FileInputStream流
* @param fileName 导出的文件名字 1832a49d4de.xlsx
**/
public static void downExcel2(InputStream in,String fileName, HttpServletResponse response) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ServletOutputStream ouputStream = null;
try {
int len = 0;
byte[] b = new byte[1024];
while ((len = in.read(b, 0, b.length)) != -1) {
os.write(b, 0, len);
}
byte[] bytes = os.toByteArray();
response.reset(); // 清空输出流
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Cache-Control", "no-cache");
response.setContentLength(bytes.length);
ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.close(ouputStream, os);
}
}
/**
* 生成excel到前端浏览器
* @param wb 组装好的XSSFWorkbook对象
* @param fileName 导出的文件名字
*/
public static void downExcel3(String fileName, XSSFWorkbook wb, HttpServletResponse response) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ServletOutputStream ouputStream = null;
try {
wb.write(os);
byte[] bytes = os.toByteArray();
response.reset(); // 清空输出流
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(fileName+ ".xlsx", "UTF-8"));
response.addHeader("Cache-Control", "no-cache");
response.setContentLength(bytes.length);
ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableUtils.close(ouputStream, os);
}
}
}