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

时间:2016-03-08 16:48:03

标签: 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),双引号和逗号的字段应括在双引号中。因此,如果单元格数据在添加到String缓冲区之前包含换行符或分隔符(,),则必须格式化单元格数据(数字或字符串或任何其他类型)。请帮助我根据CSV规则格式化单元格数据。

2 个答案:

答案 0 :(得分:1)

使用像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

答案 1 :(得分:0)

Centic的答复完全正确。只是为了扩展他的内容,这是我完整且经过测试的方法,该方法使用 Common CSV 进行实际值打印。不幸的是,我们仍然需要遍历Sheet,XSSF中没有自动CSV输出方法,但是我遵循Centic的策略进行行/单元迭代。

此示例输出到OutputStream,但是显然File同样容易(在FileReader构造函数中使用CSVPrinter)。

// Convert an XSSFWorkbook to CSV and write to provided OutputStream
private void writeWorkbookAsCSVToOutputStream(XSSFWorkbook workbook, OutputStream out) {

    CSVPrinter csvPrinter = null;

    try {
        // Or change this to  File-based constructor, if File output is required
        csvPrinter = new CSVPrinter(new OutputStreamWriter(out), CSVFormat.DEFAULT);                

        if (workbook != null) {
            XSSFSheet sheet = workbook.getSheetAt(0); // Sheet #0
            Iterator<Row> rowIterator = sheet.rowIterator();
            while (rowIterator.hasNext()) {               
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    csvPrinter.print(cell.getStringCellValue()); // Commons CSV prints here
                }
                // Newline after each row
                csvPrinter.println();
            }

        }

    }
    catch (Exception e) {
        log.error("Failed to write CSV file to output stream", e);
    }
    finally {
        try {
            if (csvPrinter != null) {
                // Close CSVPrinter
                csvPrinter.flush();
                csvPrinter.close();
            }
        }
        catch (IOException ioe) {
            log.error("Error when closing CSV Printer", ioe);
        }           
    }
}