0

I have the below csv file named Vabc.csv

REC_STATUS,TRADE_ID,   SETTLEMENT_DATE,     TRADE_EFFECTIVE_DATE,  PAYMENT_TYPE,                        VERSION,      BREAK_DOWN_BUCKET,         CAUSE,                     NUM_CASHFLOWS_AFFECTED,   PROFILE
Found  ,   178942690,  01-Feb-16,            03-Dec-14,             "Coupon",                              5,         NOISY_BREAK_BUCKET,        REC_TOOL_I | REC_TOOL_H ,      1,                     TRADE_ Offshore
Found  ,   197743320,  Various,              21-Dec-15,            "Brokerage Estimated,Upfront Fee",       1,        ACTUAL DATA BREAK BUCKET,ACTUAL_DATA_BREAK,               2,                       AVS Offshore

As you can see in the above csv file while converting it into .xls file special care need to be taken for the column PAYMENT_TYPE as its value in between consist a comma but that should not be consider as delimiter and it value starts with double quotes and end with double quotes so that should be treated as single value.

now the poistion (index of) column PAYMENT_TYPE is also fixed in the csv file that it will be always be at fifth position as you can see n the above csv file ,now i have to convert the above .csv file into .xls 2003 format through java , i am using jdk 1.5

now the column in the .xls should be like this as shown below but the issue is that payment value should be considered as single starting from double quotes and ending with double quotes and for rest the delimiter is comma separated

REC_STATUS,TRADE_ID,   SETTLEMENT_DATE,     TRADE_EFFECTIVE_DATE,  PAYMENT_TYPE,                        VERSION,      BREAK_DOWN_BUCKET,         CAUSE,                     NUM_CASHFLOWS_AFFECTED,   PROFILE
Found  ,   178942690,  01-Feb-16,            03-Dec-14,             "Coupon",                              5,         NOISY_BREAK_BUCKET,        REC_TOOL_I | REC_TOOL_H ,      1,                     TRADE_ Offshore
Found  ,   197743320,  Various,              21-Dec-15,            "Brokerage Estimated,Upfront Fee",      1,        ACTUAL DATA BREAK BUCKET,ACTUAL_DATA_BREAK,                2,                       AVS Offshore

I have designed the following below program which is not generating the .xls in them mentioned above format please advise how can i correct my above program so that it can generate .xls and the columns should be in order as shown in the above .xls sheet , below is my program please advise how to correct it

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

Folks please advise any early help would be appreciated , any solution for it

sss
  • 161
  • 1
  • 1
  • 13

1 Answers1

2

I strongly recommend to use an existing libary to write the Excel Document, for example Apache POI. It has a two compoents

  • HSSF to handle Excel '97(-2007) file format
  • XSSF to handle Excel 2007 OOXML (.xlsx) file format

Both are pure Java implementation, so you do NOT need to have MS-Excel installation.

BTW: instead of writing your own CSV parser, I would recomend to use an existing one, for example:

Psydocode for OpenCSV:

 CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), ',', '"');
 List<String[]> allLines = reader.readAll();     
 ...
 HSSFWorkbook hwb = new HSSFWorkbook();
 HSSFSheet sheet = hwb.createSheet("new sheet");
 ...
 for(String line: allLines) {
     HSSFRow row = sheet.createRow((short)sheet.getLastRowNum());
     for(int colIndex = 0; colIndex < line.lenght; colIndex ++) {
         HSSFCell cell = row.createCell((short) colIndex);
         cell.setCellType(Cell.CELL_TYPE_STRING);
         cell.setCellValue(line[colIndex]);
     }
 }
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks a lot , request you to please show how can i overcome from above mentioned problem Thanks in advance – sss Feb 13 '16 at 14:58
  • @sss: I do not see any probelm? - about which problem are you talking? – Ralph Feb 13 '16 at 15:01
  • From OP's source code it seems that he is already using POI. – rkosegi Feb 13 '16 at 15:03
  • @Ralph well I have posted the code above that is also using POI internally but when i run the above code the .xls file is generated , but the order of second row is get affected it is not coming in order as i expected – sss Feb 13 '16 at 15:04
  • So take my second hint and use an existing CSV parser. If you want to implement your own, then you should have seperate reading/parsing the CSV strongly from Excel Output stuff. I am sure if you do that, then you will find the bug. – Ralph Feb 13 '16 at 15:08
  • @Ralph.. Thanks much appreciated , I h ave opted the second one choice , lets say i am doing this CSVReader reader = new CSVReader( new FileReader("C:\\abc.csv"), ',', '"', ); // Read all rows at once List allRows = reader.readAll() – sss Feb 13 '16 at 15:12
  • now please advise all the data is allRows , so please advise how can i write the data of array allRows into an excel sheet in the same format as shown above – sss Feb 13 '16 at 15:14
  • see my added code -- I really do not see the problem - but maybe I have overlooked something. – Ralph Feb 13 '16 at 16:48