如何将 xlsx (Microsoft Excel) 文件转换为 csv?

问题描述 投票:0回答:3

目前我正在使用下面的代码使用 Java 将 XLSX 文件转换为 CSV。我需要一个更快的解决方案,因为这太慢了。

public class Test1 {
    static void convert(File inputFile, File outputFile) {
        try {
            FileOutputStream fos = new FileOutputStream(outputFile);
            // Get the workbook object for XLSX file
            XSSFWorkbook wBook = new XSSFWorkbook(
                    new FileInputStream(inputFile));
            // Get first sheet from the workbook
            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();
        }
    }

    // testing the application

    public static void main(String[] args) {
        // reading file from desktop
        File inputFile = new File("D:\\Test.xlsx");
        // writing excel data to csv
        File outputFile = new File("D:\\Test1.csv");
        convert(inputFile, outputFile);
    }
}
java excel csv apache-poi xlsx
3个回答
8
投票

文本提取器将转储整个工作簿的 TSV。性能取决于所选的实现和您的内存可用性。

然后您可以将其通过管道传输到

CSVPrinter
以获得正确的 CSV 输出。我不认为 Excel 单元格可以包含制表符,所以这应该是安全的。如果您的单元格中有换行符,我不确定 TSV 输出是否有效,但如果是,您可以使用
CSVParser
来读取它,而不是
lines()

XSSFWorkbook input = new XSSFWorkbook(new File("input.xlsx"));
CSVPrinter output = new CSVPrinter(new FileWriter("output.csv"), CSVFormat.DEFAULT); 

String tsv = new XSSFExcelExtractor(input).getText();
BufferedReader reader = new BufferedReader(new StringReader(tsv));
reader.lines().map(line -> line.split("\t").forEach(output::printRecord);

如果将整个事件缓冲为

String
效率太低,请复制基于事件的提取器之一的实现并直接写入
CSVPrinter


1
投票

使用 apache POI 将 xls/xlsx 转换为 csv 的简单方法。

public class XLSXToCSVConverter {

    public InputStream convertxlstoCSV(InputStream inputStream) throws IOException, InvalidFormatException {

        Workbook wb = WorkbookFactory.create(inputStream);

        return  csvConverter(wb.getSheetAt(0));
    }

    private InputStream csvConverter(Sheet sheet) {
        Row row = null;
        String str = new String();
        for (int i = 0; i < sheet.getLastRowNum()+1; i++) {
            row = sheet.getRow(i);
            String rowString = new String();
            for (int j = 0; j < 3; j++) {
                if(row.getCell(j)==null) {
                    rowString = rowString + Utility.BLANK_SPACE + Utility.COMMA;
                }
                else {
                    rowString = rowString + row.getCell(j)+ Utility.COMMA;
                }
            }
            str = str + rowString.substring(0,rowString.length()-1)+ Utility.NEXT_LINE_OPERATOR;
        }
        System.out.println(str);
        return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
    }
}

希望这对您有帮助,谢谢。


0
投票

尝试使用 POI API。这是参考链接

Java - Apache POI - 将 XLS/XLSX 转换为 CSV

© www.soinside.com 2019 - 2024. All rights reserved.