Solved

Set colspan, rowspan and bgcolor in excel by php?

Posted on 2004-09-20
5
5,210 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
ID: 12125175
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
ID: 12131056
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
ID: 12135502
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 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