/**
* 数组转xls格式的excel文件
* @param array $data 需要生成excel文件的数组
* @param string $filename 生成的excel文件名
* 示例数据:
$data = array(
array(NULL, 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Q2', 56, 73, 86),
array('Q3', 52, 61, 69),
array('Q4', 30, 32, 0),
);
*/
function create_xls($data,$filename = 'simple.xls'){
ini_set('max_execution_time','0');
Vendor('PHPExcel.PHPExcel');
$filename = str_replace('.xls','',$filename) . '.xls';
$phpexcel = new PHPExcel();
$phpexcel->getProperties()
->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$phpexcel->getActiveSheet()->fromArray($data);
$phpexcel->getActiveSheet()->setTitle('Sheet1');
$phpexcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');//Date in the past
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');//always modified
header ('Cache-Control: cache, must-revalidate');//HTTP/1.1
header ('Pragma: public');//HTTP/1.0
$objwriter = PHPExcel_IOFactory::createWriter($phpexcel,'Excel5');
$objwriter->save('php://output');
exit;
}

导出的使用:

     //导出
public function create_xls() {
$industry_list = M('welfare')->field('welfare_name')->select();
$data = array();
array_push($data, array('福利名称'));
foreach ($industry_list as $key => $value) {
array_push($data, array($value['welfare_name']));
}
create_xls($data, '福利');
}

导入:

    //导入
public function upload_xls() {
$upload = new \Think\Upload();
$upload->maxSize = 3145728;
$upload->exts = array('xls', 'xlsx');
$upload->saveExt = 'xls';
$upload->rootPath = './Application/Upload/excel/';
$upload->autoSub = false;
$result = $upload->uploadOne($_FILES['xls_file']);
if (!$result) {
$this->error($upload->getError(), U('WelFare/welfare_list'));
} $data = import_excel($upload->rootPath . $result['savepath'] . $result['savename']);
delDirAndFile($upload->rootPath . $result['savepath']); $list = array();
unset($data[1]);
$i = 0; foreach ($data as $k => $v) {
$param = array();
$param['welfare_name'] = $v[0];
$info = M('welfare')->where($param)->find();
if ($info)
continue; $list[$i]['welfare_name'] = $v[0];
$i++;
} if ($i) {
M('welfare')->addAll($list);
$this->success('成功插入了' . $i . '条数据', U('WelFare/welfare_list'));
} else {
$this->error('插入了0条数据', U('WelFare/welfare_list'));
}
}

thinkphp--导入导出xls文件的更多相关文章

  1. 【转】 (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

  2. (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

  3. MYSQL导入导出.sql文件

    MYSQL导入导出.sql文件   一.MYSQL的命令行模式的设置:桌面->我的电脑->属性->环境变量->新建->PATH=“:path\mysql\bin;”其中p ...

  4. Orcle导入导出dmp文件

    --Orcle导入导出dmp文件 --------------------------2013/12/06 导出表:   exp scott/tiger@mycon tables=(dept,emp) ...

  5. oracle导入导出 dmp文件

    oracle导入导出 dmp文件: 打开cmd窗口,在cmd窗口下,按照个人需要输入以下对应的命令: 1.imp 用户名/密码@网络服务名 file=XXX.dmp fromuser=XXX tous ...

  6. 【POI】导出xls文件报错:The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

    使用POI导出xls文件,由于数据过多,导致导出xls报错如下: The maximum number of cell styles was exceeded. You can define up t ...

  7. ASP.NET Core导入导出Excel文件

    ASP.NET Core导入导出Excel文件 希望在ASP.NET Core中导入导出Excel文件,在网上搜了一遍,基本都是使用EPPlus插件,EPPlus挺好用,但商用需要授权,各位码友若有好 ...

  8. thinkphp导入导出excel表单数据

    在PHP项目经常要导入导出Excel表单. 先去下载PHPExcel类库文件,放到相应位置. 我在thinkphp框架中的位置为ThinkPHP/Library/Org/Util/ 导入 在页面上传e ...

  9. 导入导出Excel文件

    搭建环境 先新建web project ,然后Add Struts Capabilties: 下载导入导出Excel所需的jar包: poi-3.8-20120326.jar包  :  http:// ...

  10. java导入导出Excel文件

    package poi.excel; import java.io.IOException; import java.io.InputStream; import java.io.OutputStre ...

随机推荐

  1. phpStorm 激活方式

    phpStorm10  激活方式 方法一:   注册时选择License server  http://idea.lanyus.com/    方法二:  用户名:Ly  注册码:  \===== L ...

  2. Pytest系列(6) - conftest.py的详细讲解

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 什么是conftest.py 可以 ...

  3. D - Expanding Rods POJ - 1905(二分)

    D - Expanding Rods POJ - 1905 When a thin rod of length L is heated n degrees, it expands to a new l ...

  4. CodeForces - 817B(分类讨论 + 排列组合)

    题目链接 思路如下 这一题是:最菜的队伍只有三个人组成,我们只需对排序后的数组的 前三个元素进行分类讨论即可: a[3] != a[2] && a[3] != ar[1] a[3] = ...

  5. iSCSI集群与存储

                                                                                                        ...

  6. zendframework3

    1.开发时关闭cache,正式上线后打开cache application config file (config/application.config.php),  disable this cac ...

  7. 100 Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. 15分钟从零开始搭建支持10w+用户的生产环境(一)

    前言 这是一个基于中小型企业或团队的架构设计. 不考虑大厂.有充分的理由相信,大厂有绝对的实力来搭建一个相当复杂的环境. 中小型企业或团队是个什么样子? 开发团队人员配置不全,部分人员身兼开发过程上下 ...

  9. ps 命令显示不完整的问题

    今天在使用ps命令的时候,无法查找到指定名字的进程ID,仔细查找才发现ps命令查找的结果中进程启动的命令以及参数信息被截断了 问题实例 用户wanng启动了一个进程 wanng_qytrunkcros ...

  10. 选择IT行业的自我心得,希望能帮助到各位!(五)

    相信很多小伙伴,在看完之前的一二三四,也是我一路走来,走走停停,走走停停,有快乐,也有伤悲,毕竟这就是人生嘛,人生不起起伏伏怎么才能体验刺激的快感,也让我从一个小男孩净化成清高浮躁的青少年,在从而让我 ...