使用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不是很久,说错的地方不要见怪啊,谢谢。。。
阅读全文
1 0
- 使用phpword导出word 中文乱码问题[IDE是PhpStrom]
- phpword生成中文乱码的问题
- PHPWord 导出生成word
- PHPword解决中文乱码
- phpword解决中文乱码
- 用phpword导出word文档
- PHPWord中文乱码问题解决方案
- R12导出中文是乱码
- C# 导出word 和中文乱码
- python之Wing IDE中文乱码问题
- 使用Eclipse导出java.doc中文乱码问题
- ODI使用MDX导出Essbase数据中文乱码问题
- freemarker导出word部分电脑出现中文乱码问题的解决
- jfreechart一&二维报表导出为word&pdf&html-中文乱码和字体模糊问题(一)
- Jsp导出word,文件名乱码的问题
- java 导出word乱码问题,99%解决问题
- phpword - 生成word的php类库相关问题总结
- Ubuntu下是使用source insight的中文乱码问题
- Letter by Letter 字典树+dp
- 解决问题的通用思路
- 动态代理笔记
- 垃圾收集器和内存分配
- Winsock(微软套接字接口)的使用(1)
- 使用phpword导出word 中文乱码问题[IDE是PhpStrom]
- android 属性动画总结
- 简明 VIM 练级攻略
- angularJS中repeat数据不能出现重复项解决
- 看Spring实战有感(一)
- 解决wireshark安装mavlink协议插件后解析报错的问题
- 推荐系统中基于深度学习的混合协同过滤模型
- 第12天面向对象
- Java 接口(Interface)