使用PHPExcel 在线预览excel(补齐excel不能预览的锅)
本来最好的是转成pdf,但是没成功,只能曲线救国了
经过一系列测试之后,还是宣布不算很成功,会受excel版本,和内容影响,不支持图片
1.网上下载一个PHPExcel.php 百度很多 2. 控制器 controller(最好的是自己用composer安装引入)
set_time_limit(0);
require dirname(dirname(__FILE__)) . '../../common/comp/excel/PHPExcel.php'; //更改为你的phpexcel文件地址
$filePath =>Yii::$app->request->post('path')
// Create new PHPExcel object
$objReader =new \PHPExcel_Reader_Excel5();
//$objReader =new \PHPExcel_Reader_Excel2007();
$objWriteHtml=new \PHPExcel_Writer_HTML($objReader->load($filePath));
echo $objWriteHtml->save("php://output");
注:
$objReader =new \PHPExcel_Reader_Excel5();
//$objReader =new \PHPExcel_Reader_Excel2007();
这两个方法看自己情况选择,结果会之间返回图片,或许这里有点坑,后面有解决的方法,自行参考
view 页面我只简单写
<?php
use yii\web\View;
use yii\helpers\Html;
$this->registerJsFile(Yii::$app->request->baseUrl . '/resources/js/jquery.min.js', ['position' => View::POS_HEAD]);
$this->registerJS("
//预览
$('.data_table').on('click', '.preview_icon', function(){
$('#fileIframe').attr('src',$(this).attr('url'));
$('.quotation_iframe_bg,.quotation_iframe').show();
});
$('.quotation_iframe_bg,.iframe_close').click(function(){
$('.quotation_iframe_bg,.quotation_iframe').hide();
});
");
?>
//预览 链接
<a class="preview_icon" href="javascript:;" url="<?= Yii::$app->urlManager->createUrl(['/goods/xls', 'path' => $model['path']]) ?>">预览</a>
<!-- 弹框 使用iframe 显示excel内容-->
<div class="comm_popup_bg quotation_iframe_bg"></div>
<div class="quotation_iframe">
<span class="iframe_close"></span>
<iframe id="fileIframe" width="100%" height="100%" src="" frameborder="0"></iframe>
</div>
如果自己觉得样式不好看,或者不符合业务的话,去phpexcel里面修改样式(类里面样式是拼合起来的,我用api写接口就必须修改下,不然样式不好看)
版权声明
本文仅代表作者观点,不代表博信信息网立场。