Solved

Set colspan, rowspan and bgcolor in excel by php?

Posted on 2004-09-20
5
5,059 Views
Last Modified: 2013-12-13
Hi, actually, i want my table in html can be view in excel or cvs. i have done view in excel by cvs format, but i got any problem.

can the excel view is same as in html browser? in html, theres bgcolor and colspan and rowspan. how to do that in excel?

how to set colspan, rowspan and bgcolor in excel as same as in html? and then how to set width coloum is flexible by the text length using script php?

thanks
0
Comment
Question by:abdi1
  • 2
5 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Sorry but you cannot do this with CSV. When Excel reads a CSV file, it only works with the data, and doesn't care about anything related to formatting. It doesn't export/save formatting in CSV and it doesn't read any formatting from CSV files, either.

If you absolutely need to do this, your best bet is to try to have PHP use a Windows COM object to create an Excel spreadsheet (the Linux version of PHP cannot use COM objects). The COM object should have methods and functions that will let you format the spreadsheet and save it as a .XLS file. However, I haven't had very much luck with PHP and COM objects before, so good luck. Here's some links to help:

PHP's COM Object Reference
http://us2.php.net/manual/en/ref.com.php

Excel.Application COM Object Reference
http://msdn.microsoft.com/library/en-us/dv_wrcore/html/wrgrfexcelapplicationobject.asp

Again, good luck!

- J
0
 
LVL 9

Accepted Solution

by:
techtonik earned 55 total points
Comment Utility
Use PEAR Spreadsheet_Excel_Writer class. There's an introductory tutorial about how to use it.
http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php

There are also some commercial scripts available to create Excel files on the fly. For example:
http://www.web-aware.com/biff/biffmanual.htm?id=10&name=biffmanual.zip

Either of these probably do not have facility to automatically convert tables to Excel  files, so you will have to convert them yourself or ask for support. =)



Some more info from Jens Bierkandt on http://www.codecomments.com/PHP_Language/message269479.html (though I haven't tested it):
---
Re: Writing a XLS (Excel) file with PHP

Try to export an Excel Sheet with the needed things as an HTML file.
Open it and copy the details to a PHP file. Fill the values dynamically
with PHP and set the header in the PHP-Script accordingly. Excel will
then open it.
---
Header is cal to this function before any browser output header("Content-Type: application/vnd.ms-excel");



Here is another post from deleted (unfortunately) user notes on header function in PHP manual:
---
andrej at dairyweb dot com dot au
19-Sep-2003 02:02
while looking for a solution to easily convert HTML->excel spreadsheets, while re-using as much of the existing template system as possible.

i stumbled across a nifty little trick with headers that does the job nicely. (note: i have only tested with IE 5.5+). Just call the headers before the HTML that you want to be rendered into a excel document, and there u go

example:
--------[snip]--------
<?php
 header("Content-type: application/vnd.ms-excel");
 header("Content-Disposition: attachment; filename=\"test.xls\"");
?>
<table border="1" cellpadding="3" cellspacing="1">
  <tr>
    <td>CustomerID</td>
    <td>CompanyName</td>
    <td>ContactName</td>
  </tr>
</table>

words words words
--------[snip]--------

hope it helps someone, as it saved me doing it with an external excel writer library
--
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
I stand corrected - that's a very useful tip, techtonik. I've asked this several times in the past and kept getting the same answer that you can't do it. I will have to try it out (woohoo - new toys!)  :)

- J
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now