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

使用java apache poi在列CSV文件中中断后获取字符串,java,apache-poi,Java,Apache Poi,我正在使用此代码将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()

我正在使用此代码将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将另一行打断。它将使用单独的行。我想使用整个列的值


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

在新行即将出现的地方,为该字符串值添加下面的代码

    CellStyle cs = wb.createCellStyle();
    cs.setWrapText(true);
    currentRow.createCell(i).setCellStyle(cs);
你似乎有一个CSV。这些国家:

包含换行符、双引号和逗号的字段应包含在双引号中。例如:

  "aaa","b CRLF
   bb","ccc" CRLF
   zzz,yyy,xxx
这比简单的CSV更复杂,不能简单地逐行读取,因为不是每个换行都意味着一条新记录。尝试查找支持RFC4180的CSV解析器

将是一个这样的世界

例如:

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
代码:


与上述结果相同。

您好,请尝试解决确切问题的链接。我只使用了该代码,但问题是请参阅附加的屏幕截图。str.length counting,直到这是测试列,并创建一个列写入到该列,然后再写入下一行。我的问题是,应该读取整列值并将整列值写入单列,而不是单独写入两列请了解CSV的内部结构,因为您已经使用逗号将其拆分为一行,如果您想了解更多信息,请在文本板中打开CSV,并查看它是如何用逗号分隔的,正确,但在逗号结束为整列之前,它不会占用整个字符串。请帮我解决这个问题。你想不想你的文本这是测试列:这是单单元格中的下一行或xlsx中的不同单元格中的下一行?我正在动态地这样做,所以我不知道单行是从哪里来的。你可以编辑我的上述代码,以便我更好地理解。在你的代码中,wb指的是工作簿还是什么?很抱歉现在耽搁了,只是我已经运行了你的代码,工作正常。谢谢Axel RichterOne更多疑问我必须删除excel工作表中的第一行我尝试过使用removeRow和shiftRows,但它只删除值,而不是整行。您能让我解决这个问题吗
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);

  }
 }
}
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);

  }
 }
}