• 使用PHPword中文乱码并且下载的方法


      如果你的编码格式是utf-8的话就用这个

       1.找到 Section.php 的 addText 函数

    1 $givenText = utf8_encode($text);

     改成

    1 $givenText = iconv('gbk', 'utf-8', $text);

      2.找到template.php这个文件,找到这个,把$replace = utf8_encode($replace);注释,换成 $replace =iconv('gbk', 'utf-8', $replace);

    1 public function setValue($search, $replace) {
    2         if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
    3             $search = '${'.$search.'}';
    4         }
    5         
    6         if(!is_array($replace)) {
    7            // $replace = utf8_encode($replace);
    8            $replace =iconv('gbk', 'utf-8', $replace);
    9         }

      3.找到PHPWordSectionTableCell.php中的addText()方法,把这行$text = utf8_encode($text);注释掉就可以了

    4.输出的时候如果有汉字,就用这个输出

    1   $document->setValue('weekday', iconv("utf-8","gbk",$list['company_name']));

    5.保存的时候也一样

    1 $document->save( iconv("utf-8","gbk",'C:/3D定制云_'.$list['company_name'].'.docx'));

     6.下载word文档到本地

     1  header("Content-type:text/html;charset=utf-8");
     2         $filename='upload/download/3D定制云_'.$bloc['company_name'].'.docx';
     3         $file_path = iconv("utf-8","gbk",$filename);
     4         $fil_name=$filename;
     5         if (!file_exists($file_path)){
     6             echo  "没有该文件";
     7             return;
     8         }else{
     9             ob_clean();
    10             ob_start();
    11             $fp = fopen($file_path,"r");
    12             $file_size = filesize($file_path);
    13             Header("Content-type:application/octet-stream");
    14             Header("Accept-Ranges:bytes");
    15             Header("Accept-Length:".$file_size);
    16             Header("Content-Disposition:attchment; filename=".$fil_name);
    17             $buffer=1024;
    18             $file_count=0;
    19             while (!feof($fp) && $file_count<$file_size ){
    20                 $file_con=fread($fp,$buffer);
    21                 $file_count +=$buffer;
    22                 echo $file_con;
    23             }
    24             fclose($fp);
    25             ob_end_flush();
    26         }

    全部程序

     1 vendor('phpoffice/PHPWord/PHPWord');
     2         $PHPWord = new PHPWord();
     3         $dir='upload/';
     4         $word='test.docx';
     5         $document = new PHPWord_Template($dir.$word);
     6         $document->setValue('Value1', $bloc['username']);
     7         $document->setValue('Value2', $bloc['original_password']);
     8         $document->setValue('weekday', iconv("utf-8","gbk",$bloc['company_name']));
     9         $document->save( iconv("utf-8","gbk",'upload/download/3D定制云_'.$bloc['company_name'].'.docx'));
    10 
    11         header("Content-type:text/html;charset=utf-8");
    12         $filename='upload/download/3D定制云_'.$bloc['company_name'].'.docx';
    13         $file_path = iconv("utf-8","gbk",$filename);
    14         $fil_name=$filename;
    15         if (!file_exists($file_path)){
    16             echo  "没有该文件";
    17             return;
    18         }else{
    19             ob_clean();
    20             ob_start();
    21             $fp = fopen($file_path,"r");
    22             $file_size = filesize($file_path);
    23             Header("Content-type:application/octet-stream");
    24             Header("Accept-Ranges:bytes");
    25             Header("Accept-Length:".$file_size);
    26             Header("Content-Disposition:attchment; filename=".$fil_name);
    27             $buffer=1024;
    28             $file_count=0;
    29             while (!feof($fp) && $file_count<$file_size ){
    30                 $file_con=fread($fp,$buffer);
    31                 $file_count +=$buffer;
    32                 echo $file_con;
    33             }
    34             fclose($fp);
    35             ob_end_flush();
    36         }
    人生得意须尽欢,莫使金樽空对月.
  • 相关阅读:
    linux编写脚本执行.py文件
    x.strip().split(' ')
    pycharm全局搜索
    list extend()
    python format格式化字符串、格式化数字
    python range函数
    Teamcenter RAC 调用查询
    Circular Local MiniMax (数列特性问题+贪心)(CF 794,d2 C)
    Odd Subarrays (贪心,dp一下)(CF 794 d2)
    Number of Groups (区间重叠问题(时间线性)+贪心优化时间复杂度+优先队列(或者set))
  • 原文地址:https://www.cnblogs.com/luojie-/p/7704614.html
Copyright © 2020-2023  润新知