TShopping

 找回密碼
 註冊
搜索
查看: 8979|回復: 1
打印 上一主題 下一主題

[教學] 如何利用PHP製作並匯出PDF報表

  [複製鏈接]
跳轉到指定樓層
1#
發表於 2015-2-23 17:30:00 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook

首先可以到FPDF網站下載程式,當然FPDF的網站有教學也值得前往觀看http://www.fpdf.org/

或直接點選這邊下載fpdf16.zip

因為FPDF的網站是英文的,因此若要使用中文字,

則需要再下載chinese-unicode.rar

好啦,將上面的檔案下載之後解壓縮放入網站的資料夾就可以開始囉


test.php

  1. <?php
  2. $hostname = "localhost";   //主機名稱
  3. $database = "test";              //資料庫名稱
  4. $username = "xxxx";            //資料庫使用者帳號
  5. $password = "****";            //資料庫使用者密碼

  6. $test = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
  7. mysql_select_db("test",$test);
  8. mysql_query("set names 'utf8'");

  9. ?>
複製代碼



reportSQL.php

  1. <?php  session_start();

  2. mysql_select_db($database, $test);

  3. //index(首頁)
  4. $query_index = "SELECT * FROM `flow` WHERE date = CURDATE( ) AND page = 'index'";
  5. $index = mysql_query($query_index, $test) or die(mysql_error());
  6. $totalRows_index = mysql_num_rows($index);
  7. $query_index2 = "SELECT * FROM `flow` WHERE page = 'index'";
  8. $index2 = mysql_query($query_index2, $test) or die(mysql_error());
  9. $totalRows_index2 = mysql_num_rows($index2);

  10. //introduce(品牌介紹)
  11. $query_introduce = "SELECT * FROM `flow` WHERE date = CURDATE( ) AND page = 'introduce'";
  12. $introduce = mysql_query($query_introduce, $test) or die(mysql_error());
  13. $totalRows_introduce = mysql_num_rows($introduce);
  14. $query_introduce2 = "SELECT * FROM `flow` WHERE page = 'introduce'";
  15. $introduce2 = mysql_query($query_introduce2, $test) or die(mysql_error());
  16. $totalRows_introduce2 = mysql_num_rows($introduce2);

  17. //......................(其餘頁面寫法同上)

  18. ?>
複製代碼



report.php

  1. <?php
  2. require('chinese-unicode.php');            //匯入剛剛下載的中文化的FPDF
  3. require_once('../Connections/test.php');   //匯入連結資料庫的語法
  4. require_once('../require/reportSQL.php');  //匯入報表的SQL語法
  5. $pdf=new PDF_Unicode();                    //建立PDF_Unicode
  6. $pdf->Open();                              //開啟
  7. $pdf->AddPage();                           //新的一頁
  8. $pdf->AddUniCNShwFont('uni');              //加入中文
  9. $pdf->SetFont('uni','',16);                //設定字型與字體大小
  10. //接著將資料放入一維陣列中
  11. $row0=array('瀏覽頁面','今日人數流量(人)','總人數流量(人)');
  12. $row1=array('首頁',$totalRows_index,$totalRows_index2);
  13. $row2=array('品牌介紹',$totalRows_introduce,$totalRows_introduce2);
  14. $row3=array('經營理念',$totalRows_manage,$totalRows_manage2);
  15. $row4=array('代理品牌',$totalRows_agent,$totalRows_agent2);
  16. $row5=array('產品',$totalRows_product,$totalRows_product2);
  17. $row6=array('公告發佈',$totalRows_publish,$totalRows_publish2);
  18. $row7=array('活動新訊',$totalRows_activities,$totalRows_activities2);
  19. $row8=array('素材介紹',$totalRows_material,$totalRows_material2);
  20. $row9=array('營業據點',$totalRows_place,$totalRows_place2);
  21. $row10=array('聯絡我們',$totalRows_contact,$totalRows_contact2);

  22. //今日人數流量統計
  23. $today=$totalRows_index+$totalRows_introduce+$totalRows_manage+$totalRows_agent+$totalRows_product+$totalRows_publish+$totalRows_activities+$totalRows_material+$totalRows_place+$totalRows_contact;
  24. $total=$totalRows_index2+$totalRows_introduce2+$totalRows_manage2+$totalRows_agent2+$totalRows_product2+$totalRows_publish2+$totalRows_activities2+$totalRows_material2+$totalRows_place2+$totalRows_contact2;
  25. $row11=array('統計',$today,$total);
  26. //再將資料放入2維陣列中
  27. $trow=array($row0,$row1,$row2,$row3,$row4,$row5,$row6,$row7,$row8,$row9,$row10,$row11);

  28. //設定表格的寬度
  29. $w=array(60,65,60);

  30. //迴圈
  31. for($j=0;$j<count($trow);$j++){
  32.   for($i=0;$i<count($trow[$j]);$i++){
  33.    $pdf->Cell($w[$i],7,$trow[$j][$i],1,0,'C');     //將資料放入表格內
  34.   } $pdf->Ln();                                    //下一列
  35. }
  36. $pdf->Output();                                   //輸出呈現
  37. ?>
複製代碼

接下來從瀏覽器觀看的結果如下圖:




真的是一個超方便的工具喔 ^ ^~~



Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])

w
Cell width. If 0, the cell extends up to the right margin.
h
Cell height. Default value: 0.
txt
String to print. Default value: empty string.
border
Indicates if borders must be drawn around the cell. The value can be either a number:
0: no border
1: frame
or a string containing some or all of the following characters (in any order):
L: left
T: top
R: right
B: bottom
Default value: 0.
ln
Indicates where the current position should go after the call. Possible values are:
0: to the right
1: to the beginning of the next line
2: below
Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
align
Allows to center or align the text. Possible values are:
L or empty string: left align (default value)
C: center
R: right align
fill
Indicates if the cell background must be painted (true) or transparent (false). Default value: false.
link
URL or identifier returned by AddLink().
Example

  1. // Set font
  2. $pdf->SetFont('Arial','B',16);
  3. // Move to 8 cm to the right
  4. $pdf->Cell(80);
  5. // Centered text in a framed 20*10 mm cell and line break
  6. $pdf->Cell(20,10,'Title',1,1,'C');
複製代碼


 

臉書網友討論
2#
發表於 2015-7-2 23:37:26 | 只看該作者


  不是很明白

版主招募中

*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-19 11:11 , Processed in 0.060896 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表