我正在使用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();
答案 0 :(得分:0)
我几天前遇到过同样的问题。实际上你不会导出真正的.xls文件。它仍然是一个扩展名为.xls的文本文件。
所以我建议您使用http://phpexcel.codeplex.com/
创建xls文件100%成功。它还有一个惊人的文档。