php 使用table方式导出excel文件
这些天在使用PHPExcel导出数据时,5000条数据竟然挂了。后来跟同事聊聊,有些明悟,PHPExcel做了很多处理,我在这里理解为渲染,就会暂用过多的空间,‘膨胀’的空间导致内存暂用过大,就挂了。其实只要我们只是简单的导出操作,没有必要使用PHPExcel。
就是将html的表格转换excel的表格;此种方法适应于设置各种单元格的显示,合并,只需设置html的table,设置css就能导出各式各样的excel模板
实例如下:
导出一个带表头,设置字体大小,居中,排版适中;
<?php
/*
*处理Excel导出
*@param $datas array 设置表格数据
*@param $titlename string 设置head
*@param $title string 设置表头
*/
function excelData($datas,$title,$filename){
$str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
$str .="<style>tr,td,th{text-align: center;height: 22px;line-height: 22px;}</style>";
$str .="<table border=1>";
$str .= $title;
foreach ($datas as $key=> $rt )
{
$str .= "<tr>";
foreach ( $rt as $k => $v )
{
$str .= "<td>{$v}</td>";
}
$str .= "</tr>\n";
}
$str .= "</table></body></html>";
header( "Content-Type: application/vnd.ms-excel; name='excel'" );
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=".$filename );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
exit( $str );
}
//todo:导出数据(自行设置)
$dataResult = array(
[
'腾讯','百度','阿里巴巴'
]
,
[
'腾讯2','百度2','阿里巴巴2'
]
);
//表头
$headList = [
'合作单位1',
'合作单位2',
'合作单位3',
];
array_unshift($dataResult,$headList);
$headTitle = "合作单位 优惠券赠送记录";
$title = "合作单位记录";
$headtitle= "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='".count($headList)."' >{$headTitle}</th></tr>";
$filename = $title.".xls";
excelData($dataResult,$headtitle,$filename);
php 使用table方式导出excel文件的更多相关文章
- vue+iview中的table表格导出excel表格
一.iveiw框架中table中有exportCsv()方法可以导出.csv后缀文件,类似于excel文件,但是并不是excel文件. 二.实现table表格导出excel文件利用Blob.js 和 ...
- PHP导出excel文件的几种方式
PHP导出excel文件的几种方式 先说说动态生成的内容当作文件来下载的方法: 1.通过把Content-Type设置为application/octet-stream,可以把动态生成的内容当作文件来 ...
- 转:PHP导出excel文件的几种方式
PHP导出excel文件的几种方式 文章来源:http://www.cnblogs.com/fredshare/archive/2012/10/29/2744243.html 先说说动态生成的内容当作 ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- 如何使用JavaScript实现纯前端读取和导出excel文件
js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯JS即可读取和导出excel的工具库,功能强大,支持格式众多,支持xls.xlsx.ods(一种OpenOffice专 ...
- html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式
先上代码 <script type="text/javascript" language="javascript"> var idTmr; ...
- 如何使用JavaScript实现纯前端读取和导出excel文件(转)
转自小茗同学博客:https://www.cnblogs.com/liuxianan/p/js-excel.html js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯 ...
- PHP导出excel文件
现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...
- PHPExcel导出excel文件
今天园子刚开,先来个货顶下,后续园丁qing我会再慢慢种园子的,希望大家多来园子逛逛. PHPExcel导出excel文件,先说下重要的参数要记住的东西 impUser() 导入方法 exportEx ...
随机推荐
- springboot测试
一.单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. 1.在pom包中添加spring-boot-starter-test包引用 <depend ...
- [Java in NetBeans] Lesson 17. File Input/Output.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...
- Tensorflow 搭建神经网络及tensorboard可视化
1. session对话控制 matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[2],[2]]) product = tf.matmul(m ...
- MySQL--5子查询与连接小结
子查询:出现在其他sql语句中的 SELECT,必须出现在小括号内,子查询外层可以是常见的SELECT语句,INSERT语句 UPDATE语句 DELETE语句,在子查询中可以包含多个关键字和条件 ( ...
- MySQL学习笔记--启动停止服务
右键点击计算机->管理->服务 windows所有的服务都在,mysql等等.可以在这里启动停止服务也可以在命令行 net start/stop <服务名> InnoDB还 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统--系统权限及操作指引
系列目录 1.权限包括菜单权限,按钮权限,数据权限 2.角色组和用户之间是多对多的关系,即多个用户可以拥有多个角色组,权限是拥有角色组的并集 1.菜单界面,菜单都是动态数据由模块管理进行设置 2.权限 ...
- ntpdate 正确的做法
网上很多搜到的资料都是过时的,主要是时间服务器不能用,不管是国内的还是国外的 1. yum -y install ntp ntpdate 2. 在/etc/crontab中,加入: 0 0 0 * 1 ...
- python string method
嗯,学习其它语言没这样全练过,嘻嘻 //test.py 1 # -*- coding: UTF-8 -*- 2 3 str = "i am worker" 4 print str. ...
- Amber TUTORIAL 4b: Using Antechamber to Create LEaP Input Files for Simulating Sustiva (efavirenz)-RT complex using the General Amber Force Field (GAFF)
sustiva.pdb PDB: 1FKO Create parameter and coordinate files for Sustiva 1. 加氢: $ reduce sustiva.pdb ...
- iOS 开发常用链接总结
知识归纳 1.招聘一个靠谱的程序员 面试题答案 https://github.com/ChenYilong/iOSInterviewQuestions 2.中文 iOS/Mac 开发博客列表 http ...