为了读取我正在使用 apache POI 的 xlsx
文件,我下载了 zip 并将以下 jsrs 放在我的 servlet 位置 webcontent/web-inf/lib
并通过 eclipse 配置构建路径
我的代码如下所示,
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
File uploadedFile = new File(fpath, fileName);
item.write(uploadedFile);
String mimeType = (Files.probeContentType(uploadedFile.toPath())).toString();
System.out.println(mimeType);
if(mimeType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
{
FileInputStream file = new FileInputStream(uploadedFile);
XSSFWorkbook workbook = new XSSFWorkbook(file);
for (int i =0; i < workbook.getNumberOfSheets(); i++)
{
XSSFSheet sheet = workbook.getSheetAt(i);
Iterator<Row> row = sheet.iterator();
while(row.hasNext()) {
Iterator<Cell> cellIterator = ((Row) row).cellIterator();
while(cellIterator.hasNext()) {
Cell cell1 = cellIterator.next();
switch(cell1.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell1.getBooleanCellValue() + "\n");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell1.getNumericCellValue() + "\n");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell1.getStringCellValue() + "\n");
break;
}
}
虽然这在 eclipse 上没有显示和错误,但当我尝试运行代码时显示以下错误
我的错误是什么?如何解决?
最佳答案
您需要添加 XML beans依赖于你的类路径。
该库通常称为xmlbeans-x.x.x.jar
关于java.lang.ClassNotFoundException : org. apache.xmlbeans.XmlException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23080945/