您好,登錄后才能下訂單哦!
小編給大家分享一下php如何導出excel,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
方法一:直接用頭部信息輸出excel格式文件,內容以表格形式展示
//直接用頭部信息輸出excel格式文件,內容以表格形式展示。 $filename='orderexcel'; header("Content-type: application/vnd.ms-excel; charset=gbk"); header("Content-Disposition: attachment; filename=$filename.xls"); //$list為數據庫查詢結果,既二維數組。利用循環出表格,直接輸出,既在線生成execl文件 foreach($list as $key => $val) { $data .= "<table border='1'>"; $data .= "<tr><td colspan='2'>訂單號:".$val['order_sn']. "</td><td>用戶名:".$val['user_name']."</td><td colspan='2'>收貨人:". $val['consignee']."</td><td colspan='2'>聯系電話:".$val['tel']."</td></tr>"; $data .= "<tr><td colspan='5'>送貨地址:".$val['address']. "</td><td colspan='2'>下單時間:".$val['add_time']."</td></tr>"; $data .= "<tr bgcolor='#999999'><th>序號</th><th>貨號</th><th>商品名稱</th><th>市場價</th> <th>本店價</th><th>購買數量</th><th>小計</th></tr>"; $data .= "<tr><th>1</th><th>".$val['goods_sn']."</th><th>".$val['goods_name']."</th><th>". $val['market_price']."</th><th>".$val['goods_price']."</th><th>".$val['goods_number']."</th><th>". $val['money']."</th></tr>"; $data .= "</table>"; $data .= "<br>"; } $data.='</table>'; if (EC_CHARSET != 'gbk') { echo yzy_iconv(EC_CHARSET, 'gbk', $data) . "\t"; } else { echo $data. "\t"; }
方法二:利用excel導出插件PHPExcel
//利用excel導出插件PHPExcel // 引入phpexcel核心類文件 require_once ROOT_PATH . '/includes/phpexcel/Classes/PHPExcel.php'; // 實例化excel類 $objPHPExcel = new PHPExcel(); // 操作第一個工作表 $objPHPExcel->setActiveSheetIndex(0); // 設置sheet名 $objPHPExcel->getActiveSheet()->setTitle('xx列表'); // 設置表格寬度 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(5); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20); // 列名表頭文字加粗 $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true); // 列表頭文字居中 $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 列名賦值 $objPHPExcel->getActiveSheet()->setCellValue('A1', '編號'); $objPHPExcel->getActiveSheet()->setCellValue('B1', '姓名'); $objPHPExcel->getActiveSheet()->setCellValue('C1', '電話'); $objPHPExcel->getActiveSheet()->setCellValue('D1', '擅長'); $objPHPExcel->getActiveSheet()->setCellValue('E1', '創建日期'); $objPHPExcel->getActiveSheet()->setCellValue('F1', '審核'); $objPHPExcel->getActiveSheet()->setCellValue('G1', '審核時間'); // 數據起始行 $row_num = 2; // 向每行單元格插入數據 foreach($res as $value) { // 設置所有垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'J' . $row_num)->getAlignment() ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 設置價格為數字格式 $objPHPExcel->getActiveSheet()->getStyle('D' . $row_num)->getNumberFormat() ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00); // 居中 $objPHPExcel->getActiveSheet()->getStyle('E' . $row_num . ':' . 'H' . $row_num)->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 設置單元格數值 $objPHPExcel->getActiveSheet()->setCellValue('A' . $row_num, $value['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . $row_num, $value['teacher_name']); $objPHPExcel->getActiveSheet()->setCellValue('C' . $row_num, $value['teacher_mobile']); $objPHPExcel->getActiveSheet()->setCellValue('D' . $row_num, $value['teacher_desc']); $objPHPExcel->getActiveSheet()->setCellValue('E' . $row_num, date('Y-m-d h:i:s',$value['createtime'])); $objPHPExcel->getActiveSheet()->setCellValue('F' . $row_num, $value['state'] ? '√' : '×'); $objPHPExcel->getActiveSheet()->setCellValue('G' . $row_num, date('Y-m-d h:i:s',$value['statetime'])); $row_num++; } $outputFileName = 'teacher_' . time() . '.xls'; $xlsWriter = new PHPExcel_Writer_Excel5($objPHPExcel); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="' . $outputFileName . '"'); header("Content-Transfer-Encoding: binary"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); $xlsWriter->save("php://output"); echo file_get_contents($outputFileName);
以上是php如何導出excel的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。