2012年2月19日 星期日

phpExcel


include_once("XXX/phpexcel/PHPExcel/IOFactory.php");
 

 
 $colum=2;
 $num=count($sss);
 $objExcel=new PHPExcel();
 $objExcel->setActiveSheetIndex(0);
 $sheet=$objExcel->getActiveSheet();
  
 for($i=0;$i<$num;$i++){
  //if($data[$i]['enable']==0) continue; #因為sql裡面排除不了enable為0的,所以做此處理
  
  $sheet->setCellValueByColumnAndRow(0,$colum,$sss[$i]['Name']);
  $sheet->setCellValueByColumnAndRow(1,$colum,$sss[$i]['ID']);           
  $sheet->setCellValueByColumnAndRow(2,$colum,$sss[$i]['Time']);
  $sheet->setCellValueByColumnAndRow(3,$colum,$sss[$i]['eTime']); 
  $colum++;
 }

  $path=t_PATH."download/";
  $path="../download/";
//2003
  $objExcel=PHPExcel_IOFactory::createWriter($objExcel,'Excel5'); //針對2003的版本
  $filePath=$path."commodity.xls";     //注意副檔名
// Export to Excel2007 (.xlsx) 匯出成2007
  $objExcel=PHPExcel_IOFactory::createWriter($objExcel,'Excel2007'); //針對2003的版本
  $filePath=$path."commodity.xlsx";    //注意副檔名


  $objExcel->save($filePath);
  
  dl_file($filePath); //此功能應該要和save分開處理,避免產生錯誤

另一個主題:讓phpexcel只讀取column的文字,不會讀取其他的 字體設定,顏色設定。

錯誤碼:Cannot use object of type PHPExcel_RichText

原因:去抓 陣列裡的 PHPExcel_RichText ,要抓出我的的文字而不是字體之類的,就產生了錯誤。
$reader->setReadDataOnly(true);

在20120313時需要寫入大量的資料,遇到疑似記憶體不足,但沒顯示任何錯誤,抓bug抓了老半天,始終抓不到。
解法:放在程式最前面
ini_set( "memory_limit", "56M");


設定欄寬範例 : $sheet->getColumnDimension("A")->setWidth(50); //設定欄冠

沒有留言: