Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep format within HTML converted to Excel

I'm working with an HTML table, that contains numbers (formated) and when I export this to xls file (just change extension... hehe) I loss some of the formated data.

Example:

in html I have " 1,000.00 | 500.00 | 20.00 " and in excel it shows like: "1,000.00 | 500 | 20"

I want it to know if it is possible to show the very same format as in html.

THankyou :P

like image 292
pojomx Avatar asked Nov 06 '22 12:11

pojomx


2 Answers

you can achive that by using class. for example:

first add class

writer.WriteLine("<style> .number{mso-number-format:\"\\#\\#0\\.00\";} </style>");

and then, in your iteration :

writer.Write("<td class=\"number\" >");
writer.Write(data);
writer.WriteLine("</td>");

as shown in : Export to Excel in ASP.NET, issue with decimal formatting for numbers greater than 1000

like image 113
arun.m Avatar answered Nov 11 '22 05:11

arun.m


I've done this. The best way to tell is to create an .xls file (not .xlsx) and then save it as an html file.

Then look at the source of the html file. You'll see some css classes at the top and then if you look at the data below you'll see them being applied to the sheet.

So just a bit of reverse engineering...

FYI - if you try to open this up in 2007 or later, you'll get an initial warning but then all works OK.

like image 41
jhorback Avatar answered Nov 11 '22 05:11

jhorback