java - 从 csv 转换为 excel 期间出现 NoClassDefFoundError

标签 java excel csv

我遇到了这个异常

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException at com.restcalls.JSONtoCSV.main(JSONtoCSV.java:176)

当我尝试将 csv 文件转换为 excel 文件时。 此行似乎导致了问题 - XSSFWorkbook workBook = new XSSFWorkbook();

我已经添加了最新的 POI jar - 3.17,这是我的代码:

String xlsxFileAddress = "C:/Users/xxxxx/REST/exports/test.xlsx"; //xlsx file address
System.out.println("here 0");
XSSFWorkbook workBook = new XSSFWorkbook();
System.out.println("here 0.1");
XSSFSheet sheet =  workBook.createSheet("sheet1");
System.out.println("here 1");
String currentLine=null;
int RowNum=0;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
System.out.println("here 2: "+br);
while ((currentLine = br.readLine()) != null) {
    String str[] = currentLine.split(",");
    RowNum++;
    XSSFRow currentRow=sheet.createRow(RowNum);
    for(int i=0;i<str.length;i++){
        currentRow.createCell(i).setCellValue(str[i]);
    }
}

FileOutputStream fileOutputStream =  new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");

最佳答案

您需要下载 XML beans 依赖项并将其添加到您的类路径中。该库通常称为 xmlbeans-x.x.x.jar。例如:搜索并下载 xmlbeans-2.6.0.jar 或者如果您有一个 Maven 项目,则设置 POM 如下

  <dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.6.0</version>
  </dependency>

关于java - 从 csv 转换为 excel 期间出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46600427/

相关文章:

python:从文本导入数据

python - 读取具有可变长度字符串的文件时,pandas.read_csv 变慢

java - 捕获当前线程的执行者

java - 使用 RMI 动态创建对象

java - 观察者设计不起作用

excel - 是否可以将范围的开始和结束单元格设为变量?

java - 如何在 GWT 中取消转义字符串

python - 如何使用 Django 从磁盘返回 Excel 作为 HttpResponse

Vba excel 将单元格转换为希伯来货币(ILS)

c - 更新 CSV 文件中的数据