zend framework 导出 下载

diergelicheng 2017-05-12 12:53:05
我这里一个系统,用的是 zend frame 框架和sqlite数据库,本来没问题,后改成mysql数据库,其他正常,就是导出word,excel,以及下载都有问题了,导出的文件和下载的文件都不正常,打不开了
一下是导出word的代码:
/**
* 导出视频评论的word文档
*/
public function exportcomments1Action() {
$vid = $_GET['vid'];
require_once "PHPWord/PHPWord.php";
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$PHPWord->addFontStyle('r1Style', array('color'=>'000000','bold'=>true, 'italic'=>false, 'size'=>12));
$PHPWord->addFontStyle('r2Style', array('color'=>'000000','bold'=>false, 'italic'=>false, 'size'=>11));
$sql = "";
$sql = "select * from videocomment where vid='$vid' and is_active='1' order by time asc";
$comments = $this->db->Execute($sql);

// $comments = D('VideoComment') -> getCommentsByVid($vid);
$sql = "";
$sql = "select * from video_tmp where id='$vid'";
$videoinfo = $this->db->getRow($sql);

// $videoinfo = D('Video') -> getVideoByVid($vid);
$videoname = $videoinfo["title"];
// $videouid = $videoinfo['uid'];

$sql = "";
$videoowner="jkl";
// $videoowner = D('User') -> getDisplayName($videouid);
$header = $section->createHeader();
$header->addText('[专家视频点评汇总]',array('bold'=>false,'size'=>10,'underline'=>single),array('align'=>'right'));
$section->addText('视频编号为'.$vid.'的专家点评汇总', 'r1Style');
$section->addTextBreak(1);
// $section->addText('视频名称:'.$videoname.' '.'上传人:'.$videoowner, 'r1Style');
$section->addText('视频名称:'.$videoname, 'r1Style');
$section->addTextBreak(1);
$table = $section->addTable();
$table->addRow();
// $table->addCell(5000)->addText("点评图片",'r2Style');
$table->addCell(5000)->addText("专家点评",'r2Style');

foreach ($comments as &$c) {
$table->addRow();
// $c['dname'] ="二的";
// $c['dname'] = D('User') -> getDisplayName($c['uid']);
// $table->addCell(5000)->addImage($c['thumb_path'],array('width'=>250, 'height'=>250));
$comment_content = "专家—的点评:".$c['content'];
$table->addCell(5000)->addText($comment_content,'r2Style');

}


// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$filename = '视频评论导出'.$vid.'.docx';//要保存的文件名
$filename=iconv("utf-8","gb2312",$filename);
$filepath = sys_get_temp_dir().'/'.$filename; //要下载的文件地址

$objWriter->save($filepath);
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename);
readfile($filepath);

exit();
}
我把上面代码中和数据库有关的代码都去掉,只随意写几个字母数字进去,导出来也不正常,下载也是一样。估计是zend framework 哪里要设置下
...全文
408 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
justanewman 2017-05-16
  • 打赏
  • 举报
回复

$filename   = $commonpath . '.csv';
$file = fopen($filename,"r");
Header("Content-type: application/octet-stream");
//按照字节大小返回
Header("Accept-Ranges: bytes");
//返回文件的大小
Header("Accept-Length: ".filesize($filename));
//这里对客户端的弹出对话框,对应的文件名
Header("Content-Disposition: attachment; filename=".basename($filename));
//修改之前,一次性将数据传输给客户端
echo fread($file, filesize($filename));
//修改之后,一次只传输1024个字节的数据给客户端
//向客户端回送数据
$buffer=1024;//
//判断文件是否读完
while (!feof($file)) {
    //将文件读入内存
    $file_data=fread($file,$buffer);
    //每次向客户端回送1024个字节的数据
    echo $file_data;
}
fclose($file);
unlink($filename); //下载完成后要进行删除    
justanewman 2017-05-16
  • 打赏
  • 举报
回复
感觉是你没有转码

<?php
private function create_csv($path)
    {
        //data为数据
        $data = array();
        $fp     = fopen($path . '\1.csv', 'a');
        $header = $data[0] . PHP_EOL . $data[1] . PHP_EOL . $data[2] . PHP_EOL;
        $header = mb_convert_encoding($header, 'gbk', 'utf-8');
        $content = $data[3];
        $content = mb_convert_encoding($content, 'gbk', 'utf-8');
        $csv     = $header . $content;
        fwrite($fp, $csv);
        fclose($fp);
}
?>
diergelicheng 2017-05-12
  • 打赏
  • 举报
回复
刚在 我的CSDN那里也发了帖子,悬赏200C币,这里不晓得如何加 C币,成功了也可以送C币

4,249

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧