package com.nanometer.smartlab.util;
|
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.xssf.streaming.SXSSFRow;
|
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
import javax.faces.context.FacesContext;
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
public class ExcelUtils {
|
|
public static SXSSFWorkbook exportExcelNew(Map<String, String> map, List<Map> dataList, String name){
|
SXSSFWorkbook sb = new SXSSFWorkbook(1000);
|
Integer total = null;
|
//sheet名字
|
SXSSFSheet sheet = sb.createSheet(name);
|
SXSSFRow titlerRow = sheet.createRow(0);
|
|
int k = 0;
|
for(Map.Entry<String, String> entry:map.entrySet()){
|
//表头
|
titlerRow.createCell(k).setCellValue(entry.getValue());
|
k++;
|
}
|
//数据
|
for (int i = 0; i < dataList.size(); i++) {
|
SXSSFRow dataRow = sheet.createRow(i + 1);
|
int j = 0;
|
for(Map.Entry<String, String> entry:map.entrySet()){
|
//表头
|
dataRow.createCell(j).setCellValue(dataList.get(i).get(entry.getKey()) == null ? "" : String.valueOf(dataList.get(i).get(entry.getKey())));
|
j++;
|
}
|
}
|
|
|
|
return sb;
|
}
|
|
|
public static void export2Excel(List<Map> list, String name, Map<String, String> map) throws Exception {
|
if (list == null || list.size() == 0) {
|
throw new Exception("没有数据可以导出");
|
}
|
|
OutputStream out = null;
|
InputStream is = null;
|
try {
|
FacesContext ctx = FacesContext.getCurrentInstance();
|
ctx.responseComplete();
|
String contentType = "application/x-download";
|
HttpServletResponse response = (HttpServletResponse) ctx
|
.getExternalContext().getResponse();
|
response.setContentType(contentType);
|
StringBuffer contentDisposition = new StringBuffer();
|
contentDisposition.append("attachment;");
|
contentDisposition.append("filename=\"");
|
contentDisposition.append(name).append(".xlsx");
|
contentDisposition.append("\"");
|
response.setHeader(
|
"Content-Disposition",
|
new String(contentDisposition.toString().getBytes(
|
System.getProperty("file.encoding")), "ISO8859-1"));
|
out = response.getOutputStream();
|
SXSSFWorkbook sb = ExcelUtils.exportExcelNew(map, list, name);
|
sb.write(out);
|
out.flush();
|
sb.dispose();// 释放workbook所占用的所有windows资源
|
ctx.responseComplete();
|
} catch (Exception e) {
|
if (is != null) {
|
try {
|
is.close();
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
}
|
if (out != null) {
|
try {
|
out.close();
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
}
|
e.printStackTrace();
|
} finally {
|
if (is != null) {
|
try {
|
is.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (out != null) {
|
try {
|
out.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
}
|