首先下載PHPEXCEL 下載地址:https://github.com/PHPOffice/PHPExcelphp
1、生成Excelhtml
<?php require "PHPExcel-1.8.0/Classes/PHPExcel.php";//引入PHPExcel加載文件 $obj_PHPExcel = new PHPExcel();//實例化PHPExcel類 等同於新建一個Excel表格 $obj_Sheet = $obj_PHPExcel->getActiveSheet(); //得到當前活動sheet的活動對象 //$obj_PHPExcel->createSheet();//能夠循環建立多個sheet,在創建多個sheet時使用 //$obj_PHPExcel->setActiveSheetIndex(0);//設置爲活動sheet 從0開始 //$obj_Sheet = $obj_PHPExcel->getActiveSheet(); //得到當前活動sheet的活動對象 $obj_Sheet->setTitle('s1');//設置當前活動Sheet名稱 //1.逐個單元格進行填充 //$obj_Sheet->setCellValue("A1", "姓名")->setCellValue("B1", "分數");//填充數據 //$obj_Sheet->setCellValue("A2", "張三")->setCellValue("B2", "50");//填充數據 //$obj_PHPExcel->getActiveSheet()->mergeCells('A1:F1');//合併單元格A1:F1(起始座標,結束座標) //$obj_PHPExcel->getActiveSheet()->unmergeCells('A1:F1');// 拆分單元格 $obj_Sheet->getDefaultStyle()->getAlignment() //設置居中顯示 ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)//垂直居中 ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中 //2.傳入數組的方式填充 //$obj_Sheet->fromArray([ // ['標題1','標題2'], //第一個數組爲第一行,數組的第一個元素爲第一列 // ['1','2'], // ['3','4'], // ['5','6'], // ['7','8'], // ['9','0'], //]); $result = [ ['id' => 'ID號', 'name' => '用戶', 'pwd' => '密碼', 'addr' => '地址'], ['id' => '2w5s2525dw88ee8wq87', 'name' => 'test01', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'd15as6ds1d6as11das6', 'name' => 'test02', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'eq4wqw564e56wq46e4w', 'name' => 'test03', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'dasx4zx56c4x564c56a', 'name' => 'test04', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'we6qw456eq4w56e4q56', 'name' => 'test05', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'd4as56d1456q4545454', 'name' => 'test06', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'e4qw64e6qw46eq46447', 'name' => 'test07', 'pwd' => '123456', 'addr' => '廣州'], ['id' => '6e4q6we8qw7e89wq78e', 'name' => 'test08', 'pwd' => '123456', 'addr' => '廣州'], ['id' => 'e897e9qw87e98qw798e', 'name' => 'test09', 'pwd' => '123456', 'addr' => '廣州'], ]; $obj_Sheet->fromArray($result);//填充數組 $obj_Writer = PHPExcel_IOFactory::createWriter($obj_PHPExcel, 'Excel2007');//建立工廠對象 //操做1 保存文件 //$obj_Writer->save('demo.xlsx');//執行保存文件 //操做2 輸出瀏覽器 browser_export('demo', false); $obj_Writer->save('php://output'); //輸出到瀏覽器 判斷文件類型 function browser_export($filename, $type = 'Excel5'){ // Redirect output to a client’s web browser (Excel5) if ($type == 'Excel5') { $ext = '.xls'; header('Content-Type: application/vnd.ms-excel');//輸出excel03文件 } else { $ext = '.xlsx'; header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');//輸出excel07文件 } header('Content-Disposition: attachment;filename="'.$filename.$ext.'"');//輸出文件的名稱 header('Cache-Control: max-age=0');//禁止緩存 }
2、導入Excel前端
<?php header('Content-type:text/html;charset=UTF-8'); require "PHPExcel-1.8.0/Classes/PHPExcel/IOFactory.php";//引入讀取excel類文件 $filename = __DIR__.'/demo.xlsx';//須要導入的excel路徑 //1.部分加載 $file_type = PHPExcel_IOFactory::identify($filename);//讀取文件類型 $obj_reader = PHPExcel_IOFactory::createReader($file_type);//獲取文件操做對象 $obj_reader->setLoadSheetsOnly('s1');//只讀取sheet的名稱 多個能夠用數組array(sheet1,sheet2...) $obj_PHPExcel = $obj_reader->load($filename);//加載文件 //2.所有加載 //$obj_PHPExcel = PHPExcel_IOFactory::load($filename); //加載文件 //$sheet_count = $obj_PHPExcel->getSheetCount(); //獲取文件sheet數量 /*1.所有讀取 for ($i=0; $i < $sheet_count; $i++) { $data = $obj_PHPExcel->getSheet($i)->toArray();//讀取每一個sheet的數據放入數組中 var_dump($data); } */ /*2.逐行讀取*/ foreach ($obj_PHPExcel->getWorksheetIterator() as $sheet) { //循環讀取sheet foreach ($sheet->getRowIterator() as $row) { //逐行處理 //去除頭部,只顯示數據 if ($row->getRowIndex() < 2 ){ continue; } foreach ($row->getCellIterator() as $cell) { //逐列獲取 $data = $cell->getValue();//讀取單元格數據 echo $data.' '; } echo "<br>"; } echo "<br>"; }
3、前端下載excelhtml5
1.生成excel文件 使用html5 download屬性 <a href="http://localhost/PHPExcel/demo.xlsx" download="demo.xlsx">Download file</a> 2.執行PHP腳本 直接輸出到瀏覽器 <a href="http://localhost/PHPExcel/import.php" >Download file</a>
PHP直接生成excel文件git
header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition:filename={$file_name}.xls"); header("Pragma: no-cache"); header("Expires: 0"); //此種方法直接輸出html表格 由於Excel的兼容性能夠打開,但不是真正的excel表格 echo '<html xmlns:x="urn:schemas-microsoft-com:office:excel">'; echo '<meta http-equiv="Content-type" content="text/html;charset=utf-8" /> '; echo $excel_content;//直接輸出html的表格代碼 echo '</html>'; }轉載:https://blog.csdn.net/qq_38044604/article/details/77771297