濮阳杆衣贸易有限公司

主頁 > 網(wǎng)站建設(shè) > 建站知識 > 織夢CMS系統(tǒng)導(dǎo)出數(shù)據(jù)到excel教程

織夢CMS系統(tǒng)導(dǎo)出數(shù)據(jù)到excel教程

POST TIME:2017-11-12 23:51

本來這個功能是一個朋友要做的,因為,之前從沒有接觸過excel,這個功能也沒有研究過,但是,這個功能比較實用的,因為,很多公司雖然有網(wǎng)站,但是,公司其它部門可能還要把公司數(shù)據(jù)導(dǎo)出到紙上面,以便研究公司數(shù)據(jù)之用,所以,很多的公司對這個功能就要求使用。

這幾天研究了一下從織夢導(dǎo)出數(shù)據(jù)功能,本來想自己去寫一個這個php程序?qū)С龅絜xcel功能來,但是,自己從沒有研究過excel,所以,就沒有向下去寫代碼,于是搜索了一下網(wǎng)上的關(guān)于這個教程,結(jié)果搜索的教程到是不少,但是,沒有幾個是正確的,包括百度經(jīng)驗里面,測試了無數(shù)個也不行。

不過讓我搜索到了一個外國人寫的phpexcel類庫,這個就是專門處理從數(shù)據(jù)庫到excel的,下載地址:http://phpexcel.codeplex.com/releases/view/107442

這個里面有很多的例子,包括excel,csv,word,pdf,htm等從數(shù)據(jù)庫導(dǎo)出來的文件格式,可以參考一下例子。

織夢基地先把從織夢系統(tǒng)導(dǎo)出來的效果上個圖給大家看看:



但是,我遇到了一個問題就是以前的時間,在excel是正常顯示的,但是,現(xiàn)在加上的時間則顯示:1970-01-01 ,我搜索了一下網(wǎng)上教程,也沒有找到相關(guān)教程??吹骄W(wǎng)上說excel也有bug 而且excel計算的時間是從1900-1-1開始的,不知道是不是還要換算一下?

時間問題終于找到原因了,原來字段寫錯了。本來是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ù),這里不多解釋了,看我在文件dedegenban.php寫的代碼:

  1. require_once (DEDEINC . '/common.func.php');
  2. if ($action == 'allexport') {
  3. include_once DEDEINC . '/PHPExcel.php';
  4. // Create new PHPExcel object
  5. $objPHPExcel = new PHPExcel();
  6. $objActSheet = $objPHPExcel->getActiveSheet();
  7. // Set document properties
  8. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw")
  9. ->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")
  10. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")
  11. ->setCategory("Test result file");
  12. //www.genban.org 跟版網(wǎng)
  13. $objPHPExcel->setActiveSheetIndex(0)
  14. ->setCellValue('A1', 'id')
  15. ->setCellValue('B1', '標(biāo)題')
  16. ->setCellValue('C1', '排序')
  17. ->setCellValue('D1', '出版時間')
  18. ->setCellValue('E1', '關(guān)鍵詞')
  19. ->setCellValue('F1', '簡介')
  20. ->setCellValue('G1', '發(fā)布時間')
  21. ->setCellValue('H1', '會員id')
  22. ->setCellValue('I1', 'flag')
  23. ->setCellValue('J1', '欄目id');
  24. $query = "Select * From `dede_archives` ";
  25. $dsql->SetQuery($query);
  26. $dsql->Execute();
  27. $index = 1;
  28. while ($row = $dsql->GetArray()) {
  29. $index++;
  30. $objPHPExcel->setActiveSheetIndex(0)
  31. ->setCellValue('A' .
  32. $index, $row['id'])->setCellValue('B' .
  33. $index, iconv("gb2312","utf-8",$row['title']))->setCellValue('C' .
  34. $index, $row['sortrank'])->setCellValue('D' .
  35. $index, "2015-7-23")->setCellValueExplicit('E' .
  36. $index, iconv("gb2312","utf-8",$row['keywords']))->setCellValue('F' .
  37. $index, iconv("gb2312","utf-8",$row['description']))->setCellValue('G' .
  38. $index, gmdate("Y-m-d",$row['pubdate']))->setCellValue('H' .
  39. $index, $row['mid'])->setCellValue('I' .
  40. $index, $row['flag'])->setCellValue('J' .
  41. $index, $row['typeid']);
  42. }
  43. // Rename worksheetwww.dedebase.com
  44. $objPHPExcel->getActiveSheet()->setTitle('Simple');
  45. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  46. $objPHPExcel->setActiveSheetIndex(0);
  47. // Redirect output to a client’s web browser (Excel5)
  48. header('Content-Type: application/vnd.ms-excel');
  49. header('Content-Disposition: attachment;filename="list.xls"');
  50. header('Cache-Control: max-age=0');
  51. $objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel, 'Excel5');
  52. $objWriter->save('php://output');
  53. exit;
  54. }
特別特別注意:在這個最上一行要寫上你的從網(wǎng)站上下載下來的phpexcel類的路徑,也就是把這個類引入到這個文件里面,只有引入到里面才能調(diào)用。

最后,在你的模板里面把下面這二行中的任意一行寫在模板里即可:
  1. <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">
  2. <a href='crtadmin/download_excel.php?action=allexport'>execl</a>
我這里放二行是因為,有的站長可能只需要一個超鏈接,有的可能需要一個input,二個任選一個即可。


上一篇:關(guān)于內(nèi)容模型字段里的“自動表單”和“固化字

下一篇:文章內(nèi)容頁圖片自動居中,自適應(yīng)手機,寬度100%

收縮
  • 微信客服
  • 微信二維碼
  • 電話咨詢

  • 400-1100-266
崇礼县| 巴中市| 彰化市| 辽阳市| 阜新市| 三河市| 曲松县| 玉树县| 西吉县| 江北区| 通化市| 清水县| 偃师市| 建平县| 通城县| 巴彦县| 梁山县| 贺兰县| 保亭| 延边| 东乡族自治县| 都安| 阿巴嘎旗| 乐亭县| 嘉兴市| 绍兴市| 昌都县| 青龙| 宜君县| 平安县| 微山县| 巍山| 武陟县| 灵石县| 新晃| 永丰县| 昭觉县| 靖宇县| 塔河县| 文水县| 都兰县|