使用PHPExcel 在线预览excel(补齐excel不能预览的锅)

15 篇文章 0 订阅

本来最好的是转成pdf,但是没成功,只能曲线救国了

经过一系列测试之后,还是宣布不算很成功,会受excel版本,和内容影响,不支持图片

最新方法:https://blog.csdn.net/qq_27229113/article/details/82353909

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写接口就必须修改下,不然样式不好看)

如果有更好的方法请告知下博主

PHP调用OpenOffice实现word/ppt转PDF-附带demo,地址:https://blog.csdn.net/qq_27229113/article/details/81984394

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可以吧可以吧

打赏可以获得大长腿妹子微信

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值