65 lines
2.0 KiB
Plaintext
65 lines
2.0 KiB
Plaintext
package com.cmeim.common.core.utils;
|
|
|
|
import com.alibaba.excel.ExcelWriter;
|
|
import com.alibaba.excel.metadata.Sheet;
|
|
import com.alibaba.excel.metadata.Table;
|
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class EasyExcelUtil {
|
|
|
|
|
|
public static void easyExportExcel(String filename,
|
|
String sheetName,
|
|
String[] columnName,
|
|
List<List<String>> dataList,
|
|
HttpServletResponse response) throws Exception {
|
|
|
|
ServletOutputStream out = null;
|
|
try {
|
|
out = response.getOutputStream();
|
|
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
|
|
|
|
// 设置EXCEL名称
|
|
String fileName = new String((filename).getBytes(), "UTF-8");
|
|
|
|
// 设置SHEET名称
|
|
Sheet sheet = new Sheet(1, 0);
|
|
sheet.setSheetName(sheetName);
|
|
|
|
// 设置标题
|
|
Table table = new Table(1);
|
|
List<List<String>> titles = new ArrayList<List<String>>();
|
|
for(int i=0;i<columnName.length;i++){
|
|
titles.add(Arrays.asList(columnName[i]));
|
|
}
|
|
table.setHead(titles);
|
|
|
|
writer.write0(dataList, sheet, table);
|
|
|
|
// 下载EXCEL
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes("gb2312"), "ISO-8859-1") + ".xlsx");
|
|
response.setContentType("multipart/form-data");
|
|
response.setCharacterEncoding("utf-8");
|
|
writer.finish();
|
|
out.flush();
|
|
|
|
} finally {
|
|
if (out != null) {
|
|
try {
|
|
out.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|