使用Apache POI将xlsx转换为csv的数据问题

时间:2019-03-20 22:31:09

标签: java csv apache-poi xlsx

我正在使用Apache Poi将我的xlsx文件转换为csv。我的xlsx文件中的第一列在excel中被标记为“常规”。此列包含诸如“ 3505123665”的值。始终为10位数,有时可能带有结尾的零“ 1234500000”。

我的XlsxToCsv.java类代码是:

public class XlsxToCsv {
public XlsxToCsv() {
}

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

    try {
        try {
            FileOutputStream fos = new FileOutputStream(outputFile);
            // Get the workbook object for XLSX file
            FileInputStream fis = new FileInputStream(inputFile);
            Workbook workbook = null;

            String ext = FilenameUtils.getExtension(inputFile.toString());

            if (ext.equalsIgnoreCase("xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (ext.equalsIgnoreCase("xls")) {
                workbook = new HSSFWorkbook(fis);
            }

            // Get first sheet from the workbook

            int numberOfSheets = workbook.getNumberOfSheets();
            Row row;
            Cell cell;
            // Iterate through each rows from first sheet

            for (int i = 0; i < numberOfSheets; i++) {
                Sheet sheet = workbook.getSheetAt(0);
                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 BOOLEAN:
                                data.append(cell.getBooleanCellValue() + ",");

                                break;
                            case NUMERIC:
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                    data.append(dateFormat.format(cell.getDateCellValue()) + ",");
                                } else {
                                    data.append(cell.getNumericCellValue() + ",");
                                }
                                break;

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

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

                        }
                    }
                    data.append('\n'); // appending new line after each row
                }

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

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
        return "\n Conversion of " + inputFile + "\n to flat file: "
                + outputFile + " is completed";
    } catch (Exception ioe) {
        System.out.println(ioe);
        return "Conversion of " + inputFile + " was not successful";
    }
  }
}

在我的csv文件中,第一列值“ 3505123665”变为“ 351E + 09”。

如何防止它执行以下两项操作:

1)将文件转换为csv时,在excel中将其转换为科学计数法类型

2)将数字误认为是“ 351E + 09”是“ 3510000000”。

我的目标:将在特定文件夹中提供一个xlsx文件。我需要将其转换为csv,并在JAVA中阅读。

我的xlsx的所有其他字段正确地转换到csv文件中,除了第一列中将数字存储为excel中的“常规”(我认为常规单元格类型是字符串?)。

希望对此有所帮助。谢谢。

0 个答案:

没有答案