热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

thinkphp导入导出xls文件

1**2*数组转xls格式的excel文件3*paramarray$data需要生成excel文件的数组4*paramstring$filename生成的excel文件名5*示例数

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

导出的使用:

1 //导出
2 public function create_xls() {
3 $industry_list = M('welfare')->field('welfare_name')->select();
4 $data = array();
5 array_push($data, array('福利名称'));
6 foreach ($industry_list as $key => $value) {
7 array_push($data, array($value['welfare_name']));
8 }
9 create_xls($data, '福利');
10 }

导入:

//导入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'));}}

 

转:https://www.cnblogs.com/laijinquan/p/7443538.html



推荐阅读
author-avatar
QQweiqiang_850
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有