标签:
下载 PHPExcel(https://github.com/PHPOffice/PHPExcel)
把下载的 zip 包解压至 ./ThinkPHP/Library/Vendor 下
控制器自定,这里用 IndexController.class.php
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { // 导入 Excel public function excel() { header(‘Content-type: text/html; charset=utf-8‘); vendor(‘PHPExcel.Classes.PHPExcel‘); $file = ‘./Data/example.xlsx‘; $Excel = new \PHPExcel(); // 如果excel文件后缀名为.xls // vendor("PHPExcel.Classes.PHPExcel.Reader.Excel5"); // 如果excel文件后缀名为.xlsx vendor("PHPExcel.Classes.PHPExcel.Reader.Excel2007"); $PHPReader = new \PHPExcel_Reader_Excel2007(); // 载入文件 $Excel = $PHPReader -> load($file); //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 $currentSheet = $Excel -> getSheet(0); //获取总列数 $allColumn = $currentSheet -> getHighestColumn(); //获取总行数 $allRow = $currentSheet -> getHighestRow(); //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始 for($currentRow = 1; $currentRow <= $allRow; $currentRow++) { //从哪列开始,A表示第一列 for($currentColumn = ‘A‘; $currentColumn <= $allColumn; $currentColumn++) { //数据坐标 $address = $currentColumn.$currentRow; //读取到的数据,保存到数组$arr中 $arr[$currentRow][$currentColumn] = $currentSheet -> getCell($address) -> getValue(); } } echo ‘<pre>‘; var_export($arr); } }
输出:
array ( 1 => array ( ‘A‘ => ‘分公司名称‘, ‘B‘ => ‘姓名‘, ‘C‘ => ‘金额‘, ), 2 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘赵娟‘, ‘C‘ => 1100, ), 3 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘孔坚‘, ‘C‘ => 1100, ), 4 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘王华发‘, ‘C‘ => 1300, ), 5 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘赵辉‘, ‘C‘ => 700, ), 6 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘华发‘, ‘C‘ => 1400, ), 7 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘赵德国‘, ‘C‘ => 700, ), 8 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘沈芳虹‘, ‘C‘ => 500, ), 9 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘周红玉‘, ‘C‘ => 1100, ), 10 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘施芬芳‘, ‘C‘ => 800, ), 11 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘蒋国建‘, ‘C‘ => 1100, ), 12 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘钱毅‘, ‘C‘ => 1400, ), 13 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘陈华惠‘, ‘C‘ => 1200, ), 14 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘曹香‘, ‘C‘ => 1400, ), 15 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘郑红妙‘, ‘C‘ => 600, ), 16 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘王宏仁‘, ‘C‘ => 800, ), 17 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘何丹美‘, ‘C‘ => 1300, ), )
需要导入的 Excel 文件如下:
// 导出 Excel public function export() { vendor(‘PHPExcel.Classes.PHPExcel‘); $Excel = new \PHPExcel(); $arr = array ( 1 => array ( ‘A‘ => ‘分公司名称‘, ‘B‘ => ‘姓名‘, ‘C‘ => ‘金额‘, ), 2 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘赵娟‘, ‘C‘ => 1100, ), 3 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘孔坚‘, ‘C‘ => 1100, ), 4 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘王华发‘, ‘C‘ => 1300, ), 5 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘赵辉‘, ‘C‘ => 700, ), 6 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘华发‘, ‘C‘ => 1400, ), 7 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘赵德国‘, ‘C‘ => 700, ), 8 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘沈芳虹‘, ‘C‘ => 500, ), 9 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘周红玉‘, ‘C‘ => 1100, ), 10 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘施芬芳‘, ‘C‘ => 800, ), 11 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘蒋国建‘, ‘C‘ => 1100, ), 12 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘钱毅‘, ‘C‘ => 1400, ), 13 => array ( ‘A‘ => ‘B分公司‘, ‘B‘ => ‘陈华惠‘, ‘C‘ => 1200, ), 14 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘曹香‘, ‘C‘ => 1400, ), 15 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘郑红妙‘, ‘C‘ => 600, ), 16 => array ( ‘A‘ => ‘A分公司‘, ‘B‘ => ‘王宏仁‘, ‘C‘ => 800, ), 17 => array ( ‘A‘ => ‘C分公司‘, ‘B‘ => ‘何丹美‘, ‘C‘ => 1300, ), ); // 设置 $Excel ->getProperties() ->setCreator("dee") ->setLastModifiedBy("dee") ->setTitle("数据EXCEL导出") ->setSubject("数据EXCEL导出") ->setDescription("数据EXCEL导出") ->setKeywords("excel") ->setCategory("result file"); foreach($arr as $key => $val) { // 注意 key 是从 0 还是 1 开始,此处是 0 // $num = $key + 1; $Excel ->setActiveSheetIndex(0) //Excel的第A列,uid是你查出数组的键值,下面以此类推 ->setCellValue(‘A‘.$key, $val[‘A‘]) ->setCellValue(‘B‘.$key, $val[‘B‘]) ->setCellValue(‘C‘.$key, $val[‘C‘]); } $Excel->getActiveSheet()->setTitle(‘export‘); $Excel->setActiveSheetIndex(0); $name=‘example_export.xlsx‘; header(‘Content-Type: application/vnd.ms-excel‘); header(‘Content-Disposition: attachment; filename=‘.$name); header(‘Cache-Control: max-age=0‘); $ExcelWriter = \PHPExcel_IOFactory::createWriter($Excel, ‘Excel2007‘); $ExcelWriter->save(‘php://output‘); exit; }
导出的 example_export.xlsx 如图:
右键 -- 属性:
参考:
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
ThinkPHP 3.2.3 使用 PHPExcel 处理 Excel 表格
标签:
原文地址:http://www.cnblogs.com/dee0912/p/5460072.html