Java中如何將符號分隔的文本文件txt轉(zhuǎn)換為excel
文本文件如下:

現(xiàn)在不好處理,打算將其轉(zhuǎn)換為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();
}
}其中去掉了橫線----------------------------------------,結果如下:

相關依賴如下:
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;
<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>到此這篇關于將符號分隔的文本文件txt轉(zhuǎn)換為excel的實現(xiàn)的文章就介紹到這了,更多相關txt分隔文件轉(zhuǎn)excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis動態(tài)新增(insert)和修改(update)方式
這篇文章主要介紹了mybatis動態(tài)新增(insert)和修改(update)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
vue+springboot讀取git的markdown文件并展示功能
Markdown-it 是一個用于解析和渲染 Markdown 標記語言的 JavaScript 庫,使用 Markdown-it,你可以將 Markdown 文本解析為 HTML 輸出,并且可以根據(jù)需要添加功能、擴展語法或修改解析行為,本文介紹vue+springboot讀取git的markdown文件并展示,感興趣的朋友一起看看吧2024-01-01
java使用Feign實現(xiàn)聲明式Restful風格調(diào)用
這篇文章主要為大家詳細介紹了java使用Feign實現(xiàn)聲明式Restful風格調(diào)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
elasticsearch集群cluster主要功能詳細分析
這篇文章主要為大家介紹了elasticsearch集群cluster主要功能詳細分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
SpringBoot通過Filter實現(xiàn)整個項目接口的SQL注入攔截詳解
這篇文章主要介紹了SpringBoot通過Filter實現(xiàn)整個項目接口的SQL注入攔截詳解,SQL注入是比較常見的網(wǎng)絡攻擊方式之一,在客戶端在向服務器發(fā)送請求的時候,sql命令通過表單提交或者url字符串拼接傳遞到后臺持久層,最終達到欺騙服務器執(zhí)行惡意的SQL命令,需要的朋友可以參考下2023-12-12

