先看源码。分为三个部分
实体类:
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Getter;
import lombok.Setter; import java.io.Serializable;
import java.math.BigDecimal; @Getter
@Setter
public class ErpStoreVo implements Serializable { @Excel(name = "店铺编号")
private String storeNo; @Excel(name = "店铺名称")
private String name; @Excel(name = "启用状态")
private String enableStatus;
...等等其他属性
}
这是导出部分的代码:
//下载导入模板 第一个方法
@PostMapping("/importStoreModel")
public void importStoreModel(HttpServletResponse response) throws Exception { List<ErpStoreVo> vo = new ArrayList<>(); exportExcel(response, "店铺信息记录", ErpStoreVo.class, BaseUtil.copyList(vo, ErpStoreVo.class));
}
//第二个方法
import cn.afterturn.easypoi.excel.ExcelExportUtil;private void exportExcel(HttpServletResponse response, String outputName, Class<?> clazz, List<?> list) throws Exception {response.setCharacterEncoding("UTF-8"); String fileName = URLEncoder.encode(outputName, "UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx"); response.setContentType("application/vnd.openxmlformats- officedocument.spreadsheetml.sheet"); response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.addHeader("Access-Control-Allow-Headers",
"Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,token"); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), clazz, list); //workbook.write(response.getOutputStream()); ServletOutputStream os = response.getOutputStream(); workbook.write(os); os.close();
}
导出的时候会报错:
cn.afterturn.easypoi.exception.excel.ExcelExportException: Excel导出错误at cn.afterturn.easypoi.excel.export.ExcelExportService.createSheet(ExcelExportService.java:181)at cn.afterturn.easypoi.excel.ExcelExportUtil.exportExcel(ExcelExportUtil.java:116)at cn.erp.pm.PmController.exportExcel(PmController.java:198)at cn.erp.pm.PmController.importStoreModel(PmController.java:220)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)at javax.servlet.http.HttpServlet.service(HttpServlet.java:523)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)at javax.servlet.http.HttpServlet.service(HttpServlet.java:590)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: nullat cn.afterturn.easypoi.excel.export.ExcelExportService.createSheetForMap(ExcelExportService.java:219)at cn.afterturn.easypoi.excel.export.ExcelExportService.createSheet(ExcelExportService.java:178)... 50 common frames omitted
后来修正了第一个方法,就不再报错 – 添加了实体类记录
@PostMapping("/importStoreModel")
public void importStoreModel(HttpServletResponse response) throws Exception {
List<ErpStoreVo> vo = new ArrayList<>();
//没有数据时手动加一条防止报错
vo.add(new ErpStoreVo());
exportExcel(response, "店铺信息记录", ErpStoreVo.class, BaseUtil.copyList(vo, ErpStoreVo.class));
}