Menu

Issue when the data contains comma(,).

Help
Hemanth
2014-09-02
2014-09-04
  • Hemanth

    Hemanth - 2014-09-02

    Hi,

    I’m facing issue when the particular field data contains comma(,). Whenever data contains comma, the data after comma is moved to next column(Viewed in MS Excel).

    CODE:

    public class Testing {

    private static final CsvPreference PIPE_DELIMITED = new CsvPreference.Builder(' ', '|', "|\n").build();
    
    public static void main(String arg[]){
        // create the customer Lists (CsvListWriter also accepts arrays!)
        final List<Object> john = Arrays.asList(new Object[] { "1", "John", "Dunbar",
                new GregorianCalendar(1945, Calendar.JUNE, 13).getTime(),
                "1600 Amphitheatre Parkway, Mountain View, CA 94043 United States"});
        final List<Object> bob = Arrays.asList(new Object[] { "2", "Bob", "Down",
                new GregorianCalendar(1919, Calendar.FEBRUARY, 25).getTime(),
                "1601 Willow Rd., Menlo Park, CA 94025 United States"});
    
         ICsvListWriter listWriter = null;
    
        try {
                listWriter = new CsvListWriter(new FileWriter("D:/Data Setup/GSS/Garbage/writeWithCsvListWriter.csv"),
                            PIPE_DELIMITED);
    
                final CellProcessor[] processors = new CellProcessor[] { 
                        new UniqueHashCode(), // customerNo (must be unique)
                        new NotNull(), // firstName
                        new NotNull(), // lastName
                        new FmtDate("dd/MM/yyyy"), // birthDate
                        new NotNull(), // mailingAddress
                };
                final String[] header = new String[] { "customerNo", "firstName", "lastName", "birthDate","mailingAddress" };
    
                // write the header
                listWriter.writeHeader(header);
    
                // write the customer lists
                listWriter.write(john, processors);
                listWriter.write(bob, processors);
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if( listWriter != null ) {
                try {
                    listWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    

    }

    Please help in this regard ASAP.

    Regards,
    Hemanth

     

    Last edit: Hemanth 2014-09-02
  • Kasper B. Graversen

    Your input is wrong not the API. Read the documentation on the site on the CSV format and escaping characters.

     
MongoDB Logo MongoDB