生成ms word文档的Yii框架

生成ms word文档的Yii框架,yii,frameworks,ms-word,Yii,Frameworks,Ms Word,我正在尝试使用Yii生成ms word文档。我发现做这件事很难。我找到了这个例子,但我无法让它发挥作用 View: echo CHtml::ajaxLink( 'Generate Word', CController::createUrl('Calculator/generateWord'), array( 'type' => 'POST', ), array('id'=>'gen_word') ); Controller: $var

我正在尝试使用Yii生成ms word文档。我发现做这件事很难。我找到了这个例子,但我无法让它发挥作用

View:
echo CHtml::ajaxLink(
    'Generate Word',
    CController::createUrl('Calculator/generateWord'), array(
        'type'    => 'POST',
    ), array('id'=>'gen_word')
);

Controller:
$var = "hello";
$div = $this->renderPartial('graphs/print', array('var'=>$var), true);
header("Pragma: no-cache"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=\"test.doc");
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
echo $div;

graphs/print:
<table>
    <tr>
        <td>This is just s test</td>
    </tr>
    <tr>
        <td><?php echo $var; ?></td>
    </tr>
</table>
视图:
echo-CHtml::ajaxLink(
“生成单词”,
CController::createUrl('Calculator/GenerateWorld'),数组(
'type'=>'POST',
),数组('id'=>'gen_word')
);
控制器:
$var=“你好”;
$div=$this->renderPartial('graphs/print',array('var'=>$var),true);
标题(“Pragma:no cache”);//必修的
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标头(“缓存控制:专用”,false);//某些浏览器需要
标题(“内容类型:application/vnd.ms word”);
标题(“内容处置:附件;文件名=\”test.doc”);
标题(“内容传输编码:二进制”);
ob_clean();
冲洗();
echo$div;
图表/打印:
这只是一个测试

当我点击“Generate Word”链接时,它什么也不做。

更正。这是必须做的。问题是我在做一个Ajax调用。相反,使用一个简单的链接没有问题

View:
echo CHtml::link('Generate Word', array('Calculator/generateWord'));

Controller:
    public function actionGenerateWord() {
    $var = "Hello World!";
    $div = $this->renderPartial('graphs/word', array('var'=>$var), true);
    header("Pragma: no-cache");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment; filename=\"test.doc");
    header("Content-Transfer-Encoding: binary");
    ob_clean();
    flush();
    echo $div;
    Yii::app()->end();
}

Rendered view:
<table>
    <tr>
        <td>This is just a test</td>
    </tr>
    <tr>
        <td><?php echo $var; ?></td>
    </tr>
</table>
视图:
echochtml::link('generateWord',array('Calculator/generateWord');
控制器:
公共功能actionGenerateWord(){
$var=“你好,世界!”;
$div=$this->renderPartial('graphs/word',array('var'=>$var),true);
标题(“杂注:无缓存”);
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标头(“缓存控制:专用”,false);
标题(“内容类型:application/vnd.ms word”);
标题(“内容处置:附件;文件名=\”test.doc”);
标题(“内容传输编码:二进制”);
ob_clean();
冲洗();
echo$div;
Yii::app()->end();
}
渲染视图:
这只是一个测试

如果docx文件没有问题,您可以尝试使用phpword操作word docx文档。以下是如何使用: