下面是我寫的一個(gè)PHP導(dǎo)出數(shù)據(jù)到CSV問(wèn)價(jià)的函數(shù),你到時(shí)候直接調(diào)用就行了

創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元三臺(tái)做網(wǎng)站,已為上家服務(wù),為三臺(tái)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
/**
*?導(dǎo)出CSV文件
*?@param?string?$fileName 文件名字
*?@param?string|array?$data?導(dǎo)出數(shù)據(jù),csv格式的字符串|數(shù)值數(shù)組
*?@param?string?$to_encoding?目標(biāo)轉(zhuǎn)換編碼
*?@param?string?$from_encoding?當(dāng)前編碼
*/
function?exportCSV($fileName?=?'',?$data?=?'',?$to_encoding?=?'gb2312',?$from_encoding?=?'utf-8')?{
$fileName?=?empty($fileName)???date('YmdHis')?:?$fileName;
//?文件標(biāo)簽
Header("Content-type:?application/octet-stream");
header("Content-type:?application/vnd.ms-excel;?charset=$from_encoding");
Header("Content-Disposition:?attachment;?filename=$fileName.csv");
$str?=?'';
if($data)?{
if(is_array($data))?{
foreach?($data?as?$v)?{
if(is_array($v))?{
foreach?($v?as?$vo)?{
$str?.=?(is_numeric($vo)???"'".$vo?:?$vo."").",";
}
$str?=?trim($str,?",")."\r\n";
}?else?{
$str?.=?(is_numeric($v)???"'".$v?:?$v).",";
}
}
$str?=?trim($str,?",")."\r\n";
}?else?{
$str?=?$data;
}
}
echo?mb_convert_encoding($str,?"gb2312",?"utf-8");
exit;
}
$data?=?new?Spreadsheet_Excel_Reader();//實(shí)例化????
02????????????????$data-setOutputEncoding('utf-8');//設(shè)置讀取編碼????
03????????????????$data-read($p);//$p就是excel文件路徑????
04?????????????????????
05????????????????for?($i?=?2;?$i?=?$data-sheets[0]['numRows'];?$i++)?{????
06????????????????????//傳如數(shù)組,將一行信息寫入數(shù)據(jù)庫(kù);????
07????????????????????$arr?=?$data-sheets[0]['cells'][$i];????
08????????????????????if($arr){????
09????????????????????????$r?=?addUserLine($arr,$posts['bid']);????
10????????????????????????if($r){????
11????????????????????????echo?"樓棟:".$r['bldgname']."?房間:".$r['roomname']."?學(xué)員:".$r['name']."?學(xué)號(hào):".$r['sid']."?導(dǎo)入成功br?/hr?/";????
12????????????????????????}else{????
13????????????????????????????echo?"br?/hr?/";????
14????????????????????????}????
15????????????????????}????
16????????????????}
thinkphp3.2和phpexcel導(dǎo)入最基本用法
先整個(gè)最基礎(chǔ)的代碼,理解了這個(gè),后面的就非常簡(jiǎn)單了
$file_name=?'./Upload/excel/123456.xls';
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.IOFactory");
$objReader?=?\PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel?=?$objReader-load($file_name,$encode='utf-8');
$sheet?=?$objPHPExcel-getSheet(0);
$highestRow?=?$sheet-getHighestRow();?//?取得總行數(shù)
$highestColumn?=?$sheet-getHighestColumn();?//?取得總列數(shù)
$s?=?$objPHPExcel-getActiveSheet()-getCell("A2")-getValue();
表格內(nèi)容:
再給大家整個(gè)一點(diǎn)難度的,先說(shuō)下思路。
1.上傳excel文件,得到它的地址
2.寫個(gè)處理exl的function,即可
實(shí)例代碼演示:
public?function?upload(){
$files?=?$_FILES['exl'];
//?exl格式,否則重新上傳
if($files['type']?!='application/vnd.ms-excel'){
$this-error('不是Excel文件,請(qǐng)重新上傳');????
}
//?上傳
$upload?=?new?\Think\Upload();//?實(shí)例化上傳類
$upload-maxSize???=?????3145728?;//?設(shè)置附件上傳大小
$upload-exts??????=?????array('xls');//?設(shè)置附件上傳類型
$upload-rootPath??=?????'./Upload/';?//?設(shè)置附件上傳根目錄
$upload-savePath??=?????'excel/';?//?設(shè)置附件上傳(子)目錄
//$upload-subName???=?????array('date',?'Ym');
$upload-subName???=?????'';
//?上傳文件??
$info???=???$upload-upload();
$file_name?=??$upload-rootPath.$info['exl']['savepath'].$info['exl']['savename'];
$exl?=?$this-import_exl($file_name);
//?去掉第exl表格中第一行
unset($exl[0]);
//?清理空數(shù)組
foreach($exl?as?$k=$v){
if(empty($v)){
unset($exl[$k]);
}????
};
//?重新排序
sort($exl);
$count?=?count($exl);
//?檢測(cè)表格導(dǎo)入成功后,是否有數(shù)據(jù)生成
if($count1){
$this-error('未檢測(cè)到有效數(shù)據(jù)');????
}
//?開(kāi)始組合數(shù)據(jù)
foreach($exl?as?$k=$v){
$goods[$k]['goods_sn']?=?$v;
//?查詢數(shù)據(jù)庫(kù)
$where['goods_sn']?=?array('like','%'.$v.'%');
$res?=?M('goods')-where($where)-find();
$goods[$k]['goods_name']?=?$res['goods_name'];
$goods[$k]['goods_thumb']?=?$res['goods_thumb'];
if($res){
//?是否匹配成功??
$goods[$k]['is_match']????=?'1';
$f?+=?1;
}else{
//?匹配失敗
$goods[$k]['is_match']????=?'0';
$w?+=?1;
}
}
//?實(shí)例化數(shù)據(jù)
$this-assign('goods',$goods);
//print_r($f);
//?統(tǒng)計(jì)結(jié)果
$total['count']?=?$count;
$total['success']?=?$f;
$total['error']?=?$w;
$this-assign('total',$total);
//?刪除Excel文件
unlink($file_name);
$this-display('info');
}
/*?處理上傳exl數(shù)據(jù)
*?$file_name??文件路徑
*/
public?function?import_exl($file_name){
//$file_name=?'./Upload/excel/123456.xls';
import("Org.Util.PHPExcel");???//?這里不能漏掉
import("Org.Util.PHPExcel.IOFactory");
$objReader?=?\PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel?=?$objReader-load($file_name,$encode='utf-8');
$sheet?=?$objPHPExcel-getSheet(0);
$highestRow?=?$sheet-getHighestRow();?//?取得總行數(shù)
$highestColumn?=?$sheet-getHighestColumn();?//?取得總列數(shù)
for($i=1;$i$highestRow+1;$i++){
$data[]?=?$objPHPExcel-getActiveSheet()-getCell('A'.$i)-getValue();????
}
return?$data;????
}
有問(wèn)題一定要及時(shí)弄清楚
網(wǎng)頁(yè)標(biāo)題:php導(dǎo)入表格數(shù)據(jù)類型,php導(dǎo)入表格數(shù)據(jù)類型不對(duì)
文章來(lái)源:http://chinadenli.net/article18/dsggedp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、企業(yè)建站、全網(wǎng)營(yíng)銷推廣、網(wǎng)站內(nèi)鏈、網(wǎng)站營(yíng)銷、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)