gpt4 book ai didi

java - 使用java apache poi在列CSV文件中获取中断后的字符串

转载 作者:行者123 更新时间:2023-12-02 10:21:52 24 4
gpt4 key购买 nike

我正在使用此代码将 CSV 转换为 XLSX

try {
String home = System.getProperty("user.home");
File file = new File(home+"/Downloads/test.csv");
File file1 = new File(home+"/Downloads/test1.xlsx");
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("sheet1");
String currentLine=null;
int RowNum=0;
BufferedReader br = new BufferedReader(new FileReader(file));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
RowNum++;
XSSFRow currentRow=sheet.createRow(RowNum);
for(int i=0;i<str.length;i++){
System.out.println(str[i]+"/n");
currentRow.createCell(i).setCellValue(str[i]);
}
}

FileOutputStream fileOutputStream = new FileOutputStream(file1);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage()+"Exception in try");
}

CSV 文件中的一列使用此 str.length 有一个换行符,它采用单独的单独行,我想采用整个列值。

CSV to XLSX

我想获取整个列值并将其写入 XLSX 文件中。

最佳答案

您似乎有一个 CSV 根据 RFC4180 。有Definition of the CSV Format状态:

  1. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

      "aaa","b CRLF
    bb","ccc" CRLF
    zzz,yyy,xxx

这比简单的 CSV 更复杂,并且不能简单地逐行读取,因为并非每个换行都意味着一条新记录。尝试寻找支持 RFC4180 的 CSV 解析器。

opencsv将会是这样的。

示例:

CSV.csv:

Field1,Field2,Field3
123,This is test column.,345
678,"This is test column.
This is next line",910
123,This is test column.,345

代码:

import java.io.FileOutputStream;
import java.io.FileReader;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.opencsv.CSVReader;

class ParseCSVToExcel {

public static void main(String[] args) throws Exception {

try (XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream("Excel.xlsx");
FileReader in = new FileReader("CSV.csv")) {

CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setWrapText(true);

Sheet sheet = workbook.createSheet("FromCSV");
Row row = null;
Cell cell = null;
int r = 0;
int maxC = 0;

CSVReader reader = new CSVReader(in);
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
row = sheet.createRow(r++);
int c = 0;
for (String field : nextLine) {
cell = row.createCell(c++);
cell.setCellValue(field);
cell.setCellStyle(cellStyle);
}
if (c > maxC) maxC = c;
}

for (int c = 0; c < maxC; c++) {
sheet.autoSizeColumn(c);
}

workbook.write(out);

}
}
}

结果:

enter image description here

<小时/>

使用Apache Commons CSV将是另一种可能性。

与上面相同的CSV.csv

代码:

import java.io.FileOutputStream;
import java.io.FileReader;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.CSVFormat;

class ParseCSVToExcelApacheCommonsCSV {

public static void main(String[] args) throws Exception {

try (XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream("Excel.xlsx");
FileReader in = new FileReader("CSV.csv")) {

CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setWrapText(true);

Sheet sheet = workbook.createSheet("FromCSV");
Row row = null;
Cell cell = null;
int r = 0;
int maxC = 0;

for (CSVRecord record : CSVFormat.RFC4180.parse(in)) {
row = sheet.createRow(r++);
int c = 0;
for (String field : record) {
cell = row.createCell(c++);
cell.setCellValue(field);
cell.setCellStyle(cellStyle);
}
if (c > maxC) maxC = c;
}

for (int c = 0; c < maxC; c++) {
sheet.autoSizeColumn(c);
}

workbook.write(out);

}
}
}

与上面的结果相同。

关于java - 使用java apache poi在列CSV文件中获取中断后的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54288403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com