The color of excel exported by phpexcel is inconsistent with that displayed on the webpage.

Source: Internet
Author: User

For some questions about how to export the color of phpexcel, how does the color of the excel exported using phpexcel differ from the color displayed on the webpage? Want to know what is going on? How can this problem be solved? The PHP code details are as follows:
PHP code:
Copy codeThe Code is 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 ");
// Query and process data ==+++++++++++++++++++ ++ ++
$ Q = $ db-> query ("select * from oa_event_sales ");
While ($ a = $ db-> fetch_array ($ q )){
$ List [] = $;
}
$ Ce = new creatExcelDb ();
$ Re = $ ce-> _ run ($ list, 'served _ time', 'client _ status', 'id ');
$ All_nums = 0;
$ Num = array ();
Foreach ($ re as $ k => $ v ){
$ Num [$ k] = count ($ re [$ k]);
$ All_nums + = count ($ re [$ k]);
}
$ Jq = array ();
$ Title1 = client_status;
$ Title2 = fin_confirm;
$ Title3 = oid;
// Remove the same value from the array
Foreach ($ re as $ key => $ val ){
If (true ){
Foreach ($ val as $ key2 => $ val2 ){
If (! In_array ($ key2, $ jq )){
$ Jq [] = $ key2;
}
}
}
}
$ Arr_keys = array ();
Foreach ($ re as $ k => $ v ){
Foreach ($ v as $ k2 => $ v2 ){
$ Arr_keys [] = $ k2;
}
}
$ C = array_count_values ($ arr_keys );
// ++ ====+++ ++ ++
// Create a processing object instance
$ ObjExcel = new PHPExcel ();
// Create a file format to write the object instance, uncomment
$ ObjWriter = new PHPExcel_Writer_Excel5 ($ objExcel );
// Set the basic attributes of the document/** it seems that the document is generally unavailable **/
$ 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 ");
//*************************************
// Set the current sheet index for subsequent content operations.
// Display the call only when multiple sheets are used.
// By default, PHPExcel will automatically create the first sheet and set SheetIndex = 0
$ ObjExcel-> setActiveSheetIndex (0 );
$ ObjActSheet = $ objExcel-> getActiveSheet ();
// Set the name of the current active sheet
$ ObjActSheet-> setTitle ('current sheetname ');
// Set the width. The value is different from that in EXCEL. It is slightly smaller than the width in EXCEL.
// $ ObjActSheet-> getColumnDimension ('A')-> setWidth (20 );
// $ ObjActSheet-> getRowDimension (1)-> setRowHeight (30); // height
// Set the cell value
$ ObjActSheet-> setCellValue ('a1', 'show head ');
/*
// Set the style
$ ObjStyleA1 = $ objActSheet-> getStyle ('a1 ');
$ ObjStyleA1-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER );
$ ObjFontA1 = $ objStyleA1-> getFont ();
$ ObjFontA1-> setName (' ');
$ ObjFontA1-> setSize (18 );
$ ObjFontA1-> setBold (true );
// Set the center alignment of the column
$ 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 the width
For ($ I = 0; $ I <$ benmu + 1; $ I ++ ){
$ ObjActSheet-> getColumnDimension (get_excel_row ($ I)-> setWidth (20 );
// Width
$ ObjActSheet-> getStyle (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 (get_excel_row ($ I). "1")-> getFill ()-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
$ ObjActSheet-> getStyle (get_excel_row ($ I). "1")-> getFill ()-> getStartColor ()-> setARGB (COLOR1 );
}
// ==================== Content ================================
$ Y = 2;
Foreach ($ re as $ k1 => $ v1) {// $ k1 are all placed in A2 .... Followed by a phone number k1 and a person-"number v1.
$ ObjActSheet-> setCellValue ('A'. $ y, $ k1 );
// Color
$ ObjActSheet-> getStyle ('A'. $ y)-> getFill ()-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
$ ObjActSheet-> getStyle ('A'. $ y)-> getFill ()-> getStartColor ()-> setARGB (COLOR1 );
Foreach ($ jq as $ k2 => $ v2) {// k2 is 0, v2 is person
Foreach ($ v1 as $ k3 => $ v3) {// $ k3 is person, and $ v3 is the expected value.
If ($ k3 = $ v2 ){
// $ ObjActSheet-> setCellValue (get_excel_row ("1" + $ k2). $ y, $ v1 [$ k3]);
$ ObjActSheet-> setCellValueExplicit (get_excel_row ("1" + $ k2). $ y, $ v1 [$ k3], PHPExcel_Cell_DataType: TYPE_STRING );
}
}
}
$ ObjActSheet-> setCellValue (get_excel_row ("1" + count ($ jq). $ y, $ num [$ k1]);
$ 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 is 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 above Code, the color in define is not correctly displayed in the exported excel? Why? Why?
Define a red color to see what the display is, as follows: define ("COLOR1", "# FF0000 ″);
However, the result shows that the blue color is similar to the purple color in excel. It is not the color displayed on the page, which is too confusing .....
Read this code: $ objActSheet-> getStyle ('A'. $ y)-> getFill ()-> getStartColor ()-> setARGB (COLOR1 );
I think it may be because of ARGB, so I tried to add two 00 in front of the color. I don't know if this would work. Try the effect first, and the format may also be like this: argb (128,255, 0, 0), you can exclude this problem first, because a is transparency, and later I learned it after testing, it is indeed a four-digit color that increases 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.