将.csv转换为.xls以获取特定行和列,该值加倍

时间:2016-02-10 16:03:54

标签: java csv

我有以下csv文件,其中包含以下数据,我希望在POI jar的帮助下将其转换为excel文件为2003格式,所以我使用的是poi 3.10版本。

在将其转换为.xls文件时需要特别注意PAYMENT_TYPE列,因为它之间的值包含一个逗号,但不应该将其视为分隔符,它的值以双引号开头,以双引号结束,以便应被视为单个值,因此列PAYMENT_TYPE的位置也在csv文件中固定,它将始终位于第五个位置,因此下面是.csv文件

REC_STATUS,TRADE_ID,SETTLEMENT_DATE,TRADE_EFFECTIVE_DATE,PAYMENT_TYPE,VERSION,BREAK_DOWN_BUCKET,CAUSE,NUM_CASHFLOWS_AFFECTED,PROFILE
Found only in File :B,178942690,01-Feb-16,03-Dec-14,"Coupon",5,NOISY_BREAK_BUCKET,REC_TOOL_ISSUE_PAYMENT_DIRECTION_MISMATCH | REC_TOOL_ISSUE_NOTIONAL_MISMATCH | TRADE_VERSION,1,AVS Offshore
Found only in File :A,197743320,Various,21-Dec-15,"Brokerage Estimated,Upfront Fee",1,ACTUAL DATA BREAK BUCKET,ACTUAL_DATA_BREAK,2,AVS Offshore

现在我有代码将其转换为.xls,如下所示.xls文件的唯一问题是,当我打开.xls文件时,第二行的值不正确,即第五列的值excel是Brokerage Estimated,Upfront Fee然后第六列价值是Upfront Fee,这是不正确的第六列的值是1,请告知我如何在我的下面的代码中纠正这个

package test;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

public class CSVToExcelConverter {

    public static void main(String args[]) throws IOException
    {
        ArrayList arList=null;
        ArrayList al=null;
        String fName = "C:\\Vabc.csv";
        String thisLine;
        int count=0;
        FileInputStream fis = new FileInputStream(fName);
        DataInputStream myInput = new DataInputStream(fis);
        int i=0;
        arList = new ArrayList();
        while ((thisLine = myInput.readLine()) != null)
        {
            al = new ArrayList();
            String strar[] = thisLine.split(",");
            for(int j=0;j<strar.length;j++)
            {
                if(j == 4){
                    al.add(strar[j] + "," + strar[j+1]);
                    j++;
                }
                al.add(strar[j]);
            }
            arList.add(al);
            System.out.println();
            i++;
        }

        try
        {
            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("new sheet");
            for(int k=0;k<arList.size();k++)
            {
                ArrayList ardata = (ArrayList)arList.get(k);
                HSSFRow row = sheet.createRow((short) 0+k);
                for(int p=0;p<ardata.size();p++)
                {
                    HSSFCell cell = row.createCell((short) p);
                    String data = ardata.get(p).toString();
                    if(data.startsWith("=")){
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        data=data.replaceAll("\"", "");
                        data=data.replaceAll("=", "");
                        cell.setCellValue(data);
                    }else if(data.startsWith("\"")){
                        data=data.replaceAll("\"", "");
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        cell.setCellValue(data);
                    }else{
                        data=data.replaceAll("\"", "");
                        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(data);
                    }
//*/
// cell.setCellValue(ardata.get(p).toString());
                }
                System.out.println();
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\test.xls");
            hwb.write(fileOut);
            fileOut.close();
            System.out.println("Your excel file has been generated");
        } catch ( Exception ex ) {
            ex.printStackTrace();
        } //main method ends
    }
}

伙计们请为此提供建议

任何早期的帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

请勿使用split()来解析csv数据,尤其是在某些字段包含逗号的情况下。

 String strar[] = thisLine.split(",");
 for(int j=0;j<strar.length;j++)
 {
     if(j == 4){
         al.add(strar[j] + "," + strar[j+1]);
          j++;
     }
     al.add(strar[j]);
 }

在上面的代码中,行分为,strar的长度会有所不同,具体取决于,中的PAYMENT_TYPE

然后,检查您所在的列号,并始终添加,和下一个值 - 即使PAYMENT_TYPE不包含,

csv数据本身有效且适当引用。

这是一些正确解析它的伪代码:

read a line

bool bInsideQuotes = false

loop over chars
  if character == '"'
    bInsideQuotes = !bInsideQuotes
  if character == ',' and !bInsideQuotes
    found a field separator