public class ExcelUtil {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, ExcelType type, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName,type);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<Map<String, Object>> list, String fileName,ExcelType type, HttpServletResponse response) {
defaultExport(list, fileName, type,response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null) ;
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName,ExcelType type, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, type);
if (workbook != null) ;
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw new RuntimeException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
throw new RuntimeException("excel文件不能为空");
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return list;
}
public static void exportMultiSheetExcel(List<ExportView> list, String fileName, ExcelType type,HttpServletResponse response) {
List<Map<String,Object>> excel = new ArrayList<>();
for(ExportView view :list){
Map<String,Object> sheet = new HashMap<>();
sheet.put("title",view.getExportParams());
sheet.put("entity",view.getCls());
sheet.put("data",view.getDataList());
excel.add(sheet);
}
exportExcel(excel,fileName,type,response);
}
public static class ExportView {
public ExportView(){
}
public ExportView(String sheetName,List<?> dataList,ExcelType type ,Class<?> cls){
ExportParams exportParams = new ExportParams(null, sheetName,type);
exportParams.setCreateHeadRows(false);
this.exportParams = exportParams;
this.dataList = dataList ;
this.cls = cls ;
}
private ExportParams exportParams;
private List<?> dataList;
private Class<?> cls;
public ExportParams getExportParams() {
return exportParams;
}
public void setExportParams(ExportParams exportParams) {
this.exportParams = exportParams;
}
public Class<?> getCls() {
return cls;
}
public void setCls(Class<?> cls) {
this.cls = cls;
}
public List<?> getDataList() {
return dataList;
}
public void setDataList(List<?> dataList) {
this.dataList = dataList;
}
}
}
easypoi多sheet导出
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近业务提了一个导出excle报表的功能需求,而之前我记忆中的导出功能,都是直接使用代码画表格的方式,往往是一大坨...
- 要完成导出样式如下图导出报表excel格式.png 1-4行:标题 5-12行:项目的基本信息 inputProD...
- Java 程序员在项目上一般会经常遇到解析数据、生成Excel的需求,比较流行的就是Apache poi框架了,p...
- 在很多时候我们都会和Excel打交道,会用Excel的人好像又多了一些技能,可以做数据统计啊,拿出来报表给老板看的...