本文實例講述了PHP中使用PHPExcel讀寫excel(xls)文件的方法,非常實用。分享給大家供大家參考之用。具體方法如下:
很多PHP類庫在讀取中文的xls、csv文件時會有問題,網上找了下資料,發現PHPExcel類庫好用,官網地址為:http://phpexcel.codeplex.com/。現將PHPExcel讀寫Excel的方法分別敘述如下:
1、讀取xls文件內容
<?php //向xls文件寫入內容 error_reporting(E_ALL); ini_set('display_errors', TRUE); include 'Classes/PHPExcel.php'; include 'Classes/PHPExcel/IOFactory.php'; //$data:xls文件內容正文 //$title:xls文件內容標題 //$filename:導出的文件名 //$data和$title必須為utf-8碼,否則會寫入FALSE值 function write_xls($data=array(), $title=array(), $filename='report'){ $objPHPExcel = new PHPExcel(); //設置文檔屬性,設置中文會產生亂碼,需要轉換成utf-8格式!! // $objPHPExcel->getProperties()->setCreator("雲舒") // ->setLastModifiedBy("雲舒") // ->setTitle("產品URL導出") // ->setSubject("產品URL導出") // ->setDescription("產品URL導出") // ->setKeywords("產品URL導出"); $objPHPExcel->setActiveSheetIndex(0); $cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //設置www.jb51.net標題 for($i=0,$length=count($title); $i<$length; $i++) { //echo $cols{$i}.'1'; $objPHPExcel->getActiveSheet()->setCellValue($cols{$i}.'1', $title[$i]); } //設置標題樣式 $titleCount = count($title); $r = $cols{0}.'1'; $c = $cols{$titleCount}.'1'; $objPHPExcel->getActiveSheet()->getStyle("$r:$c")->applyFromArray( array( 'font' => array( 'bold' => true ), 'alignment' => array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, ), 'borders' => array( 'top' => array( 'style' => PHPExcel_Style_Border::BORDER_THIN ) ), 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR, 'rotation' => 90, 'startcolor' => array( 'argb' => 'FFA0A0A0' ), 'endcolor' => array( 'argb' => 'FFFFFFFF' ) ) ) ); $i = 0; foreach($data as $d) { //這裡用foreach,支持關聯數組和數字索引數組 $j = 0; foreach($d as $v) { //這裡用foreach,支持關聯數組和數字索引數組 $objPHPExcel->getActiveSheet()->setCellValue($cols{$j}.($i+2), $v); $j++; } $i++; } // 生成2003excel格式的xls文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); } $array = array( array(1111,'名稱','品牌','商品名','http://www.bkjia.com'), array(1111,'名稱','品牌','商品名','http://www.bkjia.com'), array(1111,'名稱','品牌','商品名','http://www.bkjia.com'), array(1111,'名稱','品牌','商品名','http://www.bkjia.com'), array(1111,'名稱','品牌','商品名','http://www.bkjia.com'), ); write_xls($array,array('商品id','供應商名稱','品牌','商品名','URL'),'report'); ?>
2、向xls文件寫內容
<?php //獲取數據庫數據(mysqli預處理學習) $config = array( 'DB_TYPE'=>'mysql', 'DB_HOST'=>'localhost', 'DB_NAME'=>'test', 'DB_USER'=>'root', 'DB_PWD'=>'root', 'DB_PORT'=>'3306', ); function getProductIdByName($name) { global $config; $id = false; $mysqli = new mysqli($config['DB_HOST'], $config['DB_USER'], $config['DB_PWD'], $config['DB_NAME']); if(mysqli_connect_error()) { //兼容 < php5.2.9 OO way:$mysqli->connect_error die("連接失敗,錯誤碼:".mysqli_connect_errno()."錯誤信息:".mysqli_connect_error()); } //設置連接數據庫的編碼,不要忘了設置 $mysqli->set_charset("gbk"); //中文字符的編碼要與數據庫一致,若沒設置,結果為null $name = iconv("utf-8", "gbk//IGNORE", $name); if($mysqli_stmt = $mysqli->prepare("select id from 137_product where name like ?")) { $mysqli_stmt->bind_param("s", $name); $mysqli_stmt->execute(); $mysqli_stmt->bind_result($id); $mysqli_stmt->fetch(); $mysqli_stmt->close(); } $mysqli->close(); return $id; //得到的是gbk碼(同數據庫編碼) } $id = getProductIdByName('%伊奈衛浴伊奈分體座便器%'); var_dump($id); ?>
希望本文所述對大家的PHP程序設計有所幫助