PHP 导出到 Excel 会导致格式错误和不需要的符号

发布于 2024-11-16 12:58:00 字数 3484 浏览 5 评论 0原文

我尝试将数据从 Microsoft SQL Server 2008 数据库导出到 Excel 电子表格。

我正在使用这个教程: http://www.appservnetwork.com/modules.php ?name=News&file=article&sid=8

我的标题如下所示:

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 ");

XLS 数据单元如下所示:

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();

创建 XLS 函数如下所示:

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;
}

问题编号。 1: 在 Excel 工作表中,我想要显示的文本周围会有几个不需要的特殊字符(也许是因为我使用的是 UTF-8?(尝试先对字符串进行 utf8_decode;没有帮助)也许是因为我使用的是 Excel 2007 年?)

问题编号。 2: 值没有写入我希望它们所在的行/列中。

结果如下所示(特殊字符被 stackoverflow 编辑器自动删除,所以我用 X 替换它们):

XXXXXXX年龄XXXXXX性爱XXXX
推荐自 XXXX
自我推荐XXX 0-4 岁XXX
5-14 岁XXXX 15-49 岁XXX X
=50年XXX XX男XXX XX女XXXX XX内区XXX XX外区XXXX XX内区XXX
X出区XXXXXX新XXXXXX新XXXXXX新XXXXX旧XXXX
X新XXXXXX旧XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXX

所有这些数据都位于 A 列和第 1-8 行中。

一旦它起作用,我想知道的另一点是: 如何设置单元格格式(边框、字体等)?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼前雾蒙蒙 2024-11-23 12:58:00

作为您问题的间接解决方案,我推荐 http://phpexcel.codeplex.com/

我过去曾使用过它并取得了良好的效果。学习曲线还不错,文档还可以,它是一个小包,在论坛中有很好的支持,我有理由相信它可以回答您提出的所有问题。

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.

哎呦我呸! 2024-11-23 12:58:00

我也有同样的问题。

ob_end_clean(); $objWriter->save('php://output');

这将从 Excel 中删除不需要的字符。

I had the same problem.

ob_end_clean(); $objWriter->save('php://output');

This will remove unwanted characters from excel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文