使用Java Apache POI在列CSV文件中中断后获取字符串

时间:2019-01-21 10:53:49

标签: 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();
        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文件中。

2 个答案:

答案 0 :(得分:2)

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

  
      
  1. 包含换行符(CRLF),双引号和逗号的字段应用双引号引起来。例如:

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

这比简单的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);

  }
 }
}

与上述结果相同。

答案 1 :(得分:-1)

新行将在该字符串值中添加以下代码

    CellStyle cs = wb.createCellStyle();
    cs.setWrapText(true);
    currentRow.createCell(i).setCellStyle(cs);