browse by category or date

We have an ancient system in combo of classic ASP (VBA) and Allaire JRun which just refuse to die 🙂 Recently I was tasked to add a report that generate Excel document.

Did you know that we can simply provide a HTML format and disguise it as XLS document. With that, Excel (2003 and above) will be able to open it?

In the beginning of the asp file, we set the response header:

Response.AddHeader "content-disposition","attachment; filename=Report.xls"
Response.ContentType = "application/"

Next how do we format the data correctly? We can do that with a little bit of CSS:

.number {
.longdate {
  mso-number-format:"dd\-MMM\-yyyy HH:mm";

If you want more formats, you can always open Excel, right-click any cell, then click Format Cells.


Cool right? Frankly, I only know this trick today 😀

To use it, we simply assign the CSS class to the td element:

dim Dim strconn
Set strconn = Server.CreateObject("ADODB.Connection")

'ODBC Connection to Oracle

Dim sql
sql = ".... Some SQL query ......"
Set rs = Server.CreateObject ( "ADODB.RecordSet" )
rs.Open sql, strconn, adLockOptimistic

While not rs.EOF
	<tr align=center> 
		<td class="text"><%=rs(0)%></td>
		<td class="number"><%=rs(2)%></td>      
		<td class="number"><%=rs(9)%></td>
		<td class="number"><%=rs(10)%></td>
		<td class="longdate"><%=rs(11)%></td>
		<td><%=rs(14) %></td>
GD Star Rating
How to Correctly Format Data in HTML Excel Document, 3.0 out of 5 based on 1 rating

About Hardono

Hi, I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

No Comment

Add Your Comment