PHPexcel導(dǎo)入導(dǎo)出的簡單使用

2023-03-18    分類: 網(wǎng)站建設(shè)

使用php的時(shí)候一定會(huì)遇到表格導(dǎo)入導(dǎo)出的功能吧,這里簡單來解釋一下PHP中PHPExcel的使用如何來導(dǎo)入導(dǎo)出數(shù)據(jù)。

首先要去PHPExcel官網(wǎng)下載
這是我項(xiàng)目中的phpexcel類庫
先說一下導(dǎo)入 主要是要通過文件上傳,然后獲取到這個(gè)上傳的文件 然后可以用PHPExcel的方法去讀取文件的內(nèi)容 并返回一個(gè)數(shù)組,然后拼裝你要的數(shù)據(jù)進(jìn)行數(shù)據(jù)庫操作
這里我用的封裝好的方法去讀取的excel文件的數(shù)據(jù)
$filename文件的地址 需要一個(gè)相對(duì)路徑
$begin 從第幾行開始讀取 (這里一般是第二行 第一行基本是內(nèi)容的標(biāo)題或者描述)

$allColumn數(shù)據(jù)最后一列的字母 比如第G列是最后的數(shù)據(jù)列 就傳G
封裝方法:
function excelImport($filename, $begin,$allColumn) {
Vendor('PHPExcel.PHPExcel');
//建立reader對(duì)象
$PHPReader = new PHPExcel_Reader_Excel2007();
if (!$PHPReader->canRead($filename)) {
$PHPReader = new PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($filename)) {
return array();
}
}
//建立excel對(duì)象,此時(shí)你即可以通過excel對(duì)象讀取文件,也可以通過它寫入文件
$PHPExcel = $PHPReader->load($filename);
/* * 讀取excel文件中的第一個(gè)工作表 */
$currentSheet = $PHPExcel->getSheet(0);
/* * 取得大的列號(hào) */
// $allColumn = $currentSheet->getHighestColumn();
/* * 取得一共有多少行 */
$allRow = $currentSheet->getHighestRow();
$returnCell = '';
//循環(huán)讀取每個(gè)單元格的內(nèi)容。注意行從1開始,列從A開始
for ($rowIndex = $begin; $rowIndex <= $allRow; $rowIndex++) {
for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
$addr = $colIndex . $rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if ($cell instanceof PHPExcel_RichText) {
//富文本轉(zhuǎn)換字符串
$returnCell[$rowIndex][$colIndex] = $cell->__toString();
} else {
$returnCell[$rowIndex][$colIndex] = $cell;
}
}
}
return $returnCell;

}

讀取后會(huì)輸出一個(gè)數(shù)組,如下

獲取到數(shù)據(jù)后使用循環(huán)取出里面的數(shù)據(jù) 完成后清空了一下數(shù)據(jù)數(shù)組
這是簡單兩列數(shù)據(jù)的導(dǎo)入 .
再來說一下導(dǎo)出
我也是使用的PHPExcel

這里是先創(chuàng)建一個(gè)表格第一行的標(biāo)題數(shù)組和導(dǎo)出的文件名稱

一個(gè)一維數(shù)組鍵名要對(duì)應(yīng)表格列的字母
下面是要拼裝導(dǎo)出數(shù)據(jù)

這是一個(gè)二維數(shù)組 需要 每行對(duì)應(yīng)響應(yīng)的列的字母
這里給了一個(gè)key值來確定數(shù)據(jù)是從第幾行開始寫入的

因?yàn)橛袠?biāo)題的存在所以是從第二行開始如正式數(shù)據(jù),
使用下面這個(gè)封裝的導(dǎo)出方法進(jìn)行導(dǎo)出 ,會(huì)自動(dòng)使用瀏覽器下載你要導(dǎo)出的表格
這里也封裝了一個(gè)導(dǎo)出的方法
/**
* 導(dǎo)出成Excel文件
* @param type $filename 文件名
* @param type $data_title 頭部字段
* @param type $data_content 內(nèi)容
* @param type $Sheet1
* @param type $save_to_file 是否保存成文件,true保存
* @param type $save_path 文件保存路徑
*/
function excelExport($filename, $data_title, $data_content, $Sheet1 = "Sheet1", $save_to_file = false, $save_path = './Static/File/') {
Vendor('PHPExcel.PHPExcel');
//PHPExcel支持讀模版 所以我還是比較喜歡先做好一個(gè)Excel的模版 比較好,不然要寫很多代碼 模版我放在根目錄了
//創(chuàng)建一個(gè)對(duì)象
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("qiandai");
//獲取當(dāng)前活動(dòng)的表
$objActSheet = $objPHPExcel->setActiveSheetIndex(0);
$objActSheet->setTitle($Sheet1); //工作表標(biāo)簽
foreach ($data_title as $data_title_key => $data_title_value) {//寫入文件表頭內(nèi)容
$objActSheet->setCellValue($data_title_key.'1', $data_title_value);
$objPHPExcel->getActiveSheet()->getColumnDimension($data_title_key)->setWidth(30);//設(shè)置列的寬度
}
//現(xiàn)在就開始填充數(shù)據(jù)了 (從數(shù)據(jù)庫中) $data_content
foreach ($data_content as $data_content_value) {//寫入 文件內(nèi)容
foreach ($data_content_value as $data_content_key => $data_content_value2) {
$objActSheet->setCellValue($data_content_key, $data_content_value2);
}
}
//導(dǎo)出
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
if ($save_to_file) {
$objWriter->save($save_path . $filename);
} else {
$objWriter->save('php://output');
exit;
}
}
如果有什么方法不清楚或者想修改表格的其他設(shè)置的可以去查看手冊進(jìn)行添加。

網(wǎng)站題目:PHPexcel導(dǎo)入導(dǎo)出的簡單使用
文章轉(zhuǎn)載:http://www.bm7419.com/news24/245424.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、App設(shè)計(jì)、全網(wǎng)營銷推廣、響應(yīng)式網(wǎng)站、做網(wǎng)站、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)