当前位置:金屋文档› 将符号分隔的文本文件txt转换为excel的实现

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

文章标签:: excel ios 开发语言 swift
文章摘要: 将符号分隔的文本文件txt转换为excel的实现。

文本文件如下:

现在不好处理,打算将其转换为excel,其中通过冒号分割:line.split(":")
main方法如下:

publicstaticvoidmain(String[] args){String textFilePath ="D:\\zoom\\期刊\\J_Medline\\J_Medline";// 替换为你的文本文件路径String excelFilePath ="D:\\zoom\\期刊\\J_Medline\\output1.xlsx";// 生成的 Excel 文件路径List<String[]> data =newArrayList<>();try(BufferedReader br =newBufferedReader(newFileReader(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 =newXSSFWorkbook()){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 =newFileOutputStream(excelFilePath)){ workbook.write(outputStream);System.out.println("Excel file created successfully: "+ excelFilePath);}}catch(IOException e){ e.printStackTrace();}}

其中去掉了横线----------------------------------------,结果如下:

相关依赖如下:

importorg.apache.commons.lang3.StringUtils;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importjava.io.BufferedReader;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.IOException;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;
<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>
相关文档
  • 文本

  • 符号

  • 分隔

相关文档推荐: