使用phpword导出word 中文乱码问题[IDE是PhpStrom]

来源:互联网 发布:淘宝网男士短袖 编辑:程序博客网 时间:2024/05/21 10:46

公司最近人手不够,本来写android的现在开始接手PHP后台了,没什么办法啊,当然是去做啦。今天业务给了一个需求,把下面的table导出来,生成word文档:
这里写图片描述

最终的效果是这样的:
这里写图片描述

个人感觉还可以吧,虽然没什么艺术细胞。

生成word的PHP插件来自phpWord,还是贴一下主要程序吧:

$spendingResult = "来自你的tp查询的值啊,这我可给你看啊";$rowHeight = 400 ;$cellWidth = 2500 ;vendor('PHPWord');$PHPWord = new \PHPWord();$PHPSession = $PHPWord->createSection();// Define table style arrays$styleTable = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80); $styleFirstRow = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');        // Define cell style arrays        $styleCell = array('valign' => 'center');        $styleCellBTLR = array('valign' => 'center');        // Define font style for first row        $fontStyle = array('bold' => false, 'align' => 'center','size'=>14);        // Add table style        $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);        // Add table        $table = $PHPSession->addTable('myOwnTableStyle');        // Add row        $table->addRow($rowHeight);        $table->addCell($cellWidth,$styleCell)->addText('用户信息', $fontStyle);        $table->addCell($cellWidth,$styleCell)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleCell)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleCell)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('用户id', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['uid'], $fontStyle);        $table->addCell($cellWidth)->addText('提现人', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['cardholder'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('手机号码', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['mobile'], $fontStyle);        $table->addCell($cellWidth)->addText('朋游号', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['yyid'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth,$styleFirstRow)->addText('提现信息', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('总收入', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['totalincome'], $fontStyle);        $table->addCell($cellWidth)->addText('可提现金额', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['withdrawcash'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('当前金额', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['currentbalance'], $fontStyle);        $table->addCell($cellWidth)->addText('冻结金额', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['freezebalance'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('提现金额', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['cash'], $fontStyle);        $table->addCell($cellWidth)->addText('提现时间', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['operationtime'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth,$styleFirstRow)->addText('银行信息', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('开户人', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['cardholder'], $fontStyle);        $table->addCell($cellWidth)->addText('开户银行', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['bank'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('开户账号', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['cardno'], $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth,$styleFirstRow)->addText('支付宝信息', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('支付宝账号', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['alipay'], $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth,$styleFirstRow)->addText('操作信息', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('操作时间', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['dealtime'], $fontStyle);        $table->addCell($cellWidth)->addText('操作员', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['operator'], $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth,$styleFirstRow)->addText('状态消息', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addCell($cellWidth,$styleFirstRow)->addText('', $fontStyle);        $table->addRow($rowHeight);        $table->addCell($cellWidth)->addText('提现状态', $fontStyle);        $table->addCell($cellWidth)->addText($spendingResult['flag'], $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $table->addCell($cellWidth)->addText('', $fontStyle);        $objWriter = \PHPWord_IOFactory::createWriter($PHPWord,"Word2007");        $outfile = $spendingResult['cardholder']."_提现详情_".date('YmdHis',time()).".doc";        //export to exploer        header("Content-Type: application/force-download");        header("Content-Type: application/octet-stream");        header("Content-Type: application/download");        header('Content-Disposition:inline;filename="'.$outfile.'"');        header("Content-Transfer-Encoding: binary");        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");        header("Pragma: no-cache");        $objWriter->save('php://output');        die();

如果就这么玩的话的,你在phpStrom上导出的结果八成是这样的:
这里写图片描述

结果百度,很多结果如下:
这里写图片描述

这里写图片描述

但是哥啊,我试过了不管用啊,不过上面的答案中给了我一个解决思路,就是php文件是采用gbk编码的,这个我还是比较震惊的,看到我们的PhpStrom的右下角:
这里写图片描述

因为我的文件理论上是没有按照PHP标准写的,所以按照以上几位哥们的做法是行不通的,因为引起乱码的方式有很多种嘛,那么遇到这种情况怎么办? 看这条记录:

$table->addCell($cellWidth,$styleCell)->addText('用户信息', $fontStyle);

还是老样子,进addText源码去看看吧:
这里写图片描述

那么这个utf8_encode是干嘛的?看源码:
这里写图片描述

将ISO-8859-1转成UTF-8,那么好了,我本来就是utf-8的字符,就不需要了,那么我们只需要将这句话注释掉就行了:
这里写图片描述

注意是cell啊,我这是用session写的,你用template写的注释template里面就行了。
学php不是很久,说错的地方不要见怪啊,谢谢。。。

原创粉丝点击