PHPExcel导出数据时字段超过26列出错Invalid cell coordinate [1

http://blog.csdn.net/dl425134845/article/details/46650961

以下是解决方案函数

[php] view plain copy
 
  1. /** 
  2. *   方法名:    getExcel 
  3. *   作用  :将数据导出excel表格 
  4. *   @date   2015/03/26 
  5. *   @author dingling 
  6. *   @param1 文件名 
  7. *   @param2 字段名 
  8. *   @param3 数据 
  9. *   @return excel文件 
  10. */    
  11. function getExcel($fileName,$headArr,$data){  
  12.     //对数据进行检验  
  13.     if(empty($data) || !is_array($data)){  
  14.         die("数据必须为数组");  
  15.     }  
  16.     //检查文件名  
  17.     if(empty($fileName)){  
  18.         exit;  
  19.     }  
  20.     //组装文件名  
  21.     $date = date("Y_m_d",time());  
  22.     $fileName .= "_{$date}.xls";  
  23.   
  24.     error_reporting(E_ALL);  
  25.     ini_set('display_errors', TRUE);  
  26.     ini_set('display_startup_errors', TRUE);  
  27.     date_default_timezone_set('PRC');  
  28.   
  29.     if (PHP_SAPI == 'cli')  
  30.         die('只能通过浏览器运行');  
  31.       
  32.     //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入  
  33.     import("Org.Util.PHPExcel");  
  34.     import("Org.Util.PHPExcel.Writer.Excel5");  
  35.     import("Org.Util.PHPExcel.IOFactory.php");  
  36.       
  37.     //创建PHPExcel对象,注意,不能少了  
  38.     $objPHPExcel = new PHPExcel();  
  39.     $objProps = $objPHPExcel->getProperties();  
  40.       
  41.     //设置表头  
  42.     <span style="color:#FF0000;">$key = ord("A");//A--65  
  43.     $key2 = ord("@");//@--64</span>  
  44.     foreach($headArr as $v){  
  45.         <span style="color:#FF0000;">if($key>ord("Z")){  
  46.             $key2 += 1;  
  47.             $key = ord("A");  
  48.             $colum = chr($key2).chr($key);//超过26个字母时才会启用  dingling 20150626  
  49.         }else{  
  50.             if($key2>=ord("A")){  
  51.                 $colum = chr($key2).chr($key);  
  52.             }else{  
  53.                 $colum = chr($key);  
  54.             }  
  55.         }</span>  
  56.         $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', L($v['COMMENT']));  
  57.         $key += 1;  
  58.     }  
  59.       
  60.     $column = 2;  
  61.     $objActSheet = $objPHPExcel->getActiveSheet();  
  62.   
  63.     foreach($data as $key => $rows){ //行写入  
  64.         <span style="color:#FF0000;">$span = ord("A");  
  65.         $span2 = ord("@");</span>  
  66.         foreach($headArr as $k=>$v){  
  67.             <span style="color:#FF0000;">if($span>ord("Z")){  
  68.                 $span2 += 1;  
  69.                 $span = ord("A");  
  70.                 $j = chr($span2).chr($span);//超过26个字母时才会启用  dingling 20150626  
  71.             }else{  
  72.                 if($span2>=ord("A")){  
  73.                     $j = chr($span2).chr($span);  
  74.                 }else{  
  75.                     $j = chr($span);  
  76.                 }  
  77.             }</span>  
  78.             //$j = chr($span);  
  79.             $objActSheet->setCellValue($j.$column, strip_tags($rows[$v['FIELD']]));  
  80.             $span++;  
  81.         }  
  82.         $column++;  
  83.     }  
  84.   
  85.     $fileName = iconv("utf-8", "gb2312", $fileName);  
  86.       
  87.     // $objPHPExcel->getActiveSheet()->setTitle('CNLINK');// 重命名表  
  88.     $objPHPExcel->setActiveSheetIndex(0);// 设置活动单指数到第一个表,所以Excel打开这是第一个表   
  89.       
  90.       
  91.     // Redirect output to a client’s web browser (Excel5)  
  92.     header('Content-Type: application/vnd.ms-excel');  
  93.     header("Content-Disposition: attachment;filename="$fileName"");  
  94.     header('Cache-Control: max-age=0');  
  95.       
  96.     // If you're serving to IE 9, then the following may be needed  
  97.     header('Cache-Control: max-age=1');  
  98.     // If you're serving to IE over SSL, then the following may be needed  
  99.     header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past  
  100.     header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified  
  101.     header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1  
  102.     header ('Pragma: public'); // HTTP/1.0  
  103.   
  104.     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
  105.     // $objWriter->save('1.xls');  
  106.     // echo str_replace('.php', '.xls', __FILE__);  
  107.     $objWriter->save('php://output'); //文件通过浏览器下载  
  108.       
  109.     exit;  
  110. }  


红色部分是解决问题的关键

原文地址:https://www.cnblogs.com/wangluochong/p/7365620.html