對于一些企業(yè)網(wǎng)站來說,經(jīng)常需要把網(wǎng)站上的數(shù)據(jù)導(dǎo)出到EXCEL來進行分析,這里就需要用到PHPEXCEL,可以方便導(dǎo)出網(wǎng)站MYSQL數(shù)據(jù)庫內(nèi)容到EXCEL總
phpexcel是外國人寫專門處理從數(shù)據(jù)庫到excel的功能庫,下載地址:https://github.com/PHPOffice/PHPExcel
里面有很多的例子,包括excel,csv,word,pdf,htm等從數(shù)據(jù)庫導(dǎo)出來的文件格式,可以參考一下例子。
笨牛網(wǎng)先把從織夢系統(tǒng)導(dǎo)出來的效果上個圖給大家看看:

使用中遇到一個問題,就是以前的時間,在excel是正常顯示的,但是,現(xiàn)在加上的時間則顯示:1970-01-01 ,研究了很久,原來字段寫錯了。本來是pubdate,結(jié)果寫成了sentdate
其它都是正常顯示的
現(xiàn)在就介紹一下從織夢系統(tǒng)導(dǎo)出數(shù)據(jù)的方法:
1.從國外網(wǎng)站下載上面的phpexcel類庫,解壓后,放到根目錄里面。
2.然后,寫導(dǎo)出程序,這個可以參考這里面的例子寫,請注意的是:若你的不行,程序可能會提示404錯誤,這個就是你的路徑?jīng)]有設(shè)置好,剛開始時,我也是這個原因一直弄不對,最后,才發(fā)現(xiàn)原來是路徑錯了。
這個導(dǎo)出程序主要做如下四步:
a. 從織夢中查詢出數(shù)據(jù)
b.設(shè)置表格
c.把數(shù)據(jù)放入表格
c.輸出數(shù)據(jù)到excel里面
里面的設(shè)置大多數(shù)都是調(diào)用phpexcel類里面的函數(shù),這里不多解釋了,看我在文件dedebnxb.php寫的代碼:
require_once (DEDEINC . '/common.func.php'); if ($action == 'allexport') { include_once DEDEINC . '/PHPExcel.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); $objActSheet = $objPHPExcel->getActiveSheet(); // Set document properties $objPHPExcel->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"); //www.bnxb.com 笨牛網(wǎng) $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'id') ->setCellValue('B1', '標題') ->setCellValue('C1', '排序') ->setCellValue('D1', '出版時間') ->setCellValue('E1', '關(guān)鍵詞') ->setCellValue('F1', '簡介') ->setCellValue('G1', '發(fā)布時間') ->setCellValue('H1', '會員id') ->setCellValue('I1', 'flag') ->setCellValue('J1', '欄目id'); $query = "Select * From `dede_archives` "; $dsql->SetQuery($query); $dsql->Execute(); $index = 1; while ($row = $dsql->GetArray()) { $index++; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A' . $index, $row['id'])->setCellValue('B' . $index, iconv("gb2312","utf-8",$row['title']))->setCellValue('C' . $index, $row['sortrank'])->setCellValue('D' . $index, "2015-7-23")->setCellValueExplicit('E' . $index, iconv("gb2312","utf-8",$row['keywords']))->setCellValue('F' . $index, iconv("gb2312","utf-8",$row['description']))->setCellValue('G' . $index, gmdate("Y-m-d",$row['pubdate']))->setCellValue('H' . $index, $row['mid'])->setCellValue('I' . $index, $row['flag'])->setCellValue('J' . $index, $row['typeid']); } // Rename worksheetwww.bnxb.com $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="list.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; } |
特別特別注意:在這個最上面一行要寫上你的從網(wǎng)站上下載下來的phpexcel類的路徑,也就是把這個類引入到這個文件里面,只有引入到里面才能調(diào)用。
最后,在你的模板里面把下面這二行中的任意一行寫在模板里即可:
<input name="ss12" value="導(dǎo)出全部訂單" style="width:90px;margin-right:6px" onclick="location='crtadmin/download_oneapply.php?action=allexport';" class="np coolbg" type="button">
<a href='crtadmin/download_excel.php?action=allexport'>execl</a>
我這里放二行是因為,有的站長可能只需要一個超鏈接,有的可能需要一個input,二個任選一個即可。
新聞熱點
疑難解答
圖片精選