日期:2014-05-19  浏览次数:20807 次

poi操作Excel表格:我是现读取了Excel表格的一个预设模版,然后想在模版下填充数值。发生了问题
File file = new File(filePath);

//读取Excel表到
InputStream inputStream = new FileInputStream(file);

//读取
HSSFWorkbook hwb = new HSSFWorkbook(inputStream);-----------------------①

//获取Sheet工作区域
HSSFSheet sheet = hwb.getSheetAt(0);
——————————————————————————————————————————————————
①出报错:

java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes
at org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:130)
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:81)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:298)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:279)
at com.xxcd.tool.DownLoadExcel.updateExcel(DownLoadExcel.java:48)
at com.xxcd.action.YwTsiAction.downLoadExcel(YwTsiAction.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxcd.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)

------解决方案--------------------
Java code

HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(inputStream));

------解决方案--------------------
没看代码不知道神马原因 ,不过这个功能我也曾做过,给点建议
1.inputStream 是否有问题?

2.你的excel 文件是03?还是07? 它们使用的HSSFWorkbook【03】XSSFWorkbook【07】 是不一样的