dhatim / fastexcel

Generate and read big Excel files quickly
Other
647 stars 116 forks source link

Number Format is never automatically set #343

Open desoss opened 8 months ago

desoss commented 8 months ago

Whenever I write an Integer or a Long, the generated Excel cell has a General numbering format. Isn't it possible to generate it as a Number?

Here's the code that I'm using:

Worksheet ws = wb.newWorksheet("Sheet 1");
ws.value(0, 2, 1234);
ws.value(0, 3, 123456L);
ws.value(0, 4, 1.234);

I don't know if it might help or not, but I've decompiled the .xlsx file that I've generated, here's the XML code related to some cells:

<row>
    ....
    <c r="D1" t="n"><v>123456</v></c>
    <c r="E1" t="n"><v>1.234</v></c>
</row>

If I manually change the numbering format of these 2 cells from General to Number the related decompiled XML is:

<row>
    ....
    <c r="D1" s="1"><v>123456</v></c>
    <c r="E1"><v>1.234</v></c>
</row>
holleymcfly commented 4 months ago

Hi desoss, just setting the value doesn't format the cell. You should do something like:

sheet.style(row, col).format("0").set();  // format as number without digits
sheet.style(row, col).format("0.00").set(); // format as number with 2 digits
sheet.style(row, col).format("dd.MM.yyyy").set(); // format as date
sheet.style(row, col).format("0.00%").set(); // format as percent

Does that help?

Best regards, Holger

CamilYed commented 3 months ago

@holleymcfly Could you add more examples about cell formatting to README file?

holleymcfly commented 3 months ago

Hi CamilYed,

I'm not a contributor on the project. Which examples are you looking for?

Best, Holger