php中使用phpexcel操作excel(xls)文件
來源:易賢網(wǎng) 閱讀:855 次 日期:2014-11-24 14:48:15
溫馨提示:易賢網(wǎng)小編為您整理了“php中使用phpexcel操作excel(xls)文件”,方便廣大網(wǎng)友查閱!

phpexcel是php的一個插件,它可以實現(xiàn)讀取excel文件也就是xls文件了,下面我們就來看一個phpexcel操作excel(xls)文件例子,希望能幫助到各位。

讀取中文的xls、csv文件會有問題,網(wǎng)上找了下資料,發(fā)現(xiàn)phpexcel類庫好用1、讀取xls文件內(nèi)容

代碼如下:

<?php

//向xls文件寫入內(nèi)容

error_reporting(e_all);

ini_set('display_errors', true);

include 'classes/phpexcel.php';

include 'classes/phpexcel/iofactory.php';

//$data:xls文件內(nèi)容正文

//$title:xls文件內(nèi)容標(biāo)題

//$filename:導(dǎo)出的文件名

//$data和$title必須為utf-8碼,否則會寫入false值

function write_xls($data=array(), $title=array(), $filename='report'){

$objphpexcel = new phpexcel();

//設(shè)置文檔屬性,設(shè)置中文會產(chǎn)生亂碼,待完善...

// $objphpexcel->getproperties()->setcreator(云舒)

// ->setlastmodifiedby(云舒)

// ->settitle(產(chǎn)品url導(dǎo)出)

// ->setsubject(產(chǎn)品url導(dǎo)出)

// ->setdescription(產(chǎn)品url導(dǎo)出)

// ->setkeywords(產(chǎn)品url導(dǎo)出);

$objphpexcel->setactivesheetindex(0);

$cols = 'abcdefghijklmnopqrstuvwxyz';

//設(shè)置標(biāo)題

for($i=0,$length=count($title); $i<$length; $i++) {

//echo $cols{$i}.'1';

$objphpexcel->getactivesheet()->setcellvalue($cols{$i}.'1', $title[$i]);

}

//設(shè)置標(biāo)題樣式

$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,支持關(guān)聯(lián)數(shù)組和數(shù)字索引數(shù)組

$j = 0;

foreach($d as $v) { //這里用foreach,支持關(guān)聯(lián)數(shù)組和數(shù)字索引數(shù)組

$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.baidu.com'),

array(1111,'名稱','品牌','商品名','http://www.baidu.com'),

array(1111,'名稱','品牌','商品名','http://www.baidu.com'),

array(1111,'名稱','品牌','商品名','http://www.baidu.com'),

array(1111,'名稱','品牌','商品名','http://www.baidu.com'),

);

write_xls($array,array('商品id','供應(yīng)商名稱','品牌','商品名','url'),'report');

?>

2、向xls文件寫內(nèi)容

代碼如下:

<?php

//獲取數(shù)據(jù)庫數(shù)據(jù)(mysqli預(yù)處理學(xué)習(xí))

$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());

}

//設(shè)置連接數(shù)據(jù)庫的編碼,不要忘了設(shè)置

$mysqli->set_charset(gbk);

//中文字符的編碼要與數(shù)據(jù)庫一致,若沒設(shè)置,結(jié)果為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碼(同數(shù)據(jù)庫編碼)

}

$id = getproductidbyname('%伊奈衛(wèi)浴伊奈分體座便器%');

var_dump($id);

?>

ok...

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php中使用phpexcel操作excel(xls)文件
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機(jī)號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)