色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

php thinkphp execl

韓華玲1年前6瀏覽0評論
PHP ThinkPHP Excel是一款功能強大的Excel組件,它為用戶提供了快速、可靠和高效的Excel讀寫能力。無論是數據的導入或者是數據的導出,ThinkPHP Excel都能滿足你對于Excel數據處理的全部需求。
我們來看下面一個例子:
php
//導出Excel
public function export_excel(){
$data = array(
array('id' => '1','name' => '張三','age' => '20','sex' => '男'),
array('id' => '2','name' => '李四','age' => '21','sex' => '女'),
array('id' => '3','name' => '王五','age' => '22','sex' => '男'),
array('id' => '4','name' => '趙六','age' => '23','sex' => '女'),
);
vendor('PHPExcel.PHPExcel');
$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");
/*以下就是對處理Excel里的數據,橫著取數據*/
$PHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', '姓名')
->setCellValue('C1', '年齡')
->setCellValue('D1', '性別');
$i = 2;
foreach($data as $key => $value){
$PHPExcel->getActiveSheet()->setCellValue('A'.$i, $value['id']);
$PHPExcel->getActiveSheet()->setCellValue('B'.$i, $value['name']);
$PHPExcel->getActiveSheet()->setCellValue('C'.$i, $value['age']);
$PHPExcel->getActiveSheet()->setCellValue('D'.$i, $value['sex']);
$i++;
}
$PHPExcel->getActiveSheet()->setTitle('用戶信息');
$PHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
header('Content-Disposition: attachment;filename="用戶信息.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
$objWriter->save('php://output');
}

上面的這段代碼就是一個導出Excel表格的例子。事實上,用ThinkPHP Excel來導入數據也是非常簡單的:
php
//導入Excel
public function import_excel(){
vendor('PHPExcel.PHPExcel');
$file = request()->file('excel');
if($file){
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
$extension = $info->getExtension();
$file_name = $info->getSaveName();
$file_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . $file_name;
/*以下是對上傳的Excel文件進行處理,如讀取數據*/
if($extension == 'xls'){
vendor('PHPExcel.PHPExcel.Reader.Excel5');
$objReader = new \PHPExcel_Reader_Excel5();
}else{
vendor('PHPExcel.PHPExcel.Reader.Excel2007');
$objReader = new \PHPExcel_Reader_Excel2007();
}
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file_path);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
$rowData = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col < $highestColumnIndex; ++$col) {
$cell = $sheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
$rowData[$row][] = $val;
}
}
$this->assign('userData',$rowData);
}
}
return $this->fetch();
}

我們先上傳一個Excel文件,然后在后臺進行讀取,最后將讀取的數據展示到頁面上。
總的來說,ThinkPHP Excel是一款非常實用的組件,為程序員們提供了方便、高效的數據處理方式。無論導入還是導出數據,ThinkPHP Excel都能為你提供極致的體驗。