Phpexcel How to export Excel colors that are inconsistent with colors in your Web page

Source: Internet
Author: User
  1. Require_once './phpexcel.php ';
  2. Require_once './excel5.php ';
  3. Require_once './get_excel_row.php ';
  4. Require ". /include/base.php ";
  5. Require ". /include/function/006/createxceldb.php ";
  6. Define ("COLOR1", "#96B7F6");
  7. Data Processing--------------------------
  8. $q = $db->query ("SELECT * from Oa_event_sales");
  9. while ($a = $db->fetch_array ($q)) {
  10. $list [] = $a;
  11. }
  12. $ce = new Createxceldb ();
  13. $re = $ce->_run ($list, ' served_time ', ' client_status ', ' oid ');
  14. $all _nums=0;
  15. $num =array ();
  16. foreach ($re as $k = = $v) {
  17. $num [$k]=count ($re [$k]);
  18. $all _nums+=count ($re [$k]);
  19. }
  20. $JQ = Array ();
  21. $title 1 = client_status;
  22. $title 2 = fin_confirm;
  23. $title 3 = oid;
  24. Remove the same value from the array
  25. foreach ($re as $key = = $val) {
  26. if (true) {
  27. foreach ($val as $key 2 = $val 2) {
  28. if (!in_array ($key 2, $JQ)) {
  29. $JQ [] = $key 2;
  30. }
  31. }
  32. }
  33. }
  34. $arr _keys=array ();
  35. foreach ($re as $k = = $v) {
  36. foreach ($v as $k 2=> $v 2) {
  37. $arr _keys[]= $k 2;
  38. }
  39. }
  40. $c =array_count_values ($arr _keys);
  41. //++===---------------------------
  42. Create a Processing object instance
  43. $objExcel = new Phpexcel ();
  44. Create a file format to write to an object instance, uncomment
  45. $objWriter = new Phpexcel_writer_excel5 ($objExcel);
  46. Set document basic properties/** seems to normally use less than **/
  47. $objProps = $objExcel->getproperties ();
  48. $objProps->setcreator ("Yang Benmu");
  49. $objProps->setlastmodifiedby ("Yang Benmu");
  50. $objProps->settitle ("Yang Benmu");
  51. $objProps->setsubject ("Yang Benmu");
  52. $objProps->setdescription ("Yang Benmu");
  53. $objProps->setkeywords ("Yang Benmu");
  54. $objProps->setcategory ("Yang Benmu");
  55. //*************************************
  56. Sets the current sheet index for subsequent content operations.
  57. It is generally only necessary to display a call when multiple sheet are used.
  58. By default, Phpexcel automatically creates the first sheet to be set sheetindex=0
  59. $objExcel->setactivesheetindex (0);
  60. $objActSheet = $objExcel->getactivesheet ();
  61. Sets the name of the currently active sheet
  62. $objActSheet->settitle (' current sheetname ');
  63. Set the width, this value is different from Excel, do not know what is the unit, slightly less than the width of Excel
  64. $objActSheet->getcolumndimension (' A ')->setwidth (20);
  65. $objActSheet->getrowdimension (1)->setrowheight (30); Height
  66. Set the value of a cell
  67. $objActSheet->setcellvalue (' A1 ', ' total title display ');
  68. /*
  69. Set style
  70. $objStyleA 1 = $objActSheet->getstyle (' A1 ');
  71. $objStyleA 1->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_center);
  72. $objFontA 1 = $objStyleA 1->getfont ();
  73. $objFontA 1->setname (' Song-Body ');
  74. $objFontA 1->setsize (18);
  75. $objFontA 1->setbold (TRUE);
  76. Set Column Center alignment
  77. $objActSheet->getstyle (' D ')->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_ CENTER);
  78. */
  79. ---------------First---------------------
  80. $BENMU = 1;
  81. $objActSheet->setcellvalue (' A1 ', ' row label \ column label ');
  82. foreach ($jq as $k = = $v) {
  83. $objActSheet->setcellvalue (Get_excel_row ($BENMU). ' 1 ', $v);
  84. $benmu +=1;
  85. }
  86. $objActSheet->setcellvalue (Get_excel_row ($BENMU). ' 1 ', ' total ');
  87. Set width
  88. for ($i =0; $i < $benmu +1; $i + +) {
  89. $objActSheet->getcolumndimension (Get_excel_row ($i))->setwidth (20);
  90. Width
  91. $objActSheet->getstyle (Get_excel_row ($i). " 1 ")->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_center);
  92. $objActSheet->getstyle (Get_excel_row ($i))->getalignment ()->sethorizontal (phpexcel_style_alignment:: Horizontal_center);
  93. Color
  94. $objActSheet->getstyle (Get_excel_row ($i). " 1 ")->getfill ()->setfilltype (phpexcel_style_fill::fill_solid);
  95. $objActSheet->getstyle (Get_excel_row ($i). " 1 ")->getfill ()->getstartcolor ()->setargb (COLOR1);
  96. }
  97. -------------Content--------------------
  98. $y = 2;
  99. foreach ($re as $k 1=> $v 1) {//$k 1 all on A2 .... Back, K1 for phone number, v1 for person-"number
  100. $objActSheet->setcellvalue (' A '. $y, $k 1);
  101. Color
  102. $objActSheet->getstyle (' A '. $y)->getfill ()->setfilltype (phpexcel_style_fill::fill_solid);
  103. $objActSheet->getstyle (' A '. $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
  104. foreach ($jq as $k 2=> $v 2) {//k2 is 0,v2 is person
  105. foreach ($v 1 as $k 3=> $v 3) {//$k 3 is person, $v 3 is the value to be
  106. if ($k 3== $v 2) {
  107. $objActSheet->setcellvalue (Get_excel_row ("1" + $k 2). $y, $v 1[$k 3]);
  108. $objActSheet->setcellvalueexplicit (Get_excel_row ("1" + $k 2). $y, $v 1[$k 3],phpexcel_cell_datatype::type_string);
  109. }
  110. }
  111. }
  112. $objActSheet->setcellvalue (Get_excel_row ("1" +count ($JQ)). $y, $num [$k 1]);
  113. $y +=1;
  114. }
  115. -----------------Last----------------
  116. $objActSheet->setcellvalue ("A". $y, "total");
  117. Color
  118. $objActSheet->getstyle ("A". $y)->getfill ()->setfilltype (phpexcel_style_fill::fill_solid);
  119. $objActSheet->getstyle ("A". $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
  120. foreach ($jq as $k = + $v) {//k as Person
  121. $objActSheet->setcellvalue (Get_excel_row ("1" + $k). $y, $c [$v]);
  122. Color
  123. $objActSheet->getstyle (Get_excel_row ("1" + $k). $y)->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
  124. $objActSheet->getstyle (Get_excel_row ("1" + $k). $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
  125. }
  126. $objActSheet->setcellvalue (Get_excel_row ("1" +count ($JQ)). $y, $all _nums);
  127. Color
  128. $objActSheet->getstyle (Get_excel_row ("1" +count ($JQ)). $y)->getfill ()->setfilltype (Phpexcel_style_fill: : Fill_solid);
  129. $objActSheet->getstyle (Get_excel_row ("1" +count ($JQ)). $y)->getfill ()->getstartcolor ()->setargb ( COLOR1);
  130. //-----------------------------------
  131. Output content
  132. $outputFileName =time (). ". XLS ";
  133. Header ("Pragma:public");
  134. Header ("expires:0");
  135. Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
  136. Header ("Content-type:application/force-download");
  137. Header ("Content-type:application/octet-stream");
  138. Header ("Content-type:application/download");
  139. Header (' Content-disposition:attachment;filename= '. $outputFileName. ');
  140. Header ("Content-transfer-encoding:binary");
  141. $objWriter->save (' php://output ');
  142. ?>
Copy Code

The above code, define colors in the exported Excel does not correctly display the original color. Start by defining a red look at what the display is, as follows: Define ("Color1″," #FF0000 ″); But the result is that the definition blue appears in Excel as a purple-like color, which is not the color of the page display anyway. Look at this code: $objActSheet->getstyle (' A '. $y)->getfill ()->getstartcolor ()->setargb (COLOR1); Consider the probable cause of ARGB, Try to add two digits to the front of the color 00, the format may be such ARGB (128,255,0,0), you can rule out this problem, because a is transparent, after the test is not known, it is indeed increased transparency of the 4-bit color.

  • Related Article

    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.