java - 对于使用 apache POI 转换为 CSV 时的 xlsx 单元格数据

标签 java csv apache-poi supercsv

我正在使用下面的程序将 xlsx 转换为 csv,如果每个单元格字符串包含换行符 (/n) 或分隔符,我想添加引号字符 ("")。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 XlsxtoCSV {

    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
            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("C:\\Users\\user69\\Desktop\\test.xlsx");
        //writing excel data to csv 
        File outputFile = new File("C:\\Users\\user69\\Desktop\\test1.csv");
        xlsx(inputFile, outputFile);
    }
}

根据 RFC4180 Csv 规则。包含换行符 (CRLF)、双引号和逗号的字段应括在双引号中。因此,如果单元格数据在添加到字符串缓冲区之前包含换行符或分隔符 (,),我必须格式化单元格数据(数字或字符串或任何其他类型)。请帮助我根据 CSV 规则格式化单元格数据。

最佳答案

使用像 commons-csv 这样的库:

final Appendable out = ...;  
final CSVPrinter printer = CSVFormat.DEFAULT.withHeader("H1", "H2").print(out);
...
while (rowIterator.hasNext()) {
    ...
    while (cellIterator.hasNext()) {
        ...
        printer.print(cell.getStringCellValue());
        ...
    }
    printer.println();
}

另请参阅简短的 user-guide

关于java - 对于使用 apache POI 转换为 CSV 时的 xlsx 单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873153/

相关文章:

java - HSSF。清除单元格

java - 错误: while trying to format decimal output in Java

java - 使用 PHP 将文件移动到目录

r - R-CSV错误-意外的数字常量

python - 在 Python 中将嵌套的 JSON 转换为 CSV 文件

java - 无法转换为 org.openxmlformats.schemas.xpackage.x2006.digitalSignature.CTRelationshipReference?

java - 如何在 jME3 中以编程方式创建并执行自定义动画?

java - 二叉搜索树实现和java

excel - 在vbscript中读取csv文件

java - 如何通过 Apache POI 设置 Excel Sunburst Chart 中各个数据标签的文本属性?