Export from php to excel

Html table generated with php need to be exported with basic fonts and

Page 1 of 1

9 Replies - 76893 Views - Last Post: 01 March 2012 - 02:57 AM Rate Topic: -----

Replies To: Export from php to excel

#2 ldream07  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 09-May 07

Re: Export from php to excel

Posted 09 May 2007 - 12:48 PM

Hi All,
I am a newbie to PHP. I have a html table generated thro' PHP. The bgcolor of this table rows vary depending on the data I get into this table. Now, I am trying to export the data to excel using:

//Header information
$header = "Header1" . "\t";
$header .= "Header2" . "\t";

//Reading the data thro' POST
$data= $_POST['header1']. "\t";
$data .= $_POST['header2'] . "\t";

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=xyz.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; 



My export is successful, but how can I make the styling of table rows reflect in excel. Please help. Thanks in advance

This post has been edited by PsychoCoder: 26 February 2010 - 06:07 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 2

#3 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: Export from php to excel

Posted 11 May 2007 - 05:56 AM

For that you need to know the exact fileformat of excel files.
As it is a propriatry fileformat, i wish you the best of luck.
Was This Post Helpful? 0
  • +
  • -

#4 kumaroleti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-September 09

Re: Export from php to excel

Posted 16 September 2009 - 06:01 AM

Hi,

I also facing same problem if you got the solution plz help me
thanks in advance

regards
kumar
Was This Post Helpful? 0
  • +
  • -

#5 sunuch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 09

Re: Export from php to excel

Posted 04 November 2009 - 12:23 AM

Hi,

Even I am facing the same issue. Pls help!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_chetankhare*


Reputation:

Re: Export from php to excel

Posted 16 February 2010 - 03:05 AM

View Postldream07, on 09 May 2007 - 11:48 AM, said:

Hi All,
I am a newbie to PHP. I have a html table generated thro' PHP. The bgcolor of this table rows vary depending on the data I get into this table. Now, I am trying to export the data to excel using:

<code>
//Header information
$header = "Header1" . "\t";
$header .= "Header2" . "\t";

//Reading the data thro' POST
$data= $_POST['header1']. "\t";
$data .= $_POST['header2'] . "\t";

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=xyz.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
</code>

My export is successful, but how can I make the styling of table rows reflect in excel. Please help. Thanks in advance

Was This Post Helpful? 0

#7 Guest_Jake*


Reputation:

Re: Export from php to excel

Posted 22 February 2010 - 12:52 AM

maybe this is pretty late but here's the solution;

just simply add the DOCTYPE,HTML,BODY + CSS STYLE before your header like the one below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<title>BAND Function</title>
</head>
<style type="text/css">
	body {
		font-family:Verdana, Arial, Helvetica, sans-serif;
		font-size:12px;
		margin:0px;
		padding:0px;
	}
	#atd td{
		padding:3px;
		font-weight:bold;
	}
	#avg_col{
		background-color:#CCFFCC;
	}
	#ctbl, #ctbl td{
		padding:5px;
		border: 1px solid black;
		border-collapse:collapse;
	}
</style>
<html>
<body>
<?
		
			
			$table .= '<table border="0" cellpadding="0" cellspacing="0" id="ctbl"><tr><td>';
			$table .= '<tr id="atd">';
			$table .= '<td rowspan="2" style="background-color:#000099;color:#FFFFFF;">Time</td>';
			$table .= '<td colspan="4" style="background-color:#FFFF33">TN</td>';
			$table .= '<td colspan="4" style="background-color:#FFFF33">CN</td>';
			$table .= '<td rowspan="2" style="background-color:#000099;color:#FFFFFF;padding:0px 5px 0px 5px;">Band<br>Level</td>';
			$table .= '</tr>';
			$table .= '<tr id="atd">';
			$table .= '<td style="background-color:#FFFCCC">OFFERED</td>';
			$table .= '<td style="background-color:#FFFCCC">BAND</td>';
			$table .= '<td style="background-color:#FFFCCC">RUN TIME</td>';
			$table .= '<td style="background-color:#FFFCCC">Abandoned</td>';
			$table .= '<td style="background-color:#FFCC99">OFFERED</td>';
			$table .= '<td style="background-color:#FFCC99">BAND</td>';
			$table .= '<td style="background-color:#FFCC99">RUN TIME</td>';
			$table .= '<td style="background-color:#FFCC99">Abandoned</td>';
			$table .= '</tr>';
			$table .= '<tr>';
			$table .= $display;
			$table .= '</td></tr></table>';
		

		header("Content-type: application/x-msdownload"); 
		# replace excelfile.xls with whatever you want the filename to default to
		header("Content-Disposition: attachment; filename=$fn.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $table;
?>
</body>
</html>


Was This Post Helpful? 0

#8 maryluna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 10

Re: Export from php to excel

Posted 25 February 2010 - 04:14 PM

Hi all, I'm having the same problem and a did what Guest_Jake* said, but the code doesn't work at all, and give the following messages. How can it be solved??? Thanks in advance.

Warning: Cannot modify header information - headers already sent by (output started at /home/mflores/public_html/saepinali/archivoExcel.php:10) in /home/io/public_html/archivoExcel.php on line 57

Warning: Cannot modify header information - headers already sent by (output started at /home/mflores/public_html/saepinali/archivoExcel.php:10) in /home/io/public_html/archivoExcel.php on line 59

Warning: Cannot modify header information - headers already sent by (output started at /home/mflores/public_html/saepinali/archivoExcel.php:10) in /home/io/public_html/archivoExcel.php on line 60

Warning: Cannot modify header information - headers already sent by (output started at /home/mflores/public_html/saepinali/archivoExcel.php:10) in /home/io/public_html/archivoExcel.php on line 61

This post has been edited by maryluna: 25 February 2010 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 maryluna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 10

Re: Export from php to excel

Posted 25 February 2010 - 05:20 PM

Hi all, it's solved, headers most be at the beggining of the file and everything works well, the styles are included, be careful because I was trying in openOffice but doesn't work but if you open the file saved with excel it works just fine.

Thanks and good luck!
Was This Post Helpful? 0
  • +
  • -

#10 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Export from php to excel

Posted 26 February 2010 - 05:27 PM

Export the tabulated data as a CSV file and the import it into Excel. You'll lose formatting, but the data (which is what is of substance) will be preserved.
Was This Post Helpful? 0
  • +
  • -

#11 charles@07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 12

Re: Export from php to excel

Posted 01 March 2012 - 02:57 AM

thanks ldream07

that helped me a lot,

This post has been edited by JackOfAllTrades: 01 March 2012 - 03:59 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1