how to write to csv with a leading zero?

2020-02-15 05:24发布

I wish to write to a CSV file some data. One of the column that I input the information is in the form of an integer with leading zeros, example: 0000000013.

For some reason, excel 'converts' it to 13, and I must have it with 10 digits (0000000013). Can anyone tell how this can be circumvented?

String value = "0000000013"; //I tried String.format("%8d", 13) doesn't work
printWriter.println(value);

Thanks!!!

6条回答
仙女界的扛把子
2楼-- · 2020-02-15 05:40

Add a single quote, ', at the start of the line. This informs Excel to not treat the value as a number.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-15 05:45

When one opens a CSV file in excel 2007, it opens the Text Import Wizard. In step 3, you can specify the column data format of the column to be 'Text'. Just select the appropriate column header in the 'Data preview' pane. Then select the 'Text' radio button in the 'Column data format' pane.

查看更多
手持菜刀,她持情操
4楼-- · 2020-02-15 05:45

You should use:

String.format("%08d", 13);
查看更多
干净又极端
5楼-- · 2020-02-15 05:56

Even better way is to print your number as ="0000000013"

String value = "0000000013"; 
printWriter.println("=\"" + value + "\"");
查看更多
We Are One
6楼-- · 2020-02-15 06:05

Append tab to the number

ex: "\t"+"01234"

查看更多
Juvenile、少年°
7楼-- · 2020-02-15 06:06

Try:

String.format("%08d", 13)

Note the 0 in the format string, that tells Java to prefix the number with zeroes up to the length indicated in the field (8 in this case).

查看更多
登录 后发表回答