PHPExcel 导入导出

来源:互联网 发布:超悦软件v8000 编辑:程序博客网 时间:2024/06/06 07:21
  1. // 数字转字母
  2.     function getLetter($num) {
  3.         $str = "$num";
  4.         $num = intval($num);
  5.         if ($num <= 26){
  6.             $ret = chr(ord('A') + intval($str) - 1);
  7.         } else {
  8.             $first_str = chr(ord('A') + intval(floor($num / 26)) - 1);
  9.             $second_str = chr(ord('A') + intval($num % 26) - 1);
  10.             if ($num % 26 == 0){
  11.                 $first_str = chr(ord('A') + intval(floor($num / 26)) - 2);
  12.                 $second_str = chr(ord('A') + intval($num % 26) + 25);
  13.             }
  14.             $ret = $first_str.$second_str;
  15.         }
  16.         return $ret;
  17.     }
  18.     
  19.     // excel 导入
  20.     /**
  21.      * 导入excel到数据库
  22.      * @param string $db 数据库表名
  23.      * @param path string 文件名(路径)
  24.      * @return boolean
  25.      */
  26.     function excelImport($db, $file) {
  27.         import("Org.Util.PHPExcel");
  28.         $PHPExcel = new PHPExcel();
  29.         
  30.         $PHPReader = new \PHPExcel_Reader_Excel2007();
  31.         if (!$PHPReader->canRead($file)) {
  32.             $PHPReader = new \PHPExcel_Reader_Excel5();
  33.             if (!$PHPReader->canRead($file)){
  34.                 return false;
  35.             }
  36.         }
  37.         
  38.         $E = $PHPReader->load($file);
  39.         $cur = $E->getSheet(0);  // 读取第一个表
  40.         $end = $cur->getHighestColumn(); // 获得最大的列数
  41.         $line = $cur->getHighestRow(); // 获得最大总行数
  42.         // 获取数据数组
  43.         $info = array();        
  44.         for ($row = 1; $row <= $line; $row ++) {
  45.             for ($column = 'A'; $column <= $end; $column ++) {                
  46.                 $val = $cur->getCellByColumnAndRow(ord($column) - 65, $row)->getValue();
  47.                 $info[$row][] = $val;
  48.             }
  49.         }
  50.         
  51.         $DB = M($db);
  52.         $data = array();
  53.         for ($i = 2; $i <= count($info); $i ++) {
  54.             for ($j = 0; $j < count($info[$i]); $j ++) {
  55.                 for ($k = 0; $k < count($info[1]); $k ++) {
  56.                     $data[$i][$info[1][$k]] = $info[$i][$k];
  57.                 }
  58.             }
  59.         }
  60.         $datalist = array_values($data);
  61.         $result = $DB->addAll($datalist);
  62.         // echo $DB->getLastSql();exit;
  63.         if ($result) {
  64.             return true;
  65.         }
  66.         return false;
  67.     }
  68.     
  69.     // 导出excel
  70.     /**
  71.      * 导出excel方法
  72.      * @param array $data 需要导出的数据
  73.      * @param array $title excel表头
  74.      * @param string $name 导出后的文件名
  75.      */
  76.     function excelExport ($data, $title=null, $name=null) {
  77.         import("Org.Util.PHPExcel");
  78.         $PHPExcel = new PHPExcel();
  79.         
  80.         if(!is_null($title)){
  81.             array_unshift($data, $title);
  82.         }
  83.         
  84.         if(is_null($name)){
  85.             $name = time();
  86.         }
  87.             
  88.         foreach ($data as $k => $v) {
  89.             for ($i = 1; $i <= count($v); $i++){
  90.                 $tr = getLetter($i).($k+1);
  91.                 if ($value == null) {
  92.                     $value = '';
  93.                 }
  94.                 $buffer[$tr]=array_values($v)[$i-1];
  95.                 $PHPExcel->getActiveSheet()->setCellValue($tr, array_values($v)[$i-1]);
  96.             }        
  97.         }
  98.         
  99.         $PHPExcel->setActiveSheetIndex(0);
  100.         header('Content-Type: application/vnd.ms-excel'); 
  101.         header('Content-Disposition: attachment;filename="' . $name . '.xls"'); //文件名称 
  102.         header('Cache-Control: max-age=0');
  103.         $result = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
  104.         $result->save('php://output');        
  105.     }
原创粉丝点击