String Builder 到 CSV,如何在正确的位置添加分号?

String Builder to CSV, how to add semicolos in right place?

我下载了数据,通过StringBuilder读取得到了这个结果 (我的StringBuilder的内容)

User Manager_ID Date Humidity Temperature Pressure Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8
dt-001 N5_220 09/19/2021 20.0 20.0 1000.0 1 2 3 4 5 1 2 3
dt-002 N5_220 09/25/2021 80.0 30.0 1200.0 4 5 1 2 3 4 5 1

是否有任何方法、库或其他人有解决方案来指定数据之间的分号?当然,行尾除外。 我会很感激你的帮助:) 我想达到的目标:

User; Manager_ID; Date; Humidity; Temperature; Pressure; Q1; Q2; Q3; Q4; Q5; Q6; Q7; Q8
dt-001; N5_220; 09/19/2021; 20.0; 20.0; 1000.0; 1; 2; 3; 4; 5; 1; 2; 3
dt-002; N5_220; 09/25/2021; 80.0; 30.0; 1200.0; 4; 5; 1; 2; 3; 4; 5; 1

代码:

public void download() throws IOException {
        List<FileDTO> tests = testService.getTestFileDtoList(); // from repo
        XSLXManagerTestExporter excelExporter = new XSLXManagerTestExporter(tests );
        XSSFWorkbook workBook = excelExporter.export(); // get XSSFWorkbook
        XSSFSheet selSheet = workBook.getSheet("sheet");
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i <= selSheet.getLastRowNum(); i++) {
            Iterator<Cell> cellIterator = selSheet.getRow(i).cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (sb.length() != 0)
                    sb.append(" ");
                sb.append(cell.getStringCellValue());
            }
        }
        workBook.close();
        writeBytesToFileApache(sb.toString().getBytes());
    }


 private static void writeBytesToFileApache(byte[] bytes)
            throws IOException {
        FileUtils.writeByteArrayToFile(new File(".....\file.csv"), bytes);

    }

这是我的 StringBuilder 的 XLSX 转换器。我想将其保存为 .CSV

您是否考虑过使用 StringJoiner

public void download() throws IOException {
        List<FileDTO> tests = testService.getTestFileDtoList(); // from repo
        XSLXManagerTestExporter excelExporter = new XSLXManagerTestExporter(tests );
        XSSFWorkbook workBook = excelExporter.export(); // get XSSFWorkbook
        XSSFSheet selSheet = workBook.getSheet("sheet");

        List<String> csvLines = new ArrayList<>();
        for (Row row : selSheet) {
            StringJoiner sj = new StringJoiner(";");
            for (Cell cell : row) {
                sj.add(cell.getStringCellValue());
            }
            csvLines.add(sj.toString());
        }
        workBook.close();
        writeBytesToFileApache(csvLines().stream().collect(Collectors.joining("\n")).getBytes());
    }

您可能还想考虑 FileOutputStream 而不是 List(或 String Joiner 或任何其他基本上在内存中构建整个 CSV 并一次性刷新它的解决方案)。它的内存效率应该更高,允许您处理(可能非常)大的 excel 文件。

尝试以下操作:

    public void download() throws IOException {
        List<FileDTO> tests = testService.getTestFileDtoList(); // from repo
        XSLXManagerTestExporter excelExporter = new XSLXManagerTestExporter(tests);
        XSSFWorkbook workBook = excelExporter.export(); // get XSSFWorkbook
        XSSFSheet selSheet = workBook.getSheet("sheet");
        StringBuilder sb = new StringBuilder();
        final String LINESEP = System.getProperty("line.separator");

        for (int i = 0; i <= selSheet.getLastRowNum(); i++) {
            Iterator<Cell> cellIterator = selSheet.getRow(i).cellIterator();
            String sep = "";
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                sb.append(sep).sb.append(cell.getStringCellValue());
                sep = ";";
            }
            sb.append(LINESEP);
        }
        workBook.close();
        writeBytesToFileApache(sb.toString().getBytes());
    }