PHPExcel data export instance

Source: Internet
Author: User
PHPExcel data export instance

1. getdata. php

  1. Namespace Webadmin \ Model;
  2. Use ExtendSpace \ Excel;
  3. ......
  4. Omitted
  5. ......
  6. // Retrieve data
  7. $ DataBillArr = $ this-> get_list_bysql ($ SQL );
  8. // Replace 0 and 1 with Yes and No
  9. // PHPExcel has a built-in method for processing, but the result is TRUE/FALSE. let's handle it by yourself.
  10. $ This-> _ formatZero ($ dataBillArr, array ('taxflag', 'payflag', 'removeflag '));
  11. // Replace the payment status
  12. Foreach ($ dataBillArr as $ key => $ value ){
  13. Switch ($ value ['statustype ']) {
  14. Case '-1 ':
  15. $ DataBillArr [$ key] ['statustype '] = 'canceled ';
  16. Break;
  17. Case '-2 ':
  18. $ DataBillArr [$ key] ['statustype '] = 'canceled refunded ';
  19. Break;
  20. Case '0 ':
  21. $ DataBillArr [$ key] ['statustype '] = 'awaiting payment ';
  22. Break;
  23. Case '1 ':
  24. $ DataBillArr [$ key] ['statustype '] = 'awaiting shipping ';
  25. Break;
  26. Case '2 ':
  27. $ DataBillArr [$ key] ['statustype '] = 'waiting for shipping ';
  28. Break;
  29. Case '3 ':
  30. $ DataBillArr [$ key] ['statustype '] = 'finished ';
  31. Break;
  32. Case '10 ':
  33. $ DataBillArr [$ key] ['statustype '] = 'return finished ';
  34. Break;
  35. Case '11 ':
  36. $ DataBillArr [$ key] ['statustype '] = 'refund completed ';
  37. Break;
  38. Default:
  39. $ DataBillArr [$ key] ['statustype '] = 'none ';
  40. Break;
  41. }
  42. }
  43. // Set the field to be exported and the corresponding header name
  44. $ Header = array (
  45. Array ('title' => 'platform order no. ', 'field' => 'billcode', 'type' => 'string', 'autosize' => true ),
  46. Array ('title' => 'User account', 'field' => 'username', 'type' => 'string', 'autosize' => true ),
  47. Array ('title' => 'User nick name', 'field' => 'nickname '),
  48. Array ('title' => 'Merchant ', 'field' => 'shopuser', 'autosize' => true ),
  49. Array ('title' => 'ease of ERP ticket no. ', 'field' => 'errorn', 'type' => 'string', 'autosize' => true ),
  50. Array ('title' => 'payment order no. ', 'field' => 'babaycode', 'type' => 'string', 'autosize' => true ),
  51. Array ('title' => 'bonded batch no. ', 'field' => 'bsbatchcode', 'type' => 'string', 'autosize' => true ),
  52. Array ('title' => 'cross-border ', 'field' => 'taxflag '),
  53. Array ('title' => 'Order status', 'field' => 'statustype '),
  54. ......
  55. Omitted
  56. ......
  57. );
  58. // Call the interface to generate and export an Excel file
  59. $ Filename = 'Order streaming water meter _ '. date ('y, m, d, _ His', time ());
  60. Excel: export ($ dataBillArr, $ header, $ filename );


II. Excel. class. php

  1. Namespace ExtendSpace;
  2. /**
  3. * Class Excel general Excel interface for export and export operations
  4. * Instructions:
  5. * 1. import
  6. * To be continued...
  7. * 2. export
  8. * Use ExtendSpace \ Excel;
  9. *.....
  10. * Excel: export ($ dataArr, $ header, $ filename );
  11. *
  12. * @ Package ExtendSpace
  13. * @ Author xxxxx 2015-08-27 14:07:14
  14. * @ Version
  15. */
  16. Class Excel {
  17. // Private static $ objPHPExcel = null;
  18. /**
  19. * Entry File: export Excel
  20. * @ Return
  21. */
  22. Public static function export ($ data, $ header, $ filename = 'HMS _ excel_export '){
  23. // Introduce the PHPExcel class library
  24. Import ('phpexcel. phpexcel ', dirname (_ FILE _).'/','. php'); // This is unique to TP and can be directly used with include or require
  25. // Initialization settings
  26. $ ObjPHPExcel = new \ PHPExcel ();
  27. $ ObjPHPExcel-> getProperties ()-> setCreator ('test')-> setLastModifiedBy ('test'); // Set Chinese garbled characters here, which has not been resolved
  28. //-> SetTitle ('This is the title ')
  29. //-> SetSubject ('What is this ')
  30. //-> SetDescription ('this is description ')
  31. //-> SetKeywords ('This is the keyword ')
  32. //-> SetCategory ('Is this a directory ');
  33. // Var_dump ($ objPHPExcel-> getProperties (); exit;
  34. // Obtain the current activity worksheet
  35. $ ObjActSheet = $ objPHPExcel-> getActiveSheet ();
  36. // Write the header
  37. Foreach ($ header as $ k => $ v ){
  38. $ ColIndex = self: _ getHeaderIndex ($ k );
  39. $ ObjPHPExcel-> setActiveSheetIndex (0)-> setCellValue ($ colIndex. '1', $ v ['title']);
  40. // Whether the column needs to automatically adapt to the width
  41. If (! Empty ($ v ['autosize']) {
  42. $ ObjActSheet-> getColumnDimension ($ colIndex)-> setAutoSize (true );
  43. }
  44. }
  45. // Write data. starting from the second row, the first row is the header.
  46. $ RowNum = 2;
  47. Foreach ($ data as $ rows) {// traverses data and obtains a row
  48. Foreach ($ header as $ kk => $ vv) {// write a cell
  49. $ ColIndex = self: _ getHeaderIndex ($ kk );
  50. // Specify the cell data format
  51. If (! Empty ($ vv ['type']) {// Yes
  52. Switch ($ vv ['type']) {
  53. Case 'number ':
  54. $ Type = \ PHPExcel_Cell_DataType: TYPE_NUMERIC; // number
  55. Break;
  56. Case 'boolean ':
  57. $ Type = \ PHPExcel_Cell_DataType: TYPE_BOOL; // boolean value, 0-> FALSE; 1-> TRUE
  58. Break;
  59. Default:
  60. $ Type = \ PHPExcel_Cell_DataType: TYPE_STRING; // string
  61. Break;
  62. }
  63. $ ObjActSheet-> setCellValueExplicit ($ colIndex. $ rowNum, $ rows [$ vv ['field'], $ type );
  64. } Else {// No, general by default
  65. $ ObjActSheet-> setCellValue ($ colIndex. $ rowNum, $ rows [$ vv ['field']);
  66. }
  67. }
  68. $ RowNum ++;
  69. }
  70. // Set the row height rownum
  71. // $ ObjPHPExcel-> getActiveSheet ()-> getRowDimension ('1')-> setRowHeight (22 );
  72. // Set the font and style
  73. $ ObjActSheet-> getDefaultStyle ()-> getFont ()-> setSize (12); // The overall font size.
  74. $ ObjActSheet-> getStyle ('A1 :'. self: _ getHeaderIndex (count ($ header )). '1')-> getFont ()-> setBold (true); // bold the column title
  75. // Set the worksheet name
  76. $ ObjActSheet-> setTitle ('sheet1 ');
  77. // Set header parameters
  78. // Header ("Pragma: public ");
  79. // Header ("Expires: 0 ");
  80. // Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
  81. // Header ("Content-Type: application/force-download ");
  82. // Header ("Content-Type: application/vnd. ms-execl ");
  83. // Header ("Content-Type: application/octet-stream ");
  84. // Header ("Content-Type: application/download ");;
  85. // Header ('content-Disposition: attachment; filename = "'. $ savedFileName .'"');
  86. // Header ("Content-Transfer-Encoding: binary ");
  87. // Final output
  88. $ SavedFileName = self: _ iconv ($ filename). '.xls '; // export file name + extension
  89. Header ('content-Type: application/vnd. ms-excel ');
  90. Header ('content-Disposition: attachment; filename = "'. $ savedFileName .'"');
  91. Header ('cache-Control: max-age = 0 ');
  92. $ ObjWriter = \ PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 ');
  93. $ ObjWriter-> save ('php: // output ');
  94. // $ ObjWriter-> save ($ savedFileName );
  95. }
  96. /**
  97. * Entry File: import an Excel file
  98. * @ Return
  99. */
  100. Public static function import (){
  101. // Introduce the PHPExcel class library
  102. // Import ('phpexcel. phpexcel ', dirname (_ FILE _).'/','. php ');
  103. }
  104. Private static function _ init (){
  105. }
  106. /**
  107. * Obtain the index value of the table header, that is, A, B, C..., greater
  108. * @ Param array $ header: array used to set the header
  109. * @ Return string
  110. */
  111. Private function _ getHeaderIndex ($ num ){
  112. Return \ PHPExcel_Cell: stringFromColumnIndex ($ num );
  113. }
  114. /**
  115. * Character conversion to avoid garbled characters
  116. * @ Param string $ str characters to be processed
  117. * @ Return string
  118. */
  119. Private function _ iconv ($ str ){
  120. Return iconv ('utf-8', 'gb2312 ', $ str );
  121. }
  122. }



PHPExcel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.