1.åºäºphpä¸ä½¿ç¨excelçç®åä»ç»
2.php生成excel完整实例代码
3.PHPå¦ä½å¯¼å
¥å¯¼åºExcel
åºäºphpä¸ä½¿ç¨excelçç®åä»ç»
å¨å·¥ä½ä¸éè¦å¤çå¤è¯è¨çç¿»è¯é®é¢ï¼ç¿»è¯é½æ¯åå¨excelè¡¨æ ¼éé¢ã为äºå¤çæ¹ä¾¿æå°±ä¿çä¸æåè±æ两åãè¿æ ·éè¦å°è¿äºæ°æ®ä»excelä¸ååºæ¥ï¼ç¶ä¹ä¿åå¨excelçæ°ç»ä¸ï¼éè¿ä½¿ç¨å¾ªç¯æ°ç»å°ç¸åºçæ°æ®æ¾å ¥å°æ°æ®åºä¸ã
æ以工ä½ç第ä¸æ¥å°±æ¯è¦å°æ°æ®ä»excelä¸ååºæ¥ãè¿éæ使ç¨å°äºä¸ä¸ªå¼æºphpå¤çexcelç±»ï¼phpexcel.
该项ç®ç详ç»ä¿¡æ¯
/
ã
æç®å使ç¨çæ¯phpexcel1.7.3çæ¬,导导入
解å缩åéé¢æä¸ä¸ªPHPExcelåPHPExcel.phpæ件ã
æ们主è¦ä½¿ç¨é£ä¸ªPHPæ件ãè§ä¸å¾æ件ç®å½ç»æ
è¿ä¸ªçæ¬æ®è¯´æ¯å¯ä»¥æ¯æexcelï¼ä½æ¯æ使ç¨ç¼è¾çxlsxæ¯æ æ³è·å¾è¯¥åºçæ¯æãäºæ¯ä¹æå°±å°å®è½¬å为ãæè§æ¯æå°å¾å¥½ã
ä¸é¢ä»ç»ä¸ä¸å ·ä½ç使ç¨ï¼
å¤å¶ä»£ç
代ç å¦ä¸:
require_once('./phpexcel1.7.3/PHPExcel.php');
$php_excel_obj
=
new
PHPExcel();
$php_reader
=
newPHPExcel_Reader_Excel();
if(!$php_reader->canRead($file_name)){
$php_reader=
new
PHPExcel_Reader_Excel5();
if(!$php_reader->canRead($file_name)){
echo'NO
Excel!';
}
}
$php_excel_obj
=
$php_reader->load($file_name);
$current_sheet
=$php_excel_obj->getSheet(0);
ä¸é¢ç主è¦åè½æ¯åå§åç¸å ³çexcelç±»ï¼å¹¶è£ è½½excel第ä¸ä¸ªsheet
å¤å¶ä»£ç
代ç å¦ä¸:
$all_column
=$current_sheet->getHighestColumn();
$all_row
=$current_sheet->getHighestRow();
以ä¸åå«è·å¾è¯¥è¡¨æ ¼çæ大åå¼(åæ¯è¡¨ç¤ºå¦ï¼âG'),åæ大çè¡æ°ï¼æ°å¼è¡¨ç¤ºï¼
ä¸é¢å°ä½¿ç¨å¾ªç¯æ¥è®²exceléé¢çæ°æ®è¯»å°excelä¸ï¼
å¤å¶ä»£ç
代ç å¦ä¸:
$all_arr
=
array();
$c_arr
=
array();
//åç¬¦å¯¹ç §è¡¨
for($r_i
=
1;
$r_i<=$all_row;
$r_i++){
$c_arr=
array();
for($c_i=
'A';
$c_i<=
'B';
$c_i++){
$adr=
$c_i
.
$r_i;
$value=
$current_sheet->getCell($adr)->getValue();
if($c_i==
'A'
&&
empty($value)
)
break;
if(is_object($value))
$value=
$value->__toString();
$c_arr[$c_i]=
$value;
}
$c_arr&&
$all_arr[]
=
$c_arr;
}
ä¸é¢ç®åå°ä»ç»ä¸ä¸phpexcelçåæä½ï¼è¿ä¸ªæä½ç»å¸¸ç¨äºå°æ°æ®åºä¸çæ°æ®å¯¼å ¥å°excelä¸ï¼ä¾¿äºå±ç¤ºååææ´ç¾è§çææã
å¤å¶ä»£ç
代ç å¦ä¸:
require_once('./phpexcel1.7.3/PHPExcel.php');
$excel_obj
=
new
PHPExcel();
$objWriter
=
newPHPExcel_Writer_Excel5($excel_obj);
$excel_obj->setActiveSheetIndex(0);
$act_sheet_obj=$excel_obj->getActiveSheet();
$act_sheet_obj->setTitle('sheet');
$act_sheet_obj->setCellValue('A1',
'å符串å 容');
$act_sheet_obj->setCellValue('A2',
);
$file_name
=
"output.xls";
$objWriter->save($file_name);
代ç å¾ç®åï¼
é¦å åå§åç¸å ³çexcelåç±»ï¼ç¶ååå ¥æ°æ®ï¼æåä¿å为xlsæ件ã
è¾åºçææè§å¾
php生成excel完整实例代码
下载phpexcel类库 代码如下:
<?php
require_once('PHPExcel.php');
require_once('PHPExcel/IOFactory.php');
$objPHPExcel=new PHPExcel();
$iofactory=new IOFactory();
//获得数据 ---一般是从数据库中获得数据
$data=array(
0=>array('id'=>,'name'=>'张某某','age'=>),
1=>array('id'=>,'name'=>'EVA','age'=>)
);
//设置excel列名
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1','编号');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1','姓名');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1','年龄');
//把数据循环写入excel中
foreach($data as $key => $value){
$key+=2;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$key,$value['id']);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$key,$value['name']);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$key,$value['age']);
}
//excel保存在根目录下 如要导出文件,以下改为注释代码
$objPHPExcel->getActiveSheet() -> setTitle('SetExcelName');
$objPHPExcel-> setActiveSheetIndex(0);
$objWriter = $iofactory -> createWriter($objPHPExcel,源码钓鱼源码iapp 'Excel');
$objWriter -> save('SetExcelName.xlsx');
//导出代码
// $objPHPExcel->getActiveSheet() -> setTitle('SetExcelName');
// $objPHPExcel-> setActiveSheetIndex(0);
// $objWriter = $iofactory -> createWriter($objPHPExcel, 'Excel');
// $filename = 'SetExcelName.xlsx';
// header('Content-Type: application/vnd.ms-excel');
// header('Content-Type: application/octet-stream');
// header('Content-Disposition: attachment; filename="' . $filename . '"');
// header('Cache-Control: max-age=0');
// $objWriter -> save('php://output');
>PHPå¦ä½å¯¼å ¥å¯¼åºExcel
å¯¼å ¥å¯¼åºé½æï¼å¯ä»¥å¯¼åºofficeæ ¼å¼ï¼åæ¶å ¼å®¹ã
ä¸è½½ä¸æ¥çå ä¸æææ¡£åä¾åï¼å¤§å®¶å¯ä»¥èªè¡ç 究ã
æ段ä¾ååºæ¥ï¼
PHP代ç
<?php
/**
* PHPExcel
*
* Copyright (C) - PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., Franklin Street, Fifth Floor, Boston, MA - USA
*
* @category PHPExcel
* @package PHPExcel
* @copyright Copyright (c) - PHPExcel
* @license
* @version 1.5.0, --
*/
/** Error reporting */
error_reporting(E_ALL);
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . â../Classes/â);
/** PHPExcel */
include âPHPExcel.phpâ;
/** PHPExcel_Writer_Excel */
include âPHPExcel/Writer/Excel.phpâ;
// Create new PHPExcel object
echo date(âH:i:sâ) . â Create new PHPExcel object\nâ;
$objPHPExcel = new PHPExcel();
// Set properties
echo date(âH:i:sâ) . â Set properties\nâ;
$objPHPExcel->getProperties()->setCreator(âMaarten Balliauwâ);
$objPHPExcel->getProperties()->setLastModifiedBy(âMaarten Balliauwâ);
$objPHPExcel->getProperties()->setTitle(âOffice XLSX Test Documentâ);
$objPHPExcel->getProperties()->setSubject(âOffice XLSX Test Documentâ);
$objPHPExcel->getProperties()->setDescrÄ«ption(âTest document for Office XLSX, generated using PHP classes.â);
$objPHPExcel->getProperties()->setKeywords(âoffice openxml phpâ);
$objPHPExcel->getProperties()->setCategory(âTest result fileâ);
// Add some data
echo date(âH:i:sâ) . â Add some data\nâ;
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue(âA1â², âHelloâ);
$objPHPExcel->getActiveSheet()->setCellValue(âB2â², âworld!â);
$objPHPExcel->getActiveSheet()->setCellValue(âC1â², âHelloâ);
$objPHPExcel->getActiveSheet()->setCellValue(âD2â², âworld!â);
// Rename sheet
echo date(âH:i:sâ) . â Rename sheet\nâ;
$objPHPExcel->getActiveSheet()->setTitle(âSimpleâ);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel file
echo date(âH:i:sâ) . â Write to Excel format\nâ;
$objWriter = new PHPExcel_Writer_Excel($objPHPExcel);
$objWriter->save(str_replace(â.phpâ, â.xlsxâ, __FILE__));
// Echo done
echo date(âH:i:sâ) . â Done writing file.\r\nâ;