在java中将csv转换为xls,负责列

时间:2016-02-09 16:45:35

标签: java

我是java开发人员,我想将.csv文件转换为.xls 2003格式,以便将我的csv转换为.xls文件,我的.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 :B,197728700,Various,21-Dec-15,"Coupon,(x20)",2,ACTUAL DATA BREAK BUCKET,ACTUAL_DATA_BREAK,20,AVS Offshore

现在您可以看到.csv文件以逗号分隔,但是对于付款类型下的值,该值类似于"Coupon,(x20)"现在为此值应该被视为单个,因此逻辑应该像第五个那样列,因为索引将从0开始说明,因此列的位置在.csv文件中是固定的,因此对于第五列付款类型的值,如果值为"Coupon,(x20)"则应将其视为单个不分割这个第五列的值对于第五列值逻辑应该从双引号开始并以双引号结束,所以请告诉我如何将.csv转换为.xls,同时处理第五列值

我所尝试的是,如下所示,请告知我如何更正此代码并克服问题

public class CSVToExcelConverter {

public static void main(String args[]) throws IOException
{
ArrayList arList=null;
ArrayList al=null;
String fName = "test.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++)
{
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("test.xls");
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
} catch ( Exception ex ) {
ex.printStackTrace();
} //main method ends
}
}

2 个答案:

答案 0 :(得分:0)

    public class CSVToExcelConverter {

    public static void main(String args[]) throws IOException
    {
         ArrayList arList=null;
         ArrayList al=null;
         String fName = "test.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 == 5){
                       a1.add(strar[j] + "," + strar[j+1]);
                       j++;
                  }
                  al.add(strar[j]);
              }
              arList.add(al);
              i++;
         }
    }
}

答案 1 :(得分:0)

您可以使用opencsv

 CSVReader reader = new CSVReader(
                new FileReader("c:/xxx.csv"), ',', '"', 1);
 // Read all rows at once
 List<String[]> allRows = reader.readAll();

此代码会忽略引号中的分隔符 - , - "并跳过第一行(REC_STATUS,TRADE_ID,SETTLEMENT_DATE ...)。

如果您不需要跳过第一行,只需使用默认值(分隔符 - ,引号 - "

CSVReader reader = new CSVReader(
                    new FileReader("c:/xxx.csv"));
// Read all rows at once
List<String[]> allRows = reader.readAll();