使用PHPword中文乱码并且下载的方法
如果你的编码格式是utf-8的话就用这个
1.找到 Section.php 的 addText 函数
$givenText = utf8_encode($text);
改成
$givenText = iconv('gbk', 'utf-8', $text);
2.找到template.php这个文件,找到这个,把$replace = utf8_encode($replace);注释,换成 $replace =iconv('gbk', 'utf-8', $replace);
public function setValue($search, $replace) {
if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
$search = '${'.$search.'}';
}
if(!is_array($replace)) {
// $replace = utf8_encode($replace);
$replace =iconv('gbk', 'utf-8', $replace);
}
3.找到PHPWord\Section\Table\Cell.php中的addText()方法,把这行$text = utf8_encode($text);注释掉就可以了
4.输出的时候如果有汉字,就用这个输出
$document->setValue('weekday', iconv("utf-8","gbk",$list['company_name']));
5.保存的时候也一样
$document->save( iconv("utf-8","gbk",'C:/3D定制云_'.$list['company_name'].'.docx'));
6.下载word文档到本地
header("Content-type:text/html;charset=utf-8");
$filename='upload/download/3D定制云_'.$bloc['company_name'].'.docx';
$file_path = iconv("utf-8","gbk",$filename);
$fil_name=$filename;
if (!file_exists($file_path)){
echo "没有该文件";
return;
}else{
ob_clean();
ob_start();
$fp = fopen($file_path,"r");
$file_size = filesize($file_path);
Header("Content-type:application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".$file_size);
Header("Content-Disposition:attchment; filename=".$fil_name);
$buffer=1024;
$file_count=0;
while (!feof($fp) && $file_count<$file_size ){
$file_con=fread($fp,$buffer);
$file_count +=$buffer;
echo $file_con;
}
fclose($fp);
ob_end_flush();
}
全部程序
vendor('phpoffice/PHPWord/PHPWord');
$PHPWord = new \PHPWord();
$dir='upload/';
$word='test.docx';
$document = new \PHPWord_Template($dir.$word);
$document->setValue('Value1', $bloc['username']);
$document->setValue('Value2', $bloc['original_password']);
$document->setValue('weekday', iconv("utf-8","gbk",$bloc['company_name']));
$document->save( iconv("utf-8","gbk",'upload/download/3D定制云_'.$bloc['company_name'].'.docx'));
header("Content-type:text/html;charset=utf-8");
$filename='upload/download/3D定制云_'.$bloc['company_name'].'.docx';
$file_path = iconv("utf-8","gbk",$filename);
$fil_name=$filename;
if (!file_exists($file_path)){
echo "没有该文件";
return;
}else{
ob_clean();
ob_start();
$fp = fopen($file_path,"r");
$file_size = filesize($file_path);
Header("Content-type:application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".$file_size);
Header("Content-Disposition:attchment; filename=".$fil_name);
$buffer=1024;
$file_count=0;
while (!feof($fp) && $file_count<$file_size ){
$file_con=fread($fp,$buffer);
$file_count +=$buffer;
echo $file_con;
}
fclose($fp);
ob_end_flush();
}
使用PHPword中文乱码并且下载的方法的更多相关文章
- jquery.ajax的url中传递中文乱码问题的解决方法
jquery.ajax的url中传递中文乱码问题的解决方法 JQuery JQuery默认的contentType:application/x-www-form-urlencoded 这才是JQu ...
- qt creator修改程序编码(解决中文乱码问题)的方法
qt creator修改程序编码(解决中文乱码问题)的方法 qt creator修改程序编码的功能有几处. 1.edit - select encoding 选择载入(显示)编码和储存编码,其中GB2 ...
- windows下mysql中文乱码, 配置解决方法
内容源自:windows下mysql中文乱码, 配置解决方法 解决方法:打开mysql安装目录,打开my.ini文件(如果只有my-default.ini文件,先将其改名为my.ini!!!) 修改内 ...
- resin后台输出中文乱码的解决的方法!
近期从tomcat移植到resin,发现这东西不错啊! 仅仅是后台输出时有时候中文会乱码. 如今找到resin后台输出中文乱码的解决的方法: 编辑conf/resin.con文件: <!--ja ...
- 解决 php7 cli 模式下中文乱码的两中方法
解决 php7 cli 模式下中文乱码的两中方法1. 给PHP文件开头加上 exec('chcp 936'); 然后把该文件以 ANSI 格式编码2. 在 php.ini 中设置 default_ch ...
- 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法
众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...
- 解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168
目前比较流行的是使用SWFUpload控件,这个控件的详细介绍可以参见官网http://demo.swfupload.org/v220/index.htm 在使用这个控件批量上传文件时发现中文文件名都 ...
- PHP中文乱码的常见解决方法总结
PHP中文乱码是PHP开发中的常见问题之一.PHP中文乱码有时发生在网页本身,有些产生在于MySQL交互的过程中,有时与操作系统有关.下面进行一番总结. 一.首先是PHP网页的编码 1. php文件本 ...
- PHPWord中文乱码、单元格合并、动态表格模板解决方案合集
摘要: 最近一个项目开发要用到PHP技术导出Word文档,采用PHPWord插件,版本为0.6.2 beta,CodePlex已停止维护.网上还有另外一个版本的PhpWord,项目类名大小写上略有不 ...
随机推荐
- C++ 内存分配(new,operator new)面面观 (转)
本文主要讲述C++ new运算符和operator new, placement new之间的种种关联,new的底层实现,以及operator new的重载和一些在内存池,STL中的应用. 一 new ...
- idea编译时JDK版本变化
修改参考:http://www.cnblogs.com/woshimrf/p/5863248.html 添加maven-compiler-plugin插件.
- Java堆栈内存总结
在Java中,主要存在四块内存空间,除了保存static类型属性的全局数据区,以及保存虽有方法定义的全局代码区之外,程序员更多的在乎内存中的另外两种区域--对象的生存空间堆(heap)和方法调用及变量 ...
- centos7下安装apache服务器httpd的yum方式安装
转自Clement-Xu的csdn博客 http://blog.csdn.net/clementad/article/details/41620631 Apache在Linux系统中,其实叫&qu ...
- ng-csv 异步数据下载
相信很多码友遇到一个很坑的问题吧,就是使用ng-csv 的时候 lazy-load="true" 设置为true 还是 会下载0行数据 var getArray= functio ...
- MySql主键自动生成,表、实体、C#调用方法
1.表:mysql建表语句 DROP TABLE IF EXISTS `keycode`; CREATE TABLE `keycode` ( `Id` ) NOT NULL AUTO_INCREMEN ...
- linux下lampp(xampp)安装memcached扩展
原理:根据自己的lampp中的php版本,编译memcache时,引用对应下载的php版本,并安装后的php_config来生成memcache.so文件,再将这个so文件放置到lamppp对应位置, ...
- No new migrations found. Your system is up-to-date.处理
显然是migrations表中存储的相关操作记录了,删除就好了!!!
- EmguCV中图像类型进行转换
1. Bitmap:类型不在 Emgucv命名空间中 2. Image<TColor, TDepth> 3. Mat: 4. UMat: 高 ...
- hdu3016 线段树+简单DP
以每个方块左右坐标区间为节点建立字典树,每个节点保存这个区间对应的方块的下标,将方块按照高度排序. 如何得到第i个方块可以移动到的两个方块呢?将所有方块排完序,将前i-1个方块放入字典树,根据第i个方 ...