Solved

Set colspan, rowspan and bgcolor in excel by php?

Posted on 2004-09-20
5
5,109 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is this error or Notice in php error log? 6 32
PHP: concatenate query 12 38
echo time from sql to input type="time" 9 17
using php variable inside javascript 5 13
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

20 Experts available now in Live!

Get 1:1 Help Now