我正在尝试使用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>
当我点击“生成Word”链接时,它什么也没做。
答案 0 :(得分:2)
校正。这是它必须要做的方式。问题是我正在进行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>