|
|
@ -64,6 +64,36 @@ public class ExcelUtils { |
|
|
|
out.close(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Excel导出 |
|
|
|
* |
|
|
|
* @param response response |
|
|
|
* @param fileName 文件名 |
|
|
|
* @param list 数据List |
|
|
|
* @param pojoClass 对象Class |
|
|
|
*/ |
|
|
|
public static void exportExcelEpmet(HttpServletResponse response, String fileName, Collection<?> list, |
|
|
|
Class<?> pojoClass) throws IOException { |
|
|
|
if(StringUtils.isBlank(fileName)){ |
|
|
|
//当前日期
|
|
|
|
fileName = DateUtils.format(new Date()); |
|
|
|
} |
|
|
|
|
|
|
|
ExportParams exportParams = new ExportParams(); |
|
|
|
//设置导出的样式
|
|
|
|
exportParams.setStyle(EasyPoiExcelExportStylerImpl.class); |
|
|
|
//设置sheet名称
|
|
|
|
exportParams.setSheetName("Sheet1"); |
|
|
|
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); |
|
|
|
Sheet sheet1 = workbook.getSheetAt(0); |
|
|
|
sheet1.setDefaultColumnWidth(50*256); |
|
|
|
//sheet1.setDefaultRowHeight((short)(2*256));
|
|
|
|
ServletOutputStream out = ExcelUtils.getOutputStreamForExcelEpmet(fileName,response); |
|
|
|
workbook.write(out); |
|
|
|
out.flush(); |
|
|
|
out.close(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Excel导出,先sourceList转换成List<targetClass>,再导出 |
|
|
|
* |
|
|
@ -84,6 +114,26 @@ public class ExcelUtils { |
|
|
|
exportExcel(response, fileName, targetList, targetClass); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Excel导出,先sourceList转换成List<targetClass>,再导出 |
|
|
|
* |
|
|
|
* @param response response |
|
|
|
* @param fileName 文件名 |
|
|
|
* @param sourceList 原数据List |
|
|
|
* @param targetClass 目标对象Class |
|
|
|
*/ |
|
|
|
public static void exportEpmetExcel(HttpServletResponse response, String fileName, Collection<?> sourceList, |
|
|
|
Class<?> targetClass) throws Exception { |
|
|
|
List<Object> targetList = new ArrayList<>(sourceList.size()); |
|
|
|
for(Object source : sourceList){ |
|
|
|
Object target = targetClass.newInstance(); |
|
|
|
BeanUtils.copyProperties(source, target); |
|
|
|
targetList.add(target); |
|
|
|
} |
|
|
|
|
|
|
|
exportExcelEpmet(response, fileName, targetList, targetClass); |
|
|
|
} |
|
|
|
|
|
|
|
public static ServletOutputStream getOutputStreamForExcel(String fileName, HttpServletResponse response) throws IOException { |
|
|
|
fileName = URLEncoder.encode(fileName, "UTF-8"); |
|
|
|
if (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")){ |
|
|
@ -97,6 +147,19 @@ public class ExcelUtils { |
|
|
|
return response.getOutputStream(); |
|
|
|
} |
|
|
|
|
|
|
|
public static ServletOutputStream getOutputStreamForExcelEpmet(String fileName, HttpServletResponse response) throws IOException { |
|
|
|
fileName = URLEncoder.encode(fileName, "UTF-8"); |
|
|
|
if (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")){ |
|
|
|
fileName = fileName + ".xls"; |
|
|
|
} |
|
|
|
response.setContentType("application/vnd.ms-excel"); |
|
|
|
response.setCharacterEncoding("utf8"); |
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + fileName); |
|
|
|
response.addHeader("Access-Control-Expose-Headers", "Content-disposition"); |
|
|
|
|
|
|
|
return response.getOutputStream(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* desc:easypoi导出多个sheet |
|
|
|
* @param fileName |
|
|
|