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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用 PHP 7 和 PHPExcel 库编写一个导出 1 万行数据的功能的实例的步骤: 第一步:安装 PHPExcel 库 使用 Composer 安装 PHPExcel 库,执行以下命令: ``` composer require phpoffice/phpexcel ``` 第二步:查询数据 在控制器方法中查询需要导出数据,例如: ```php use app\index\model\User; $users = User::limit(10000)->select()->toArray(); ``` 这里使用了一个简单的 User 模型来查询用户数据。 第三步:创建 Excel 文件 使用 PHPExcel 库创建一个 Excel 文件,例如: ```php use PHPExcel; use PHPExcel_IOFactory; $excel = new PHPExcel(); $sheet = $excel->getActiveSheet(); // 设置表头 $sheet->setCellValue('A1', 'ID') ->setCellValue('B1', 'Name') ->setCellValue('C1', 'Email'); // 设置数据 foreach ($users as $key => $user) { $row = $key + 2; // 数据行从第 2 行开始 $sheet->setCellValue('A' . $row, $user['id']) ->setCellValue('B' . $row, $user['name']) ->setCellValue('C' . $row, $user['email']); } ``` 这里设置了表头和数据,注意数据行从第 2 行开始。 第四步:设置 Excel 文件格式并下载 使用 PHPExcel 库设置 Excel 文件的格式,并将文件下载到客户端,例如: ```php $filename = 'users.xlsx'; // 设置文件格式 $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save($filename); // 下载文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); readfile($filename); unlink($filename); // 删除文件 ``` 这里将 Excel 文件保存为 .xlsx 格式,并将文件下载到客户端。 以上就是用 PHP 7 和 PHPExcel 库编写一个导出 1 万行数据的功能的实例的步骤,希望对你有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值