This is a question that **haunts** me at work. Some of what I do is online reporting, and these reports have to be printable, and printing from a browser has always proven to be a hit or miss proposition.
The good news? If your users are willing to view your page in IE, and have Excel installed on their computers, then you are in business.
The process I'm about to describe is fairly simple, and here's how it works --
Here we go -- this is a very simplified version, but can be scaled on up to however big you wish the table to be.
<table name=listTable id=listTable> <tr> <td>This will be what gets printed</td> </tr> </table>
Notice that I put for the url in the window.open() statement, 'outputFile.xls'. That is the name of your pre-formatted Excel file that contains all of your formatting. I'll get to how to set that up in a second.
Now, you need to add a special 'print' button to your page that will look more or less like this:
Let me take just a second for the sake of completeness and tell you that in order to set up the Excel file in the first place, just save an Excel file as the pre-determined name and place it in your web directory. The first time it's used here, it will be just a plain old file. Then, visit your page, and press the print button. That will open up your file for you.
Once it's open, paste in the contents of your clipboard, and start changing column widths, row heights, print margins, headers, footers, etc...
Print it off and see how it looks. Continue this process until it's perfect. Once you have it perfect, save it (with the content). Now that you have that, go in and delete all the content from the workbook, including any table borders, merged cells, background coloring, etc... All that type of formatting will come in with each new pasting of content.
So now what you have is an Excel sheet with all your printing options set up to the T... nothing more, nothing less. Resave the file under the same name.
Your file is now ready for use by your users, and the headaches of browser printing is a thing of the past.
Happy Coding! Paul Prewett
This method was actually proposed to me by member RobSchultz a while ago in the vbScript forum, and I have been refining it for a while. Thanks, RobSchultz. This method has saved me ALOT of headaches.