简体   繁体   English

无法使用poi api将xlsx转换为csv

[英]can not convert xlsx to csv using poi api

I am trying to convert xlsx file to csv file using below code 我正在尝试使用以下代码将xlsx文件转换为csv文件

import java.io.*;
import java.util.Iterator;
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;

public class XLStoCSVConvert {

     static void xlsx(File inputFile, File outputFile) {
            // For storing data into CSV files
            StringBuffer data = new StringBuffer();

            try {
                FileOutputStream fos = new FileOutputStream(outputFile);
                // Get the workbook object for XLSX file
                System.out.println("working......1");
                XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
                // Get first sheet from the workbook
                System.out.println("working......2");
                XSSFSheet sheet = wBook.getSheetAt(0);
                Row row;
                Cell cell;
                // Iterate through each rows from first sheet
                Iterator<Row> rowIterator = sheet.iterator();

                while (rowIterator.hasNext()) {
                    row = rowIterator.next();

                    // For each row, iterate through each columns
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {

                        cell = cellIterator.next();

                        switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_BOOLEAN:
                                data.append(cell.getBooleanCellValue() + ",");

                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                data.append(cell.getNumericCellValue() + ",");

                                break;
                            case Cell.CELL_TYPE_STRING:
                                data.append(cell.getStringCellValue() + ",");
                                break;

                            case Cell.CELL_TYPE_BLANK:
                                data.append("" + ",");
                                break;
                            default:
                                data.append(cell + ",");

                        }
                    }
                }

                fos.write(data.toString().getBytes());
                fos.close();

            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }

    public static void main(String[] args) {
        File inputFile = new File("/home/raptorjd4/Desktop/ToConsult.xlsx");
        //writing excel data to csv 
        File outputFile = new File("/home/raptorjd4/Desktop/RaptorTrackingSystem/ToConsult.csv");
        xlsx(inputFile, outputFile);

    }

}

But i am getting output, 但是我正在得到输出,

Working......1 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 工作中...... 1 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException

My jar in lib folder, 我的jar在lib文件夹中,

poi-3.5-FINAL.jar poi-3.5-FINAL.jar

poi-ooxml-3.11.jar poi-ooxml-3.11.jar

why i am getting this error when i mapped all needed jar file in lib folder. 为什么当我在lib文件夹中映射所有需要的jar文件时出现此错误。

Where am i doing mistake? 我在哪里做错了?

For me your code worked just fine, below are the dependencies I added:- 对我来说,您的代码工作得很好,以下是我添加的依赖项:-

在此处输入图片说明

The problem is either with the data in your excel or some missing dependency. 问题可能出在Excel中的数据还是缺少一些依赖项。
Ensure that you do have all the necessary dependencies on your classpath. 确保您确实对类路径具有所有必要的依赖关系。

Your problem is this part: 您的问题是这部分:

  • poi-3.5-FINAL.jar poi-3.5-FINAL.jar

  • poi-ooxml-3.11.jar poi-ooxml-3.11.jar

As explained in this Apache POI FAQ entry : 本Apache POI常见问题解答条目所述

Can I mix POI jars from different versions? 我可以混合不同版本的POI罐子吗?

No. This is not supported. 否。不支持此功能。

All POI jars in use must come from the same version. 所有使用中的POI罐子必须来自同一版本。 A combination such as poi-3.11.jar and poi-ooxml-3.9.jar is not supported, and will fail to work in unpredictable ways. 不支持诸如poi-3.11.jar和poi-ooxml-3.9.jar之类的组合,它们将无法以不可预测的方式工作。

You must ensure that all of your Apache POI jars come from the same version! 必须确保所有Apache POI jar都来自相同版本!

Switch your jars to be from the same version, ideally the latest, and you should be good 将您的jar切换为相同版本,最好是最新版本,您应该会做得很好

I solved this problem by adding below jar files, 我通过添加以下jar文件解决了这个问题,

poi-3.9.jar

poi-ooxml-3.9.jar

poi-ooxml-schemas-3.9-20121203.jar

xmlbeans-2.3.0.jar

dom4j-1.6.1.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM