【问题标题】:Yii Framework generating ms-word documentsYii 框架生成 ms-word 文档
【发布时间】:2023-03-20 01:08:01
【问题描述】:

我正在尝试使用 Yii 生成一个 ms-word 文档。我发现很难做到。我找到了这个例子Generating 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>

当我点击“生成单词”链接时,它什么也没做。

【问题讨论】:

标签: yii frameworks ms-word


【解决方案1】:

更正。这是必须这样做的方式。问题是我正在做一个 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2011-12-26
    相关资源
    最近更新 更多