java - 使用 apache poi 将 .xls 转换为 csv 文件

标签 java excel csv apache-poi

我已将 .xls 转换为 csv 文件。我的代码运行良好。但我面临一个问题。就像某些单元格值有 (xxx,Md) 这些单元格值应考虑一个值,但采用两列。如何在这里纠正问题,我附上了我的代码。

try
{
    FileOutputStream fos = new FileOutputStream(outputFile);
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
    workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
    HSSFSheet sheet = workbook.getSheetAt(0);

    for(int rowIndex = sheet.getFirstRowNum(); rowIndex <= sheet.getLastRowNum(); rowIndex++)
    {
        Cell cell=null;
        Row row = null;

        int previousCell = -1;
        int currentCell = 0;
        row = sheet.getRow(rowIndex);

        for(int colIndex=row.getFirstCellNum(); colIndex < row.getLastCellNum(); colIndex++)
        {
            cell = row.getCell(colIndex);
            currentCell = cell.getColumnIndex();

            /* Cell processing starts here*/
            System.out.println("coll"+colIndex);

            switch (cell.getCellType())
            {
                case Cell.CELL_TYPE_BOOLEAN:
                    cellDData.append(cell.getBooleanCellValue() + ",");
                    System.out.println("boo"+ cell.getBooleanCellValue());
                    break;

                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell))
                    {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                        String  strCellValue = dateFormat.format(cell.getDateCellValue());
                        cellDData.append(strCellValue +",");
                    }
                    else 
                    {
                        System.out.println(cell.getNumericCellValue());
                        Double value = cell.getNumericCellValue();
                        Long longValue = value.longValue();
                        String strCellValue1 = new String(longValue.toString());
                        cellDData.append(strCellValue1 +",");
                    }
                    break;

                case Cell.CELL_TYPE_STRING:
                    String out=cell.getRichStringCellValue().getString();
                    cellDData.append(cell.getRichStringCellValue().getString() + ",");
                    System.out.println("string"+cell.getStringCellValue());
                    break;

                case Cell.CELL_TYPE_BLANK:
                    cellDData.append("" +",");
                    System.out.print("THIS IS BLANK");
                    break;

                default:
                    break;
            }
        }
    }
}

最佳答案

我可以想到以下两个选项:

  1. 将分隔符从 COMMA 更改为其他内容(也许是 PIPE?)
  2. 将字符串括在引号 ("") 中

示例代码...

case Cell.CELL_TYPE_STRING:
                String out=cell.getRichStringCellValue().getString();
                if(out.contains(",")) {
                    out = "\""+out+"\"";
                }
                cellDData.append(out + ",");
                System.out.println("string"+out);
                break;

P.S: 您可以使用 Strnigbuilder 代替字符串连接

关于java - 使用 apache poi 将 .xls 转换为 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23781826/

相关文章:

Java 中的 Linux 打印

java - 使用java将xml转换为具有特定xml元素的excel

php - 将 MySQL 下载为 CSV 文件时出现问题

java - 没有命名空间的 XML。针对多个 XSD 之一进行验证

java - 使用 Spring MVC 应用程序(没有 web.xml)部署的简单 HelloWorld 给出 404 错误

java - 如何旋转SeekBar上的TextView?

mysql - 将所有数据从 MySQL 数据库导出到电子表格

通过 Jenkins 实现 Python Excel 自动化

powershell - 尝试为 Active Directory 运行 PowerShell 脚本时出现错误解析错误

csv - 从CSV读取会在变量中产生重复的条目