PHP database Excel export
I am using a PHP script to export data from MySQL into Excel. The first row of the Excel sheet is a column heading. I want them to appear in bold. How do I do this? I am using the following code:
$con = mysql_connect("localhost","admin","password");
if (!$con)
{
die('Could not connect to DB: \n' . mysql_error());
}
mysql_select_db("ALNMSI", $con);
$result = mysql_query("SELECT * FROM survey1");
$filename = "alnmsi_" . date('d-m-Y') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: applic开发者_如何学JAVAation/vnd.ms-excel"); $flag = false;
$flag = false;
while($row = mysql_fetch_array($result))
{
if(!$flag)
{
data_keys();
$flag = true;
}
array_walk($row, 'cleanData');
data_array($row);
}
You should use a library to generate Excel files.
- PHPExcel : http://phpexcel.codeplex.com/
- Spreadsheet_Excel_Writer : http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.intro.php
I'm assuming you are doing it wrong. Whatever you output it's probably not a valid Excel file, since you didn't mention BIFF (this is the binary format which Excel files would be made of).
If you output TAB or COMMA separated values, then Excel only accidentially opens it correctly. And there is no way to style headers.
Also read:
- PHP to Excel, bold font
- MySQL to Excel generation using PHP
- Format text in Excel file via PHP
- Output Excel file in PHP after echo
精彩评论