问题
I try to export data out of my Microsoft SQL Server 2008 Database to an Excel Spreadsheet.
I am using this tutorial: http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8
My header looks like this:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename.xls ");
header("Content-Transfer-Encoding: binary ");
The XLS Data Cell looks like this:
xlsBOF();
xlsWriteString(0,0,"$lng_report_age_title");
xlsWriteString(0,4,"$lng_report_sex_title");
xlsWriteString(0,6,"$lng_report_referredfrom_title");
xlsWriteString(0,8,"$lng_report_selfreferred_title");
xlsWriteString(1,0,"$lng_report_age04");
xlsWriteString(1,1,"$lng_report_age514");
xlsWriteString(1,2,"$lng_report_age1549");
xlsWriteString(1,3,"$lng_report_age50");
xlsWriteString(1,4,"$lng_report_sexm");
xlsWriteString(1,5,"$lng_report_sexf");
xlsWriteString(1,6,"$lng_report_referredfrom_indistrict");
xlsWriteString(1,7,"$lng_report_referredfrom_outdistrict");
xlsWriteString(1,8,"$lng_report_selfreferred_indistrict");
xlsWriteString(1,10,"$lng_report_selfreferred_outdistrict");
xlsWriteString(2,6,"$lng_report_referredfrom_new");
xlsWriteString(2,7,"$lng_report_referredfrom_new");
xlsWriteString(2,8,"$lng_report_selfreferred_new");
xlsWriteString(2,9,"$lng_report_selfreferred_old");
xlsWriteString(2,10,"$lng_report_selfreferred_new");
xlsWriteString(2,11,"$lng_report_selfreferred_old");
xlsWriteNumber(3,0,"$searchresultsage04");
xlsWriteNumber(3,1,"$searchresultsage514");
xlsWriteNumber(3,2,"$searchresultsage1549");
xlsWriteNumber(3,3,"$searchresultsage50");
xlsWriteNumber(3,4,"$searchresultssexm");
xlsWriteNumber(3,5,"$searchresultssexf");
xlsWriteNumber(3,6,"$searchresultsreferredfrom01");
xlsWriteNumber(3,7,"$searchresultsreferredfrom02");
xlsWriteNumber(3,8,"$searchresultsselfreferred03");
xlsWriteNumber(3,9,"$searchresultsselfreferred04");
xlsWriteNumber(3,10,"$searchresultsselfreferred05");
xlsWriteNumber(3,11,"$searchresultsselfreferred06");
xlsEOF();
exit();
And the create XLS functions look like this:
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteString( $Row , $Col , $Value ) {
$L = strlen( $Value );
echo pack( "sssss" , 0x204 , 8 + $L , $Row , $Col , 0x0 , $L );
echo $Value;
return;
}
Problem Nr. 1: In the Excel Sheet there will be several unwanted special characters around the text that I want to display (Maybe because I'm using UTF-8? (tried to utf8_decode the string first; didn't help)Maybe because I'm using Excel 2007?)
Problem Nr. 2: The Values are not getting written into the row/column I want them to be in.
The result looks like this (the special chars get automatically removed by the stackoverflow editor, so I replace them with Xs):
XXXXXXXAgeXXXXXSexXXXX
Referred FromXXXX
Self ReferredXXX 0-4 YearsXXX
5-14 YearsXXXX 15-49 YearsXXX X
=50 YearsXXX XXMaleXXX XXFemaleXXXX XXIn DistrictXXX XXOut DistrictXXXX XXIn DistrictXXX
XOut DistrictXXXXXXNewXXXXXXNewXXXXXXNewXXXXXOldXXXX
XNewXXXXXXOldXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXX
All this data is in column A and in rows 1-8.
One more point I would like to know, once it works, is: How can I format the cells (Borders, Fonts, etc)?
Thanks in advance.
回答1:
As an indirect solution to your question, I'd recommend the classes at http://phpexcel.codeplex.com/.
I've used it in the past with good results. The learning curve isn't too bad, the documentation is OK, it's a small package, it has good support in the forums, and I'm reasonably sure it can answer everything you asked.
回答2:
I had the same problem.
ob_end_clean(); $objWriter->save('php://output');
This will remove unwanted characters from excel.
来源:https://stackoverflow.com/questions/6448904/php-export-to-excel-results-in-wrong-format-and-unwanted-signs