Phpexcel export Excel colors and color display inconsistencies in Web pages _php tips

Source: Internet
Author: User
Some questions about Phpexcel export color, how does the color of Excel exported with phpexcel inconsistent with the color displayed in the Web page? Want to know what's going on? How should it be solved? The PHP code details are as follows:
PHP Code:
Copy Code code as follows:

<?php
Require_once './phpexcel.php ';
Require_once './excel5.php ';
Require_once './get_excel_row.php ';
Require ". /include/base.php ";
Require ". /include/function/006/createxceldb.php ";
Define ("COLOR1", "#96B7F6");
Processing Data ===+++++++++++++++++++++++++++++++++++++++++++++
$q = $db->query ("SELECT * from Oa_event_sales");
while ($a = $db->fetch_array ($q)) {
$list [] = $a;
}
$ce = new Createxceldb ();
$re = $ce->_run ($list, ' served_time ', ' client_status ', ' oid ');
$all _nums=0;
$num =array ();
foreach ($re as $k => $v) {
$num [$k]=count ($re [$k]);
$all _nums+=count ($re [$k]);
}
$JQ = Array ();
$title 1 = client_status;
$title 2 = fin_confirm;
$title 3 = oid;
Remove the same value from the array
foreach ($re as $key => $val) {
if (true) {
foreach ($val as $key 2 => $val 2) {
if (!in_array ($key 2, $JQ)) {
$JQ [] = $key 2;
}
}
}
}
$arr _keys=array ();
foreach ($re as $k => $v) {
foreach ($v as $k 2=> $v 2) {
$arr _keys[]= $k 2;
}
}
$c =array_count_values ($arr _keys);
//++===++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Create a Process object instance
$objExcel = new Phpexcel ();
Create a file format to write to an object instance, uncomment
$objWriter = new Phpexcel_writer_excel5 ($objExcel);
To set the basic properties of a document/** does not seem to use **/in general
$objProps = $objExcel->getproperties ();
$objProps->setcreator ("Yang Benmu");
$objProps->setlastmodifiedby ("Yang Benmu");
$objProps->settitle ("Yang Benmu");
$objProps->setsubject ("Yang Benmu");
$objProps->setdescription ("Yang Benmu");
$objProps->setkeywords ("Yang Benmu");
$objProps->setcategory ("Yang Benmu");
//*************************************
Sets the current sheet index for subsequent content operations.
It is generally only necessary to display calls when multiple sheet are used.
By default, Phpexcel will automatically create the first sheet set sheetindex=0
$objExcel->setactivesheetindex (0);
$objActSheet = $objExcel->getactivesheet ();
Sets the name of the current active sheet
$objActSheet->settitle (' current sheetname ');
Set width, this value is different from Excel, do not know what unit, slightly less than the width of Excel
$objActSheet->getcolumndimension (' A ')->setwidth (20);
$objActSheet->getrowdimension (1)->setrowheight (30); Height
Set the value of a cell
$objActSheet->setcellvalue (' A1 ', ' total title display ');
/*
Set style
$objStyleA 1 = $objActSheet->getstyle (' A1 ');
$objStyleA 1->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_center);
$objFontA 1 = $objStyleA 1->getfont ();
$objFontA 1->setname (' song Body ');
$objFontA 1->setsize (18);
$objFontA 1->setbold (TRUE);
Set Column Center alignment
$objActSheet->getstyle (' D ')->getalignment ()->sethorizontal () (Phpexcel_style_alignment::horizontal_ CENTER);
*/
============first===================================
$BENMU = 1;
$objActSheet->setcellvalue (' A1 ', ' row label \ column label ');
foreach ($jq as $k => $v) {
$objActSheet->setcellvalue (Get_excel_row ($BENMU). 1 ', $v);
$benmu +=1;
}
$objActSheet->setcellvalue (Get_excel_row ($BENMU). 1 ', ' total ');
Set width
For ($i =0 $i < $benmu +1; $i + +) {
$objActSheet->getcolumndimension (Get_excel_row ($i))->setwidth (20);
Width
$objActSheet->getstyle (The Get_excel_row ($i). 1 ")->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_center);
$objActSheet->getstyle (Get_excel_row ($i))->getalignment ()->sethorizontal (phpexcel_style_alignment:: Horizontal_center);
Color
$objActSheet->getstyle (The Get_excel_row ($i). 1 ")->getfill ()->setfilltype (phpexcel_style_fill::fill_solid);
$objActSheet->getstyle (The Get_excel_row ($i). 1 ")->getfill ()->getstartcolor ()->setargb () (COLOR1);
}
==============content=================
$y = 2;
foreach ($re as $k 1=> $v 1) {//$k 1 All placed in A2 .... Back, K1 for phone number, v1 for person-"number
$objActSheet->setcellvalue (' A ' $y, $k 1);
Color
$objActSheet->getstyle (' A '. $y)->getfill ()->setfilltype (phpexcel_style_fill::fill_solid);
$objActSheet->getstyle (' A '. $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
foreach ($jq as $k 2=> $v 2) {//k2 is 0,v2 is a person
foreach ($v 1 as $k 3=> $v 3) {//$k 3 is person, $v 3 is the value to be
if ($k 3== $v 2) {
$objActSheet->setcellvalue (Get_excel_row ("1" + $k 2). $y, $v 1[$k 3]);
$objActSheet->setcellvalueexplicit (Get_excel_row ("1" + $k 2). $y, $v 1[$k 3],phpexcel_cell_datatype::type_string);
}
}
}
$objActSheet->setcellvalue (Get_excel_row ("1" +count ($JQ)). $y, $num [$k 1]);
$y +=1;
}
=================last==========
$objActSheet->setcellvalue ("A". $y, "total");
Color
$objActSheet->getstyle ("A". $y)->getfill ()->setfilltype (phpexcel_style_fill::fill_solid);
$objActSheet->getstyle ("A". $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
foreach ($jq as $k => $v) {//k as Person
$objActSheet->setcellvalue (Get_excel_row ("1" + $k). $y, $c [$v]);
Color
$objActSheet->getstyle (Get_excel_row ("1" + $k). $y)->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
$objActSheet->getstyle (Get_excel_row ("1" + $k). $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
}
$objActSheet->setcellvalue (Get_excel_row ("1" +count ($JQ)). $y, $all _nums);
Color
$objActSheet->getstyle (Get_excel_row ("1" +count ($JQ)). $y)->getfill ()->setfilltype (Phpexcel_style_fill: : Fill_solid);
$objActSheet->getstyle (Get_excel_row ("1" +count ($JQ)). $y)->getfill ()->getstartcolor ()->setargb ( COLOR1);
//===============================
Output content
$outputFileName =time (). ". XLS ";
Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header (' Content-disposition:attachment;filename= '. $outputFileName. ');
Header ("Content-transfer-encoding:binary");
$objWriter->save (' php://output ');
?>

In the code above, the colors in the define are not correctly displayed in the exported Excel colors? What's the reason? Why, then?
Start defining a red look at what the show is, as follows: Define ("Color1″," #FF0000 ″);
But the result is that the definition of blue is shown in Excel as a color similar to purple, which is not the color that the page displays, it's so confusing ...
Look at this code: $objActSheet->getstyle (' A '. $y)->getfill ()->getstartcolor ()->setargb (COLOR1);
A bit feel may be argb reason, so try to put the color front add two digits 00, I do not know this line is not good, try the effect first, and the format may be such a ARGB (128,255,0,0), you can eliminate this problem, because a is transparent, then the test to know, It's really a 4-bit color that adds transparency.

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.