为什么我会收到文件错误:“数据可能已丢失”使用 Microsoft Excel 2007 版本打开 xls 格式的 Excel 文件时?

发布于 2024-11-08 19:02:04 字数 2083 浏览 3 评论 0原文

我正在使用 PHP 生成 Excel 文件。它成功生成了文件,但是当我尝试在 Excel 2007 中打开它时,它显示错误:“数据可能已丢失”。请提出解决建议。

这是我的代码:

 require_once "../php_writeexcel-0.3.0/class.writeexcel_workbook.inc.php";
 require_once "../php_writeexcel-0.3.0/class.writeexcel_worksheet.inc.php";

$fname = tempnam("/tmp", "demo.xls");
$workbook =& new writeexcel_workbook($fname);
$worksheet =& $workbook->addworksheet('Demo');
$worksheet2 =& $workbook->addworksheet('Another sheet');
$worksheet3 =& $workbook->addworksheet('And another');
$worksheet->set_column('A:C', 32);
$heading  =& $workbook->addformat(array(
                                        bold    => 1,
                                        color   => 'blue',
                                        size    => 18,
                                        merge   => 1,
                                        ));

$headings = array('Out Telco Wise Report', '');
$worksheet->write_row('C1', $headings, $heading);

$worksheet->write('A2', "Request From");
$worksheet->write('B2', "Out Telco");
$worksheet->write('C2', "Total");

$i=3;
while($rowres=mysql_fetch_array($result_excel))
{

            $sql3="select ceil(length(msg_body)/160) as no_sms, count(*)as total from outbox   ".$with_out_client." AND request_from='".$rowres['request_from']."' AND out_telco='".                 $rowres['out_telco']."'  group by no_sms";
                 $result2=mysql_query($sql3);
                 $count2=0;
                 while($row2 = mysql_fetch_array($result2))
                   {
                     $count2 =$count2+($row2['total']*$row2['no_sms']);             
                   }

$worksheet->write('A'.$i, $rowres["request_from"]);
$worksheet->write('B'.$i, $rowres["out_telco"]);
$worksheet->write('C'.$i, $count2);
$i++;
}  
$workbook->close();

header("Content-Type:application/vnd.ms-excel; name=\"example-demo.xls\"");
header("Content-Disposition: attachment; filename=\"example-demo.xls\"");
$fh=fopen($fname, "rb");
fpassthru($fh);
unlink($fname);

I am working on generating an Excel file, using PHP. It is successful in generating the file, but when I try to open it in Excel 2007, it shows an error: "Data may have been lost". Please give suggestions to solve it.

Here is my code:

 require_once "../php_writeexcel-0.3.0/class.writeexcel_workbook.inc.php";
 require_once "../php_writeexcel-0.3.0/class.writeexcel_worksheet.inc.php";

$fname = tempnam("/tmp", "demo.xls");
$workbook =& new writeexcel_workbook($fname);
$worksheet =& $workbook->addworksheet('Demo');
$worksheet2 =& $workbook->addworksheet('Another sheet');
$worksheet3 =& $workbook->addworksheet('And another');
$worksheet->set_column('A:C', 32);
$heading  =& $workbook->addformat(array(
                                        bold    => 1,
                                        color   => 'blue',
                                        size    => 18,
                                        merge   => 1,
                                        ));

$headings = array('Out Telco Wise Report', '');
$worksheet->write_row('C1', $headings, $heading);

$worksheet->write('A2', "Request From");
$worksheet->write('B2', "Out Telco");
$worksheet->write('C2', "Total");

$i=3;
while($rowres=mysql_fetch_array($result_excel))
{

            $sql3="select ceil(length(msg_body)/160) as no_sms, count(*)as total from outbox   ".$with_out_client." AND request_from='".$rowres['request_from']."' AND out_telco='".                 $rowres['out_telco']."'  group by no_sms";
                 $result2=mysql_query($sql3);
                 $count2=0;
                 while($row2 = mysql_fetch_array($result2))
                   {
                     $count2 =$count2+($row2['total']*$row2['no_sms']);             
                   }

$worksheet->write('A'.$i, $rowres["request_from"]);
$worksheet->write('B'.$i, $rowres["out_telco"]);
$worksheet->write('C'.$i, $count2);
$i++;
}  
$workbook->close();

header("Content-Type:application/vnd.ms-excel; name=\"example-demo.xls\"");
header("Content-Disposition: attachment; filename=\"example-demo.xls\"");
$fh=fopen($fname, "rb");
fpassthru($fh);
unlink($fname);

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文