// 導入excel
function import_excel($file){
// 判斷文件是什么格式
$type = pathinfo($file);
$type = strtolower($type["extension"]);
$type=$type==='csv' ? $type : 'Excel5';
ini_set('max_execution_time', '0');
Vendor('PHPExcel.PHPExcel');
// 判斷使用哪種格式
$objReader = PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($file);
$sheet = $objPHPExcel->getSheet(0);
// 取得總行數(shù)
$highestRow = $sheet->getHighestRow();
// 取得總列數(shù)
$highestColumn = $sheet->getHighestColumn();
//循環(huán)讀取excel文件,讀取一條,插入一條
$data=array();
//從第一行開始讀取數(shù)據(jù)
for($j=1;$j<=$highestRow;$j++){
//從A列讀取數(shù)據(jù)
for($k='A';$k<=$highestColumn;$k++){
// 讀取單元格
$data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
}
}
return $data;
}
// 導出excel
function export_excel($data,$filename='simple.xls'){
ini_set('max_execution_time', '0');
Vendor('PHPExcel.PHPExcel');
$filename=str_replace('.xls', '', $filename).'.xls';
$phpexcel = new PHPExcel();
$phpexcel->getProperties()
->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$phpexcel->getActiveSheet()->fromArray($data);
$phpexcel->getActiveSheet()->setTitle('Sheet1');
$phpexcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
$objwriter->save('php://output');
exit;
}
參考www.baijunyao.com
excel導入導出
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內容
- 今天來講下企業(yè)的開發(fā)的一些功能應用吧,就是Ajax使用,以及簡單的企業(yè)報表的導入導出,基于原有的項目進行展示(其實...
- 前言 本文由作者三汪首發(fā)于簡書。 前幾天發(fā)了一篇文章,提供了基于最新POI版本的Excel導出示例,提供了網(wǎng)上各個...
- 1. 使用 node-xlsx 包 node-xlsx: 基于Node.js解析excel文件數(shù)據(jù)及生成excel...
- 我們不造輪子,只是輪子的搬運工。(其實最好是造輪子,造比別人好的輪子) 開發(fā)中經(jīng)常會遇到excel的處理,導入導出...