Formatting Excel files using HTML | KEVIN RESCHENBERG 06-06-2007 |
Excel-ready files (including .CSV and other variants) are among the most
common output types. Developers frequently ask how to add formatting (bolding,
colors, etc.) to these files. For .CSV files, the answer is easy: You can't.
However, there are ways to create files that contain
formatting. I previously pointed to two methods (SYLK and XML)
here.
Then the other day someone mentioned that Excel reads HTML files. Well, of
course! Why wouldn't it, if it can read XML? The bigger question is, after having
done previous research on Excel formatting, why hadn't I ever thought of trying HTML?
It was one of those forehead-slapping moments.
HTML is easy, but it turns out that the HTML you need for Excel is even easier
than for a web page. It doesn't need the <HEAD>, <BODY> or other tags. You can get
by with just a <TABLE> because, well, that's the sort of thing Excel wants to
present anyway.
<table>
<tr>
<td>1.23</td>
<td>4.56</td>
<td style="width: 8em;">=sum(A1..B1)</td>
</tr>
<tr>
<td colspan="3"
style="color: red; background-color: yellow;">Styled</td>
</tr>
</table>
And that's it. Try it. Copy the snippet above and save it to a new file with the
.XLS extension, and then open it in Excel.
Without using an alternative method (such as the XML format), you can't create
multiple worksheets in one file. But other than that, the HTML method is an extremely
flexible formatting method that can be handled easily in SQR. Just create your file with the .XLS extension
and send it to your users. (But watch out—they may want you to go back and format all
of the other files your programs produce.)
|