PHP uses Phpexcel to generate an Excel document instance

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags application content content type display document error error message office

PHP tutorial using Phpexcel to build an Excel document instance
Ini_set ("Display_errors", 1);//whether to display an error message
Ini_set (' include_path ', Ini_get (' include_path ').

Require_once ("./1.7.2/classes/phpexcel.php");
Require_once ("./1.7.2/classes/phpexcel/writer/excel5.php");

$objExcel = new Phpexcel ();
Print_r ($objExcel);
$objWriter = new Phpexcel_writer_excel5 ($objExcel);
$objWriter = new phpexcel_writer_excel2007 ($objExcel); Used in 2007 format
$objWriter->setoffice2003compatibility (TRUE);

//Set document basic Properties     www.jzread.com
$objProps tutorial = $objExcel->getproperties ();
//print_r ($ Objprops);
$objProps->setcreator ("Zeal li");   
$objProps->setlastmodifiedby ("Zeal li");    
$objProps->settitle ("Office XLS Test Document");   
$objProps->setsubject ("Office XLS test document, Demo");   
$objProps->setdescription ("Test document, generated by Phpexcel. ");    
$objProps->setkeywords (Office Excel phpexcel);   
$objProps-> Setcategory ("Test");

//*************************************   
//sets the current sheet index for subsequent content operations.    
//Generally, you only need to display the call when you are using multiple sheet.    
//By default, Phpexcel automatically creates the first sheet set sheetindex=0   
 
$objExcel- >setactivesheetindex (0);   
  
  
$objActSheet = $objExcel-> Getactivesheet ();   
$objActSheet = $objExcel->getactivesheet ();  
// Sets the name of the current active sheet    
$objActSheet->settitle (' Test sheet ');   
  
//*************************************   
//Set cell contents    
//    
//by phpexcel automatically determines the cell content type    
$objActSheet->setcellvalue (' A1 ', ' string content ') based on incoming content;  String content    
$objActSheet->setcellvalue (' A2 ', ' <a herf= ' www.jzread.com ' >26</a> ');            //numeric    
$objActSheet->setcellvalue (' A3 ', true);          //Boolean    
$objActSheet->setcellvalue (' A4 ', ' =sum (A2:A2) ');//Formula    
  
//Explicit specifying content type    
$objActSheet->setcellvalueexplicit (' A5 ', ' 847475847857487584 ',  phpexcel_cell_datatype::type_string); 
//merged cells    
$ Objactsheet->mergecells (' b1:c22 ');   
  
//Detach cell    
$ Objactsheet->unmergecells (' b1:c22 ');    

//*************************************   
//Set cell styles    
//    
  
//Setting width    
$objActSheet->getcolumndimension (' B ')->setautosize (true);   
$objActSheet->getcolumndimension (' A ')->setwidth;   
  
$objStyleA 5 = $objActSheet->getstyle (' A5 ');    

//sets the number format of the cell contents.    
//   
///If PHPEXCEL_WRITER_EXCEL5 is used to generate content,   
// It should be noted that in the Phpexcel_style_numberformat class, the const variable defines the    
///Various custom formatting methods that are available to other types, but when the Setformatcode    
//For format_number, the actual effect is not set to the format of "0". Need    
//Modify the GETXF ($style) method in the Phpexcel_writer_excel5_format class source code,   
//In if ($ This->_biff_version = = 0x0500) {(Near line No. 363) before adding a    
//Line code:   
//if ($ifmt = = ' 0 ') $ifmt = 1;   
//   
//formatted as Phpexcel_style_numberformat::format_number, Avoid certain large numbers    
//are displayed using scientific notation, and the following Setautosize method allows each line of content    
//To be displayed in its original content.    
$objStyleA 5->getnumberformat ()->setformatcode (phpexcel_style_numberformat::format_ number);

//Set Font    
$objFontA 5 = $objStyleA 5->getfont ();   
$objFontA 5-> SetName (' Courier New ');   
$objFontA 5->setsize (a);   
$objFontA 5-> Setbold (True);   
$objFontA 5->setunderline (phpexcel_style_font::underline_single);    
$objFontA 5->getcolor ()->setargb (' FF999999 ');   
  
// Set Alignment    
$objAlignA 5 = $objStyleA 5->getalignment ();   
$objAlignA 5-> Sethorizontal (phpexcel_style_alignment::horizontal_right);   
$objAlignA 5->setvertical ( Phpexcel_style_alignment::vertical_center);   
  
//Set border    
$ ObjBorderA5 = $objStyleA 5->getborders ();   
$objBorderA 5->gettop ()->setborderstyle ( Phpexcel_style_border::border_thin);   
$objBorderA 5->gettop ()->getcolor ()->setargb (' FFFF0000 '); color   
$objBorderA 5->getbottom ()->setborderstyle (phpexcel_style_border::border_ THIN);   
$objBorderA 5->getleft ()->setborderstyle (Phpexcel_style_border::border_thin);    
$objBorderA 5->getright ()->setborderstyle (Phpexcel_style_border::border_thin);    
  
//Setting Fill Color    
$objFillA 5 = $objStyleA 5->getfill ();    
$objFillA 5->setfilltype (phpexcel_style_fill::fill_solid);   
$ ObjFillA5->getstartcolor ()->setargb (' ffeeeeee ');

Copies the style information from the specified cell.
$objActSheet->duplicatestyle ($objStyleA 5, ' b1:c22 ');

//*************************************   


//Add picture


$objDrawing = new phpexcel_worksheet_drawing ();


$objDrawing-&gt;setname (' zealimg ');


$objDrawing-&gt;setdescription (' Image inserted by Zeal ');


$objDrawing-&gt;setpath ('.    /images/201003/1269919647975424741.jpg ');


$objDrawing-&gt;setheight (150);


$objDrawing-&gt;setcoordinates (' C23 ');


$objDrawing-&gt;setoffsetx (10);


$objDrawing-&gt;setrotation (15);


$objDrawing-&gt;getshadow ()-&gt;setvisible (true);


$objDrawing-&gt;getshadow ()-&gt;setdirection (36);


$objDrawing-&gt;setworksheet ($objActSheet);


  


//Add a new worksheet


$objExcel-&gt;createsheet ();


$objExcel-&gt;getsheet (1)-&gt;settitle (' Test 2 ');


  


//Protection cell


$objExcel-&gt;getsheet (1)-&gt;getprotection ()-&gt;setsheet (true);


$objExcel-&gt;getsheet (1)-&gt;protectcells (' A1:c22 ', ' phpexcel ');


//*************************************   


//output content


//   


$outputFileName = "Output.xls";


//To file


$objWriter-&gt;save ($outputFileName);


or


//To Browser


/*


header ("Content-type:application/force-download");


header ("Content-type:application/octet-stream");


header ("Content-type:application/download");


header (' Content-disposition:inline;filename= "'. $outputFileName. ');


header ("Content-disposition:attachment;filename= Commodity sales click Data Report. xls");


header ("Content-transfer-encoding:binary");


header ("Expires:mon, 1997 occurs GMT");


header ("last-modified:"). Gmdate ("D, D M Y h:i:s").    "GMT");


header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");


header ("Pragma:no-cache");


$objWriter-&gt;save (' php://output ');

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.