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->setname (' zealimg ');
$objDrawing->setdescription (' Image inserted by Zeal ');
$objDrawing->setpath ('. /images/201003/1269919647975424741.jpg ');
$objDrawing->setheight (150);
$objDrawing->setcoordinates (' C23 ');
$objDrawing->setoffsetx (10);
$objDrawing->setrotation (15);
$objDrawing->getshadow ()->setvisible (true);
$objDrawing->getshadow ()->setdirection (36);
$objDrawing->setworksheet ($objActSheet);
//Add a new worksheet
$objExcel->createsheet ();
$objExcel->getsheet (1)->settitle (' Test 2 ');
//Protection cell
$objExcel->getsheet (1)->getprotection ()->setsheet (true);
$objExcel->getsheet (1)->protectcells (' A1:c22 ', ' phpexcel ');
//*************************************
//output content
//
$outputFileName = "Output.xls";
//To file
$objWriter->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->save (' php://output ');