为了读取多个excel文件,我必须创建一个返回此excel文件中行数的方法,但问题是我打开工作簿两次。请等待你的建议!!
这是返回此excel文件中行数的方法:
public static int calculateSizeFile(InputStream file)
{
int size = 0;
try {
// Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() != 0) {
size++;
}
file.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return size;
}
这是返回此excel文件中对象列表的方法
public AtlasDto[] getAtlasOperation(FileInputStream inputDuplic,int size) {
int r = 0;
AtlasDto[] listOperationAtlas2 = new AtlasDto[size];
try {
// Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(inputDuplic);
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
System.out.println("la taille de atlas est : " + r);
String accountNumber = null;
String ordinal = null;
String devise = null;
double nominal = 0;
// Get the workbook instance for XLS file
int i = 0;
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() != 0) {
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getColumnIndex() == 4) {
String account = ""
+ sheet.getRow(row.getRowNum()).getCell(4)
.getRawValue();
accountNumber = account.substring(1);
}
if (cell.getColumnIndex() == 9) {
ordinal = ""
+ sheet.getRow(row.getRowNum()).getCell(9)
.getRawValue();
}
if (cell.getColumnIndex() == 7) {
devise = cell.getStringCellValue().toString();
}
if (cell.getColumnIndex() == 8) {
nominal = cell.getNumericCellValue();
}
}
AtlasDto operationAtlas = new AtlasDto(accountNumber,
ordinal, devise, nominal);
listOperationAtlas2[i] = operationAtlas;
i++;
}
inputDuplic.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listOperationAtlas2;
}
这就是我得到的异常
java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:234)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:402)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:278)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:51)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:84)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:218)
at ma.bmci.business.dao.impl.DuplicDaoImpl.getAtlasOperation(DuplicDaoImpl.java:42)
at ma.bmci.business.service.impl.DuplicServiceImpl.getDuplicService(DuplicServiceImpl.java:24)
at ma.bmci.web.managedBeans.DuplicBean.init(DuplicBean.java:59)
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:483)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
首先,在getAtlasOperation()
中,您正在传递FileInputStream inputDuplic
并且您正在关闭它:
inputDuplic.close();
在while
循环的一次迭代后{p>:while (rowIterator.hasNext()) { ...
。
您在calculateSizeFile()
中遇到同样的问题,因为您已将文件关闭:
file.close();
在while
循环中,但您仍在从工作簿中访问数据。
在while
循环结束后移动那些关闭文件流的行,它应该可以正常工作。