Home > php教程 > php手册 > body text

Thinkphp+phpexcel导出和导入

WBOY
Release: 2016-06-07 11:38:29
Original
1232 people have browsed it

在网上找了很多excel导出和导入示例,代码不是很简洁,很多出错,自己在excel方法和控制器改了下。
首先我们看下导出的HTML<a>导出</a>接着我们看下Index/excelExport 导出的PHP代码:$list = M("user")->field("id,username,password")->order("id DESC")->limit(50)->select(); <br> $title = array('ID', '用户名', '密码'); //设置要导出excel的表头 <br> exportExcel($list, '素材火', $title);exportExcel方法如下:function exportExcel($data, $savefile = null, $title = null, $sheetname = 'sheet1') { <br>     import("Org.Util.PHPExcel"); <br>     //若没有指定文件名则为当前时间戳 <br>     if (is_null($savefile)) { <br>         $savefile = time(); <br>     } <br>     //若指字了excel表头,则把表单追加到正文内容前面去 <br>     if (is_array($title)) { <br>         array_unshift($data, $title); <br>     } <br>     $objPHPExcel = new \PHPExcel(); <br>     //Excel内容 <br>     $head_num = count($data); <br>  <br>     foreach ($data as $k => $v) { <br>         $obj = $objPHPExcel->setActiveSheetIndex(0); <br>         $row = $k + 1; //行 <br>         $nn = 0; <br>  <br>         foreach ($v as $vv) { <br>             $col = chr(65 + $nn); //列 <br>             $obj->setCellValue($col . $row, $vv); //列,行,值 <br>             $nn++; <br>         } <br>     } <br>     //设置列头标题 <br>     for ($i = 0; $i          $alpha = chr(65 + $i); <br>         $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //单元宽度自适应  <br>         $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setName("Candara");  //设置字体 <br>         $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setSize(12);  //设置大小 <br>         $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //设置颜色 <br>         $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 <br>         $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 <br>         $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setBold(true); //加粗 <br>     } <br>  <br>     $objPHPExcel->getActiveSheet()->setTitle($sheetname); //题目 <br>     $objPHPExcel->setActiveSheetIndex(0); //设置当前的sheet   <br>     header('Content-Type: application/vnd.ms-excel'); <br>     header('Content-Disposition: attachment;filename="' . $savefile . '.xls"'); //文件名称 <br>     header('Cache-Control: max-age=0'); <br>     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //Excel5 Excel2007 <br>     $objWriter->save('php://output'); <br> }接着我们看下导入的表单:<form> <br>     <div> <br>         <label>Excel表格:</label> <br>         <input> <br>     </div> <br>     <div> <br>         <input> <br>     </div> <br> </form>thinkphp上传Index/upload:$upload = new \Think\Upload(); // 实例化上传类 <br> $upload->maxSize = 3145728; // 设置附件上传大小 <br> $upload->exts = array('xls', 'xlsx'); // 设置附件上传类 <br> $upload->savePath = '/'; // 设置附件上传目录 <br> // 上传文件 <br> $info = $upload->uploadOne($_FILES['file']); <br> $filename = 'Uploads' . $info['savepath'] . $info['savename']; <br> $exts = $info['ext']; <br> if (!$info) {// 上传错误提示错误信息 <br>     $this->error($upload->getError()); <br> } else {// 上传成功 <br>     $this->goods_import($filename, $exts); <br> }goods_import导入方法:function goods_import($filename, $exts = 'xls') { <br>     //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入 <br>     import("Org.Util.PHPExcel"); <br>     //创建PHPExcel对象,注意,不能少了\ <br>     $PHPExcel = new \PHPExcel(); <br>     //如果excel文件后缀名为.xls,导入这个类 <br>  <br>     if ($exts == 'xls') { <br>         import("Org.Util.PHPExcel.Reader.Excel5"); <br>         $PHPReader = new \PHPExcel_Reader_Excel5(); <br>     } else if ($exts == 'xlsx') { <br>         import("Org.Util.PHPExcel.Reader.Excel2007"); <br>         $PHPReader = new \PHPExcel_Reader_Excel2007(); <br>     } <br>     //载入文件 <br>     $PHPExcel = $PHPReader->load($filename); <br>     //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 <br>     $currentSheet = $PHPExcel->getSheet(0); <br>     //获取总列数 <br>     $allColumn = $currentSheet->getHighestColumn(); <br>     //获取总行数 <br>     $allRow = $currentSheet->getHighestRow(); <br>     //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始 <br>     for ($currentRow = 1; $currentRow          //从哪列开始,A表示第一列 <br>         for ($currentColumn = 'A'; $currentColumn              //数据坐标 <br>             $address = $currentColumn . $currentRow; <br>             //读取到的数据,保存到数组$arr中 <br>             $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue(); <br>         } <br>     } <br>     $this->save_import($data); <br> }phpexcel导入和导出演示下载:http://www.sucaihuo.com/php/140.html

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!