好的,我在使用PHPWord类时遇到了困难,这个类可以找到:http://phpword.codeplex.com
奇怪的是,当我在“示例”文件中使用相同的代码时,它可以很好地生成。当我不使用头文件强制下载时,文件会打开得很好。使用此代码的头文件会导致它在下载时在word文件中抛出错误,指出文件已损坏,无法打开,但随后它可以正常打开。
public function export()
{
// Load PHPWORD Library
$this->load->library('PHPWord');
$sec = $this->phpword->createSection($secStyle);
$header = $sec->createHeader();
$header->addWatermark('images/CC_watermark.png', array('marginTop'=>1015, 'marginLeft'=>-80));
$resultSelected = $this->input->post('cbox');
foreach($resultSelected as $row)
{
$sec->addText($row);
echo $row."<br>";
}
$fileName = "Plan_Generate_".date('Ymd').".docx";
// Force Download
$filePath = $fileName;
$fileName = basename($filePath);
// $fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
// header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
// readfile ($filePath);
// exit();
// Save File
// $fileName = "files/SomethingNew_".date("Ymd").".docx";
$objWriter = PHPWord_IOFactory::createWriter($this->phpword, 'Word2007');
$objWriter->save('php://output');
}
这是我在尝试生成文件时使用的代码。我遇到的问题是当它试图强制下载时会抛出一个错误。谢谢你的帮助!如果你不能完全理解问题,就提出问题。
更新:
这是我收到的错误的图像。感谢您的快速响应,实际上我将尝试Codeigniters的方式,Tomm。早上。
发布于 2011-08-31 16:07:27
您应该通过CodeIgniters函数设置标头,这可能是导致问题的原因:
$this->output->set_header();
允许您手动设置服务器标头,输出类将在输出最终呈现的显示时为您发送这些标头。示例:
$this->output->set_header("HTTP/1.0 200 OK");
$this->output->set_header("HTTP/1.1 200 OK");
$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
我的假设是,您正在尝试以PHP方式更新报头,但您是在按照CI规则进行输出。
发布于 2012-10-12 03:36:16
这是通过使用CI的$this->download->force_download()
修复的。
并通过ob_start()
、ob_get_contents()
和ob_end_clean()
为需要帮助的人传递数据。
发布于 2011-08-31 17:10:02
你确定你不想
header("Content-Type: application/octet-stream");
而不只是流?真正的错误是什么?是word、浏览器还是php抛出了这个错误?
此外,CodeIgniter有一个下载助手来发送文件...你可能想试一试。http://codeigniter.com/user_guide/helpers/download_helper.html
https://stackoverflow.com/questions/7258868
复制相似问题