数据超过百万导出excel一个小工具类

public boolean exportExcel(HttpServletRequest request, HttpServletResponse response, List s) {
boolean flage ;
try {
SXSSFWorkbook wb = new SXSSFWorkbook(10000);
SXSSFSheet sheet = wb.createSheet();
SXSSFRow row = sheet.createRow(0);
//表头栏
for (int i = 0; i < testexcel.length; i++) {
SXSSFCell cell = row.createCell(i);
cell.setCellValue(testexcel[i]);
}
for (int i = 0; i < s.size(); i++) {
row = sheet.createRow(i + 1);
//序号
row.createCell(0).setCellValue(i + 1);
//所属项目
if (s.get(i).getStypeid() != null) {
row.createCell(1).setCellValue(s.get(i).getStypeid());
}
if (s.get(i).getStypename() != null) {
row.createCell(2).setCellValue(s.get(i).getStypename());
}
if (s.get(i).getUpload_data() != null) {
row.createCell(3).setCellValue(s.get(i).getUpload_data());
}
if(s.get(i).getStandard_content()!=null){
if (s.get(i).getStandard_content().contains(",")) {
String[] split = s.get(i).getStandard_content().split(",");
for (int j = 0; j < split.length; j++) {
row.createCell(4 + j).setCellValue(split[j]);
}
} else {
if (s.get(i).getStandard_content() != null) {
row.createCell(4).setCellValue(s.get(i).getStandard_content());
}
}
}
}
File pathServer = new File(request.getSession().getServletContext().getRealPath("/") + “excel”);
//判断是否有这个目录
if (!pathServer.exists()) {
pathServer.mkdirs();
}
//将excel文件保存到服务器
FileOutputStream fos = new FileOutputStream(pathServer + “/” + “jl.xlsx”);
wb.write(fos);
fos.close();
//弹出下载框部分
ServletContext context = request.getSession().getServletContext();
//弹出下载框里默认名称
String path = context.getRealPath(“jl.xlsx”);
//获取文件名字
String filename = path.substring(path.lastIndexOf("\") + 1);
filename = URLEncoder.encode(filename, “utf-8”);
//设置响应头
response.setHeader(“content-disposition”, “attachment;filename=” + filename);
//从下面这个路径读取要下载文件
@SuppressWarnings(“resource”)
InputStream is = new FileInputStream(pathServer + “/” + “jl.xlsx”);
OutputStream out = response.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
flage = true;
} catch (Exception e) {
e.printStackTrace();
flage = false;
}
return flage;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值