-2

我正在使用 php 脚本将数据从 Mysql 数据库导出到 Excel 工作表。打开 excel 文件时,我遇到以下错误。错误在 codenovssem.xls 中找到不可读的内容你想恢复这个内容吗工作簿?如果您信任工作簿的来源,请单击是...当我单击是时它显示如下“无法读取文件..”我使用了以下代码

$result=mysql_query("select * from t3diff ");//retrieving records from database
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/x-msexcel;charset=utf-8");
header("Content-Disposition: attachment;filename=codenovsem.xls "); 
header("Content-Transfer-Encoding: binary ");
xlsBOF();
/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, 
both are start       at '0'
*/
xlsWriteLabel(0,0,"student Marks" );
// Make column labels. (at line 3)
xlsWriteLabel(2,0,"codeno");
xlsWriteLabel(2,1,"na");
xlsWriteLabel(2,2,"marks");
xlsWriteLabel(2,3,"subname");
xlsWriteLabel(2,4,"subcode");
xlsWriteLabel(2,5,"branch");
xlsWriteLabel(2,6,"year");
xlsWriteLabel(2,7,"sem");
xlsWriteLabel(2,8,"setno");
xlsWriteLabel(2,9,"bundleno");
xlsWriteLabel(2,10,"afor");
xlsWriteLabel(2,11,"oname");
xlsWriteLabel(2,12,"valdate");
xlsWriteLabel(2,13,"valtime");
$xlsRow =  3;
// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){
xlsWriteNumber($xlsRow,0,$row['codeno']);
xlsWriteNumber($xlsRow,1,$row['na']);
xlsWriteNumber($xlsRow,2,$row['marks']);
xlsWriteNumber($xlsRow,3,$row['subname']);
xlsWriteNumber($xlsRow,4,$row['subcode']);
xlsWriteNumber($xlsRow,5,$row['branch']);
xlsWriteNumber($xlsRow,6,$row['year']);
xlsWriteNumber($xlsRow,7,$row['sem']);
xlsWriteNumber($xlsRow,8,$row['setno']);
xlsWriteNumber($xlsRow,9,$row['bundleno']);
xlsWriteNumber($xlsRow,10,$row['afor']);
xlsWriteNumber($xlsRow,11,$row['oname']);
xlsWriteNumber($xlsRow,12,$row['valdate']);
xlsWriteNumber($xlsRow,13,$row['valtime']);
$xlsRow++;
} 
xlsEOF();//End of Excel file
exit();
4

1 回答 1

0

几天前我遇到了同样的问题。实际上,您不会导出真正的 .xls 文件。它仍然是一个扩展名为 .xls 的文本文件。

所以我建议你使用http://phpexcel.codeplex.com/

创建 xls 文件是 100% 成功的。它还有一个惊人的文档。

于 2013-02-28T10:19:43.413 回答