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

織夢(mèng)DEDECMS使用PHPEXCEL將內(nèi)容數(shù)據(jù)導(dǎo)出到excel的方法

 使用中遇到一個(gè)問(wèn)題,就是以前的時(shí)間,在excel是正常顯示的,但是,現(xiàn)在加上的時(shí)間則顯示:1970-01-01 ,研究了很久,原來(lái)字段寫錯(cuò)了。本來(lái)是pubdate,結(jié)果寫成了sentdate
其它都是正常顯示的
現(xiàn)在就介紹一下從織夢(mèng)系統(tǒng)導(dǎo)出數(shù)據(jù)的方法:
1.從國(guó)外網(wǎng)站下載上面的phpexcel類庫(kù),解壓后,放到根目錄里面。
2.然后,寫導(dǎo)出程序,這個(gè)可以參考這里面的例子寫,請(qǐng)注意的是:若你的不行,程序可能會(huì)提示404錯(cuò)誤,這個(gè)就是你的路徑?jīng)]有設(shè)置好,剛開(kāi)始時(shí),我也是這個(gè)原因一直弄不對(duì),最后,才發(fā)現(xiàn)原來(lái)是路徑錯(cuò)了。
這個(gè)導(dǎo)出程序主要做如下四步:
a. 從織夢(mèng)中查詢出數(shù)據(jù)
b.設(shè)置表格
c.把數(shù)據(jù)放入表格
d.輸出數(shù)據(jù)到excel里面
里面的設(shè)置大多數(shù)都是調(diào)用phpexcel類里面的函數(shù),這里不多解釋了,看我在文件dedebnxb.php寫的代碼:

01require_once (DEDEINC . '/common.func.php');
02 
03if ($action == 'allexport') {
04 
05    include_once DEDEINC . '/PHPExcel.php';
06    // Create new PHPExcel object
07    $objPHPExcel = new PHPExcel();
08 
09    $objActSheet = $objPHPExcel->getActiveSheet();
10 
11    // Set document properties
12    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw")
13    ->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")
14    ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")
15    ->setCategory("Test result file");
16          //www.bnxb.com 笨牛網(wǎng)
17    $objPHPExcel->setActiveSheetIndex(0)
18    ->setCellValue('A1', 'id')
19    ->setCellValue('B1', '標(biāo)題')
20    ->setCellValue('C1', '排序')
21    ->setCellValue('D1', '出版時(shí)間')
22    ->setCellValue('E1', '關(guān)鍵詞')
23    ->setCellValue('F1', '簡(jiǎn)介')
24    ->setCellValue('G1', '發(fā)布時(shí)間')
25    ->setCellValue('H1', '會(huì)員id')
26    ->setCellValue('I1', 'flag')
27    ->setCellValue('J1', '欄目id');
28    $query = "Select * From `dede_archives` ";
29    $dsql->SetQuery($query);
30    $dsql->Execute();
31 
32    $index = 1;
33 
34    while ($row = $dsql->GetArray()) {
35 
36        $index++;
37        $objPHPExcel->setActiveSheetIndex(0)
38        ->setCellValue('A' .
39        $index, $row['id'])->setCellValue('B' .
40        $index, iconv("gb2312","utf-8",$row['title']))->setCellValue('C' .
41        $index, $row['sortrank'])->setCellValue('D' .
42        $index, "2015-7-23")->setCellValueExplicit('E' .
43        $index, iconv("gb2312","utf-8",$row['keywords']))->setCellValue('F' .
44        $index, iconv("gb2312","utf-8",$row['description']))->setCellValue('G' .
45        $index, gmdate("Y-m-d",$row['pubdate']))->setCellValue('H' .
46        $index, $row['mid'])->setCellValue('I' .
47        $index, $row['flag'])->setCellValue('J' .
48        $index, $row['typeid']);
49    }
50 
51    // Rename worksheetwww.bnxb.com
52    $objPHPExcel->getActiveSheet()->setTitle('Simple');
53 
54    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
55    $objPHPExcel->setActiveSheetIndex(0);
56 
57    // Redirect output to a client’s web browser (Excel5)
58    header('Content-Type: application/vnd.ms-excel');
59 
60    header('Content-Disposition: attachment;filename="list.xls"');
61    header('Cache-Control: max-age=0');
62 
63    $objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel, 'Excel5');
64    $objWriter->save('php://output');
65    exit;
66 
67}

特別特別注意:在這個(gè)最上面一行要寫上你的從網(wǎng)站上下載下來(lái)的phpexcel類的路徑,也就是把這個(gè)類引入到這個(gè)文件里面,只有引入到里面才能調(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>
我這里放二行是因?yàn)?有的站長(zhǎng)可能只需要一個(gè)超鏈接,有的可能需要一個(gè)input,二個(gè)任選一個(gè)即可。

?