1

I using opencsv library in java and export csv. But i have problem. When i used string begin zero look like : 0123456 , when i export it remove 0 and my csv look like : 123456. Zero is missing. I using way :

"\"\t"+"0123456"+ "\""; but when csv export it look like : "0123456" . I don't want it. I want 0123456. I don't want edit from excel because some end user don't know how to edit. How to export csv using open csv and keep 0 begin string. Please help

1
  • In order to reproduce your issue it would be helpful if you could post a complete minimal code example of how you generate the csv, how the output looks like (not in Excel, but in the csv) and how you expect it to look like. Commented Jun 8, 2019 at 6:39

3 Answers 3

1

I think it is not really the problem when generating CSV but the way excel treats the data when opened via explorer.

Tried this code, and viewed the CSV in a text editor ( not excel ), notice that it shows up correctly, though when opened in excel, leading 0s are lost !

CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"));
    // feed in your array (or convert your data to an array)
    String[] entries = "0123131#21212#021213".split("#");
    List<String[]> a = new ArrayList<>();
    a.add(entries);
    //don't apply quotes
    writer.writeAll(a,false);
    writer.close();

See correct leading 0s being retained in text editor

If you are really sure that you want to see the leading 0s for numeric values when excel is opened by user, then each cell entry be in format ="dataHere" format; see code below:

 CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"));
        // feed in your array (or convert your data to an array)
        String[] entries = "=\"0123131\"#=\"21212\"#=\"021213\"".split("#");
        List<String[]> a = new ArrayList<>();
        a.add(entries);
        writer.writeAll(a);
        writer.close();

This is how now excel shows when opening excel from windows explorer ( double clicking ):

Excel displays with leading 0s

But now, if we see the CSV in a text editor, with the modified data to "suit" excel viewing, it shows as :

enter image description here

Also see link : format-number-as-text-in-csv-when-open-in-both-excel-and-notepad

Sign up to request clarification or add additional context in comments.

Comments

0

have you tried to use String like this "'"+"0123456". ' char will mark number as text when parse into excel

1 Comment

Thanks you. I have edit answer. I using string like this but it not work
0

For me OpenCsv works correctly ( vers. 5.6 ). for example my csv file has a row as the following extract:

 "999739059";;;"abcdefgh";"001024";

and opencsv reads the field "1024" as 001024 corretly. Of course I have mapped the field in a string, not in a Double.

But, if you still have problems, you can grab a simple yet powerful parser that fully adheres with RFC 4180 standard: mykong.com

Mykong shows you some examples using opencsv directly and, in the end, he writes a simple parser to use if you don't want to import OpenCSV , and the parser works very well , and you can use it if you still have any problems.

So you have an easy-to-understand source code of a simple parser that you can modify as you want if you still have any problem or if you want to customize it for your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.