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;
}