代码贴出来了,求各位给个解决方案!!
public String exportExcel(JSONArray exportList, String[] head, String name, String sheetname, boolean b,
HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 声明一个工作薄
String str = "";
SXSSFWorkbook wb = new SXSSFWorkbook(100);
Sheet sheet = wb.createSheet();
wb.setSheetName(0, sheetname);// 工作簿名称
// 给单子名称一个长度
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
CellStyle style = wb.createCellStyle();
// 创建第一行(也可以称为表头)
Row row = null;
row = sheet.createRow(0);
// 样式字体居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 创建单元格
Cell cell= null;
// 表头
for (int i = 0; i < head.length; i++) {
cell = row.createCell(i);
cell.setCellValue(head[i]);
}
JSONObject job = new JSONObject();
if (exportList.size() > 0) {
for (int i = 0; i < exportList.size(); i++) {
row = sheet.createRow(i + 1);
job = exportList.getJSONObject(i); // 遍历 jsonarray
// 数组,把每一个对象转成json 对象
Iterator it = job.keys();
int j = 0;
while (it.hasNext()&&j<head.length) { // 遍历对象中的所有键值
String key = (String) it.next();
String value = job.get(key).toString();
if (b) {// 去空
if (!"".equals(value)) {
row.createCell(j++).setCellValue(value);
;
}
} else {
row.createCell(j++).setCellValue(value);
;
}
}
if(i%3000==0||i==exportList.size()-1){
System.gc();
}
}
}
name = name+".xlsx";
// 清空response
resp.reset();
resp.setContentType("APPLICATION/OCTET-STREAM");
resp.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("utf-8"), "iso8859-1"));
try {
OutputStream out = resp.getOutputStream();
wb.write(out);
out.close();
str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
} catch (Exception e) {
e.printStackTrace();
str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
}
return str;
}