• 将符号分隔的文本文件txt转换为excel的实现


    文本文件如下:
    在这里插入图片描述
    现在不好处理,打算将其转换为excel,其中通过冒号分割:line.split(":")
    main方法如下:

     public static void main(String[] args) {
         
            String textFilePath = "D:\\zoom\\期刊\\J_Medline\\J_Medline";  // 替换为你的文本文件路径
            String excelFilePath = "D:\\zoom\\期刊\\J_Medline\\output1.xlsx";  // 生成的 Excel 文件路径
    
            List<String[]> data = new ArrayList<>();
    
            try (BufferedReader br = new BufferedReader(new FileReader(textFilePath))) {
                String line;
                while ((line = br.readLine()) != null) {
                    String[] fields = line.split(":");
                    String strip = StringUtils.strip(Arrays.toString(fields), "[]");
                    if(!strip.equals("--------------------------------------------------------")){
                        data.add(fields);
                    }
    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            try (Workbook workbook = new XSSFWorkbook()) {
                Sheet sheet = workbook.createSheet("Sheet1");
                int rowNum = 0;
    
                for (String[] rowData : data) {
                    Row row = sheet.createRow(rowNum++);
                    int colNum = 0;
                    for (String field : rowData) {
                        Cell cell = row.createCell(colNum++);
                        cell.setCellValue(field);
                    }
                }
                try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
                    workbook.write(outputStream);
                    System.out.println("Excel file created successfully: " + excelFilePath);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    其中去掉了横线----------------------------------------,结果如下:
    在这里插入图片描述
    相关依赖如下:

    import org.apache.commons.lang3.StringUtils;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.12.0</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    UDS知识整理(四):ECU复位——0x11服务
    yolov7训练自己的数据集及报错处理
    python之word文档生成
    Selenium自动化测试 —— 通过cookie绕过验证码的操作!
    Golang协程详解
    [附源码]Python计算机毕业设计Django框架的资产管理系统设计与实现
    美国这几年的人口死亡数据
    6.2 全排列Heap算法
    计算一个日期是一年中的第多天问题(普通、结构体版)
    L2研发工程师—牛客网
  • 原文地址:https://blog.csdn.net/weixin_42260782/article/details/132603333