[alibaba/easyexcel]excel 导出出现数组越界

2024-05-24 31 views
0

触发场景描述 在本地、开发环境都能够正常导出excel文件 在测试环境中使用时无法正常导出 触发Bug的代码

   response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    fileName = URLEncoder.encode(fileName, "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), clazz)
        .sheet("Sheet1")
        .doWrite(datas);

提示的异常或者没有达到的效果 [2019-11-26 16:38:37.444] [http-nio-8068-exec-5] [ERROR] [] DirectJDKLog.log:175 - Servlet.service() for servlet [dispatcherServlet] in context with path [/ed/api] threw exception [Request processing failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: 0] with root cause java.lang.ArrayIndexOutOfBoundsException: 0 at sun.font.CompositeFont.getSlotFont(CompositeFont.java:340) at sun.font.CompositeGlyphMapper.initMapper(CompositeGlyphMapper.java:81) at sun.font.CompositeGlyphMapper.(CompositeGlyphMapper.java:62) at sun.font.CompositeFont.getMapper(CompositeFont.java:398) at sun.font.CompositeFont.canDisplay(CompositeFont.java:424) at java.awt.Font.canDisplayUpTo(Font.java:2063) at java.awt.font.TextLayout.singleFont(TextLayout.java:470)

回答

9

没有看到easyExcel相关的逻辑,请提供更多堆栈信息

6
font{
    line-height: 1.6;
}
ul,ol{
    padding-left: 20px;
    list-style-position: inside;
}

    报错信息如下:java.lang.ArrayIndexOutOfBoundsException: 0  at sun.font.CompositeFont.getSlotFont(CompositeFont.java:340)   at sun.font.CompositeGlyphMapper.initMapper(CompositeGlyphMapper.java:81)   at sun.font.CompositeGlyphMapper.<init>(CompositeGlyphMapper.java:62)   at sun.font.CompositeFont.getMapper(CompositeFont.java:398) at sun.font.CompositeFont.canDisplay(CompositeFont.java:424)    at java.awt.Font.canDisplayUpTo(Font.java:2063) at java.awt.font.TextLayout.singleFont(TextLayout.java:470) at java.awt.font.TextLayout.<init>(TextLayout.java:531) at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275) at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)   at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:82)  at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:658)  at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:679)  at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:90)   at com.alibaba.excel.util.WorkBookUtil.createSheet(WorkBookUtil.java:66)    at com.alibaba.excel.context.WriteContextImpl.initSheet(WriteContextImpl.java:154)  at com.alibaba.excel.context.WriteContextImpl.currentSheet(WriteContextImpl.java:131)   at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:56)    at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161)    at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146)    at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:190)    at com.netease.hz.bdms.ed.service.util.ExcelUtil.exportExcel$sentryProxy2(ExcelUtil.java:92)    at com.netease.hz.bdms.ed.service.util.ExcelUtil.exportExcel(ExcelUtil.java)    at com.netease.hz.bdms.ed.facade.TableCapturedFacade.listUnreasonableNameTable$sentryProxy7(TableCapturedFacade.java:795)   at com.netease.hz.bdms.ed.facade.TableCapturedFacade.listUnreasonableNameTable(TableCapturedFacade.java)    at com.netease.hz.bdms.ed.facade.TableCapturedFacade$$FastClassBySpringCGLIB$$488314c7.invoke(<generated>)  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)  at com.netease.hz.bdms.ed.facade.TableCapturedFacade$$EnhancerBySpringCGLIB$$150bade.listUnreasonableNameTable(<generated>) at com.netease.hz.bdms.ed.controller.TableCapturedController.listUnreasonableNameTables$sentryProxy7(TableCapturedController.java:221)  at com.netease.hz.bdms.ed.controller.TableCapturedController.listUnreasonableNameTables(TableCapturedController.java)   at com.netease.hz.bdms.ed.controller.TableCapturedController$$FastClassBySpringCGLIB$$704413cb.invoke(<generated>)  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) at com.netease.hz.bdms.ed.aop.PermissionAspect.around(PermissionAspect.java:44) at sun.reflect.GeneratedMethodAccessor211.invoke(Unknown Source)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)  at com.netease.hz.bdms.ed.controller.TableCapturedController$$EnhancerBySpringCGLIB$$5d69027e.listUnreasonableNameTables(<generated>)   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:104)  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)  at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)   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.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  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:92) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  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.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  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:200)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  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:200)  at org.apache.catalina.core.StandardContextValve.invoke$sentryProxy(StandardContextValve.java:96)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)   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:408)   at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)  at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)   at java.lang.Thread.run(Thread.java:745)代码逻辑:/** * 输出Excel文件 * * 列名默认为clazz对象中的字段名,如果需要定义为中文名,需要在字段上加@ExcelProperty,指定列名 * * @param fileName 输出文件的文件名 * @param datas 数据列表 * @param response 响应 * @return 导出Excel */public static <T> void exportExcel(String fileName, Class clazz, List<T> datas,    HttpServletResponse response) throws IOException {  Preconditions.checkArgument(StringUtils.isNotBlank(fileName));  // 设置文件名  response.setContentType("application/vnd.ms-excel");  response.setCharacterEncoding("utf-8");  fileName = URLEncoder.encode(fileName, "UTF-8");  response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");  EasyExcel.write(response.getOutputStream(), clazz)      .sheet("Sheet1")      .doWrite(datas);}

                            2271545898

                                2271545898@qq.com

    签名由
    网易邮箱大师
    定制

在2019年11月27日 16:02,pdkst<notifications@github.com> 写道: 

没有看到easyExcel相关的逻辑,请提供更多堆栈信息

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.

9

应该是你的测试环境缺少了一些字体、或者字体相关的配置导致的 参考资料

9

可能是最后一行什么都没写,但是被识别到了,可是尝试把数据后面的空白行删掉,再重新选择试试看

3

可能是最后一行什么都没写,但是被识别到了,可是尝试把数据后面的空白行删掉,再重新选择试试看

与数据无关,和运行机器相关

2
    ExcelWriterBuilder write = EasyExcel.write(new FileOutputStream(new File("")));
    write.registerWriteHandler(new WorkbookWriteHandler(){
        @Override
        public void beforeWorkbookCreate() {}
        @Override
        public void afterWorkbookCreate(WriteWorkbookHolder writeWorkbookHolder) {
            Workbook workbook = writeWorkbookHolder.getWorkbook();
            Font font = workbook.getFontAt(0);
            font.setFontName("FontName");
        }
    });
    write.sheet().doWrite(objects);