阅读XLSX文件有限制吗?使用XLS文件,我没有遇到任何问题。
我的环境是:
Apache POI库的位置
I unzipped the downloaded POI package into the following folder:
C:\Program Files\SmartBear\SoapUI-5.3.0\bin\ext
fyi - I have also tried adding it to lib
我尝试运行下面提供的groovy脚本时遇到的错误是:
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
我的Groovy脚本是:
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
String filepath = "E:\\SoapUiProjects\\MyWorkspace\\data\\data.XLSX"
String sheetName = "login"
FileInputStream fis = new FileInputStream(new File(filepath))
Workbook wb = WorkbookFactory.create(fis)
Sheet sh = wb.getSheet(sheetName)
Iterator itrRow = sh.rowIterator()
while(itrRow.hasNext())
{
Row row = itrRow.next()
for(Cell cell : row)
{
switch(cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
log.info cell.getNumericCellValue()
break;
default:
log.info cell.getRichStringCellValue()
}
}
}
fis.close()
wb.close()
Stacktrace:
2017-07-11 15:09:50,631 ERROR [SoapUI] An error occurred [java.lang.reflect.InvocationTargetException], see error log for details
2017-07-11 15:09:50,631 ERROR [errorlog] org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:249)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:211)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:170)
at org.apache.poi.ss.usermodel.WorkbookFactory$create.call(Unknown Source)
at Script21.run(Script21.groovy:14)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:90)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141)
at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.POIXMLTypeLoader
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:85)
... 19 more
提前谢谢你。
答案 0 :(得分:0)
poi-ooxml jar不可用时抛出异常
java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.POIXMLTypeLoader
我已将下面的库添加到soap UI ext文件夹中。
C:\Program Files\SmartBear\SoapUI-5.3.0\bin\ext
<强>库强>
commons-collections4-4.1.jar
ooxml-schemas-1.3.jar
ooxml-security-1.1.jar
openxml4j-1.0-beta.jar
poi-3.16.jar
poi-contrib-3.6.jar
poi-examples-3.16.jar
poi-excelant-3.16.jar
poi-ooxml-3.16.jar
poi-ooxml-schemas-3.16.jar
poi-scratchpad-3.16.jar
您的代码适用于我。
Groovy代码:
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
String filepath = "D:\\testdata\\TestData.xlsx"
String sheetName = "Sheet1"
FileInputStream fis = new FileInputStream(new File(filepath))
Workbook wb = WorkbookFactory.create(fis)
Sheet sh = wb.getSheet(sheetName)
Iterator itrRow = sh.rowIterator()
while(itrRow.hasNext())
{
Row row = itrRow.next()
for(Cell cell : row)
{
switch(cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
log.info cell.getNumericCellValue()
break;
default:
log.info cell.getRichStringCellValue()
}
}
}
fis.close()
wb.close()
<强>输出:强>
Sat Jul 29 08:00:54 SGT 2017:INFO:VarName
Sat Jul 29 08:00:54 SGT 2017:INFO:URL
Sat Jul 29 08:00:54 SGT 2017:INFO:DBURL
Sat Jul 29 08:00:54 SGT 2017:INFO:https://www.stackoverflow.com
Sat Jul 29 08:00:54 SGT 2017:INFO:Test