首先感谢阿里巴巴提供了easyexcel工具类,github地址:https://github.com/alibaba/easyexcel
注意!!这里只是一个简单的示例,VC大法即可使用,对于复杂的execl导出可能会出现问题。
另Execl文件后缀为xlsx。
1、环境搭建
jar包依赖
<!-- excel导入导出插件 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.1.2-beat1</version> </dependency>
2、代码
public class MyExcel { //读 @Test public void simpleRead() { FileInputStream fileInput; try { fileInput = new FileInputStream("F://javaio文件目录//hss.xlsx"); List<Object> read = EasyExcelFactory.read(fileInput, new Sheet(0,0)); System.out.println(read); } catch (FileNotFoundException e) { e.printStackTrace(); } } //写 @Test public void simpleWrite() { FileOutputStream fileOut; try { File file = new File("F://javaio文件目录//hss123.xlsx"); if (file.exists()) { file.delete(); } fileOut=new FileOutputStream("F://javaio文件目录//hss123.xlsx"); ExcelWriter writer = EasyExcelFactory.getWriter(fileOut); Sheet sheet = new Sheet(1,0); Sheet sheet2 = new Sheet(1,0); sheet.setSheetName("HelloWord"); List<List<String>> data2 = new ArrayList<>(); List<String> list2 = new ArrayList<>(); List<String> list3 = new ArrayList<>(); List<String> list4 = new ArrayList<>(); List<String> list5 = new ArrayList<>(); List<List<String>> data = new ArrayList<>(); List<String> list1 = new ArrayList<>(); for (int i = 0; i < 10; i++) { list1.add("123"); list1.add("123"); list1.add("123"); list1.add("123"); list1.add("123"); data.add(list1); } list2.add("你好1"); list3.add("你好2"); list4.add("你好3"); list5.add("你好4"); data2.add(list2); data2.add(list3); data2.add(list4); data2.add(list5); sheet2.setHead(data2); writer.write0(null, sheet2); writer.write0(data, sheet); writer.finish(); fileOut.close(); } catch (Exception e) { e.printStackTrace(); } } }
3.作为Util类使用(使用时调用
baseExportExcel
)具体class类型地址 https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java
/** * excel工具类 */ @Slf4j public class EasyexcelUtil { protected static FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHH:mm:ss"); /** * * @param fileName 文件名 * @param cameraReportStopList 普通vo类 * @param entityModel 继承BaseRowModel的模型class * @param response * @param <T> */ public static <T extends BaseRowModel> void baseExportExcel(String fileName , List cameraReportStopList , Class<T> entityModel , HttpServletResponse response) { List<BaseRowModel> modelList = new ArrayList<>(); if (StringUtils.isEmpty(cameraReportStopList)) { return; } cameraReportStopList.forEach(cameraReportStopVo -> { try{ BaseRowModel cameraReportStopModel = entityModel.newInstance(); BeanUtils.copyProperties(cameraReportStopVo , cameraReportStopModel); modelList.add(cameraReportStopModel); } catch (Exception e){ log.info("excel export exception",e); } }); try{ writeExcel(response , modelList , fileName+fastDateFormat.format(new Date()) , "第一页"); } catch (Exception e){ log.info("excel export exception",e); e.printStackTrace(); } } /** * 导出 Excel :一个 sheet,带表头 * * @param response HttpServletResponse * @param list 数据 list,每个元素为一个 BaseRowModel * @param fileName 导出的文件名 * @param sheetName 导入文件的 sheet 名 */ public static void writeExcel(HttpServletResponse response, List<? extends BaseRowModel> list, String fileName, String sheetName) throws Exception { ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX); Class clazz = null; if (list.size() > 0) { clazz = list.get(0).getClass(); } else { clazz = BaseRowModel.class; } Sheet sheet = new Sheet(1, 0, clazz); sheet.setSheetName(sheetName); writer.write(list, sheet); writer.finish(); } /** * 导出文件时为Writer生成OutputStream * @param fileName * @param response * @return * @throws Exception */ private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception { 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) { log.info("excel export exception",e); ExceptionCast.cast("导出失败!"); } return null; } }