java easyExcel框架
public class EasyExcelWriterFactory {
private int sheetNo = 0;
private ExcelWriter excelWriter = null;
public EasyExcelWriterFactory(OutputStream outputStream) {
excelWriter = EasyExcel.write(outputStream).build();
}
public EasyExcelWriterFactory(File file) {
excelWriter = EasyExcel.write(file).build();
}
public EasyExcelWriterFactory(String filePath) {
excelWriter = EasyExcel.write(filePath).build();
}
/**
* 链式模板表头写入
* @param headClazz 表头格式
* @param data 数据 List<ExcelModel> 或者List<List<Object>>
* @return
*/
public EasyExcelWriterFactory writeModel(Class headClazz, List data, String sheetName){
excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(headClazz).build());
return this;
}
/**
* 链式自定义表头写入
* @param head
* @param data 数据 List<ExcelModel> 或者List<List<Object>>
* @param sheetName
* @return
*/
public EasyExcelWriterFactory write(List<List<String>> head, List data, String sheetName){
excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(head).build());
return this;
}
public void finish() {
excelWriter.finish();
}
}
public class MultipleSheelPropety<T> implements Serializable {
private static final long serialVersionUID = 1L;
private List<T> datas;
private String sheetName;
}
public class EasyExcelUtil {
/*阿里巴巴easyExcle框架================================== -end*/
/**
* 同步无模型读(指定sheet和表头占的行数)
* @param inputStream
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @return List<Map < colNum, cellValue>>
*/
public static <T> List<T> syncRead(InputStream inputStream, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
}
/**
* 同步按模型读(指定sheet和表头占的行数)
*
* @param inputStream
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @return
*/
public static <T> List<T> syncReadModel(InputStream inputStream, Class clazz, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
}
/**
* web导出excel文件
* @param response response
* @param list 实体数据
* @param fileName 文件名
* @param sheetName sheet名
* @param clazz 实体类
* @throws Exception
*/
public static void exprotExcel(HttpServletResponse response, List<?> list, String fileName, String sheetName, Class<?> clazz)throws Exception {
EasyExcel.write(getOutputStream(fileName,response), clazz)
.sheet(sheetName)
.doWrite(list);
}
/**
* web根据excel模板导出excel文件
* @param response response
* @param data 实体数据
* @param templateFileName 模板名称
* @param fileName 文件名
* @param sheetName sheet名
* @throws Exception
*/
public static void exprotTemplateExcel(HttpServletResponse response, String templateFileName,
List data, String fileName, String sheetName) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.withTemplate(new ClassPathResource("template/excel/" + templateFileName).getInputStream())
.build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 填充集合 {.name}
excelWriter.fill(data, writeSheet);
excelWriter.finish();
} catch (IOException e) {
throw new CpmException("导出excel表格失败!", e);
}
}
/**
* 多个sheet页的数据链式写入(失败了会返回一个有部分数据的Excel)
* ExcelUtil.writeWithSheets(response, exportFileName)
* .writeModel(ExcelModel.class, excelModelList, "sheetName1")
* .write(headData, data,"sheetName2")
* .finish();
*
* @param response
* @param exportFileName 导出的文件名称
* @return
*/
public static EasyExcelWriterFactory writeWithSheetsWeb(HttpServletResponse response, String exportFileName) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String fileName = URLEncoder.encode(exportFileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(response.getOutputStream());
return excelWriter;
}
/**
* 多个sheet页的数据链式写入
* ExcelUtil.writeWithSheets(outputStream)
* .writeModel(ExcelModel.class, excelModelList, "sheetName1")
* .write(headData, data,"sheetName2")
* .finish();
*
* @param outputStream
* @return
*/
public static EasyExcelWriterFactory writeWithSheets(OutputStream outputStream) {
EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(outputStream);
return excelWriter;
}
/**
* 导出文件时为Writer生成OutputStream
* @param fileName
* @param response
* @return
*/
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) {
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
return response.getOutputStream();
} catch (IOException e) {
throw new CpmException("导出excel表格失败!", e);
}
}
/**
* 导出 Excel :多个 sheet,带表头
*
* @param response HttpServletResponse
* @param multipleSheelPropetys 数据 list
* @param fileName 导出的文件名
*/
public static <T> void writeExcelWithSheets(HttpServletResponse response, List<MultipleSheelPropety> multipleSheelPropetys,
String fileName) {
OutputStream outputStream = getOutputStream(response, fileName);
Integer sheetNo = 0;
ExcelWriter excelWriter = EasyExcelFactory.getWriter(outputStream);
for (MultipleSheelPropety multipleSheelPropety : multipleSheelPropetys) {
WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo, multipleSheelPropety.getSheetName()).head(multipleSheelPropety.getDatas().get(0).getClass()).build();
excelWriter.write(multipleSheelPropety.getDatas(), writeSheet);
sheetNo++;
}
excelWriter.finish();
}
/*阿里巴巴easyExcle框架================================== -end*/
}
每天一点点,惊喜不间断