8

我用过PHPExcel公司Yii2项目中的扩展,我创建了PHPExcel公司对象,但此组件导出“不可读内容”Excel文件。

我已经从这些链接中阅读了此问题的所有可能解决方案:

  1. PHPExcel-.xlsx文件下载无法读取的内容
  2. PHPExcel创建“不可读内容”
  3. https://stackoverflow.com/questions/32103447/excel-found-unreadable-content-with-phpexcel
  4. PHPExcel不可读内容

我得到了以下“无法读取的内容”Excel文件:

不可读内容

我的代码是,

成分Excel网格.php:

<?php(电话)名称空间应用\组件;使用Yii;使用Closure;使用yii\i18n\Formatter;使用yii\base\InvalidConfigException;使用yii\helpers\Url;使用yii\helpers\Html;使用yii\helpers\Json;使用yii\helpers\ArrayHelper;使用yii\widgets\BaseListView;使用yii\base\Model;使用\PHPExcel;使用\PHPExcel_IOFactory;使用\PHPExcel_Settings;使用\PHPExcel_Style_Fill;使用\PHPExcel_Writer_IWriter;使用\PHPExcel_Worksheet;类ExcelGrid扩展了\yii\grid\GridView{公共$columns_array;公共$properties;public$filename='excel';public$扩展名=“xlsx”;私有$_provider;私有$_visibleColumns;private$_beginRow=1;私有$_endRow;私有$_endCol;私有$_objPHPExcel;私有$_objPHPExcelSheet;private$_objPHPExcelWriter;私人$_objPHPExcelWriter;公共函数init(){父项::init();}公共函数run(){//$this->test();if(函数存在('mb_internal_encoding')){$oldEncoding=mb_internal_encoding();mb内部编码('utf8');}对象开始();$this->init_provider();$this->init_excel_sheet();$this->initPHPExcelWriter(“Excel2007”);$this->generateHeader();$row=$this->generateBody();$writer=$this->_objPHPExcelWriter;$this->setHttpHeaders();ob_end_clean();$writer->保存('php://输出');      if(函数存在('mb_internal_encoding'))mb_internal_encoding($oldEncoding);出口;Yi::$app->end();//$writer->save('test.xlsx');父项::run();}公共函数init_provider(){$this->_provider=克隆($this->dataProvider);}公共函数init_excel_sheet(){$this->_objPHPExcel=新PHPExsel();$creator=“”;$title=“”;$subject=“”;$description=“Excel网格”;$category=“”;$keywords=“”;$manager=“”;$created=日期(“Y-m-d H:i:s”);$lastModifiedBy=“”;提取($this->属性);$this->_objPHPExcel->getProperties()->setCreator($creator)->setTitle($title)->setSubject($subject)->setDescription($description)->setCategory($category)->setKeywords($keywords)->setManager($manager)//->setCompany($公司)->已创建集合(已创建$)->设置上次修改者($lastModifiedBy);$this->_objPHPExcelSheet=$this->_objPHPExcel->getActiveSheet();}公共函数initPHPExcelWriter($writer){$this->_objPHPExcelWriter=PHPExcel_IOFactory::createWriter($this->_objPHPExcel,$作者);}公共函数generateHeader(){$this->setVisibleColumns();$sheet=$this->_objPHPExcelSheet;$colFirst=self::columnName(1);$this->_endCol=0;foreach($this->_visibleColumns作为$column){$this->_endCol++;$head=($column instanceof\yii\grid\DataColumn)$this->getColumnHeader($column):$column->header;$cell=$sheet->setCellValue(self::columnName($this->_endCol)$this->_beginRow,$head,true);}$sheet->freezePane($colFirst.($this->_beginRow+1));}公共函数generateBody(){$columns=$this->_visibleColumns;$models=array_values($this->_provider->getModels());if(计数($columns)==0){$cell=$this->_objPHPExcelSheet->setCellValue('A1',$this->空文本,true);$model=重置($models);返回0;}$keys=$this->_provider->getKeys();$this->_endRow=0;foreach($models作为$index=>$model){$key=$keys[$index];$this->generateRow($model,$key,$index);$this->_endRow++;}//将自动筛选设置为打开$this->_objPHPExcelSheet->setAutoFilter(self::columnName(1)。$this->_beginRow。":" .self::columnName($this->_endCol)。$this->_endRow);return($this->_endRow>0)?计数($models):0;}公共函数generateRow($model,$key,$index){$单元格=[];/*@var$column列*/$this->_endCol=0;foreach($this->_visibleColumns作为$column){if($column instanceof\yii\grid\SerialColumn||$column instanceof\yii\grid\ActionColumn){继续;}其他{$format=$column->format;$value=($column->content===null)?$this->formatter->format($column->getDataCellValue($model,$key,$index),$format):call_user_func($column->content,$model,$key,$index,$column);}if(空($value)&&!空($column->attribute)&&$column->attribution!==null){$value=ArrayHelper::getValue($model,$column->属性,'');}$this->_endCol++;$cell=$this->_objPHPExcelSheet->setCellValue(self::columnName($this->_endCol)。($index+$this->_beginRow+1),strip_tags($value),true);}}受保护的函数setVisibleColumns(){$cols=[];foreach($this->列为$key=>$column){if($column instanceof\yii\grid\SerialColumn||$column-instanceof\ yii\grid\ActionColumn){继续;}$cols[]=$column;}$this->_visibleColumns=$cols;}公共函数getColumnHeader($col){if(isset($this->columns_array[$col->attribute])返回$this->columns_array[$col->attribute];/*@var$模型yii\base\model*/if($col->header!===null ||($col->label===null&&$col->attribute===null){返回修剪($col->header)!==''$col->header:$col->grid->emptyCell;}$provider=$this->dataProvider;if($col->label===null){if(ActiveDataProvider的$provider实例&&$provider->ActiveQueryInterface的查询实例){$model=新的$provider->query->modelClass;$label=$model->getAttributeLabel($col->attribute);}其他{$models=$provider->getModels();if(($model=重置($models))模型实例){$label=$model->getAttributeLabel($col->attribute);}其他{$label=$col->属性;}}}其他{$label=$col->标签;}return$label;}公共静态函数columnName($index){$i=$索引-1;如果($i>=0&&$i<26){返回chr(ord('A')+$i);}如果($i>25){return(self::columnName($i/26))。(自我::columnName($i%26+1));}返回“A”;}受保护的函数setHttpHeaders(){标题(“Cache-Control:no-Cache”);标题(“Pragma:no-cache”);标题(“Content-Type:application/{$this->extension};charset=utf-8”);header(“Content-Disposition:attachment;filename={$this->filename}.{$thix->extension}”);页眉(“过期时间:0”);}}

查看文件countryExcel.php:

<?php程序\app\components\ExcelGrid::小部件([“dataProvider”=>$dataProvider,“filterModel”=>$searchModel,“扩展名”=>“xlsx”,“文件名”=>“国家列表”。日期(“Y-m-dH:i:s”),“属性”=>[//“创建者”=>“”,//“标题”=>“”,//“主题”=>“”,//“类别”=>“”,//'关键词'=>'',//“经理”=>“”,],“列”=>[['class'=>'yii\grid\SerialColumn'],'国家名称',[“属性”=>“created_at”,“value”=>函数($data){return Yii::$app->formatter->asDateTime($data->created_at);},],[“属性”=>“created_by”,“value”=>“createdBy.user_login_id”,],[“属性”=>“updated_at”,“value”=>函数($data){返回(!空($data->updatedat)?Yi::$app->formatter->asDateTime($data->updated_at):Yi:('stu','(未设置)');},],[“attribute”=>“updated_by”,'value'=>'updatedBy.user_login_id',],],]);?>

控制器文件国家控制器.php:

<?php程序类CountryController扩展控制器{............公共函数actionExcel(){$searchModel=new CountrySearch();$dataProvider=$searchModel->search(Yi::$app->request->queryParams);return$this->renderPartial('CountryExportExcel'['searchModel'=>$searchModel,'dataProvider'=>$dataProvider,]);}.............}?>

提前谢谢。

7
  • 在文本编辑器中打开文件并查找前导/尾随空格;任何BOM标题;或文件中的任何明文错误消息 评论 2015年11月30日9:12
  • 您正在创建的EXCEL文件。。。。。。这就是抱怨内容不可读的文件 评论 2015年11月30日9:14
  • 这是在gedit文本编辑器中打开的excel文件的快照截图@马克银行
    – 加米特
    评论 2015年11月30日9:21
  • 1
    首先,看起来你有一个换行符作为第一个字符,这将是一个腐败。。。。通常由拖尾引起?>在您的一个文件中。。。这就是为什么大多数框架/库作者建议不要使用?>在文件末尾 评论 2015年11月30日9:28
  • 我必须删除?>在的末尾Excel网格.php文件。但仍然得到“不可编辑的内容”,而且第一行的第一个字符是换行符。
    – 加米特
    评论 2015年11月30日9:35

2个答案2

重置为默认值
1

这不是一个有效的解决方案,但对我来说很有效。。

header(“Content-Disposition:附件;文件名=\”$filename\“”);$export=\moonland\phpexcel\Excel::export([“models”=>$exportData,“columns”=>$exportColumns,“标题”=>[“created_at”=>dtTZ()],]);
0
0

您的文件似乎是使用类似记事本的编辑器打开的正确xlsx文件++可能是字符编码问题

尝试更改编码

if(函数存在('mb_internal_encoding')){$oldEncoding=mb_internal_encoding();mb内部编码('utf8');}

使用cp1250的值--到cp1258

微软在US-EN中的编码
(我希望您的excel国家/地区安装配置)

1
  • 我不明白你说了什么?
    – 加米特
    评论 2015年12月7日7:03

你的答案

单击“发布您的答案”,表示您同意我们的服务条款并确认您已阅读我们的隐私政策.

不是你想要的答案吗?浏览标记的其他问题问你自己的问题.