PHP Export Excel

Source: Internet
Author: User

With the Phpexcel class export Excel, because before directly through the header export download Excel, after the local open has been garbled, but if the direct save on the server open is not garbled, there is no way to finally save to the server, and then after downloading and then delete.

Attached code

1<?PHP2 require_once' Phpexcel.php ';3 require_once' phpexcel/writer/excel5.php ';//for other low version xls4 require_once' phpexcel/writer/excel2007.php ';//for excel-2007 format5 Header("content-type:text/html; Charset=utf-8 ");6 7 $objExcel=NewPhpexcel ();//Create a Processing object instance8 $objWriter=NewPhpexcel_writer_excel2007 ($objExcel);//for 2007 formats9 //$objWriter = new Phpexcel_writer_excel5 ($objExcel);//For other version formatsTen  One //************************************* A //Set document basic Properties - //$objProps = $objExcel->getproperties (); - //$objProps->setcreator ("Zeal Li"); the //$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"); +  A //************************************* at //sets the current sheet index for subsequent content operations.  - //Generally only need to display the call when using multiple sheet.  - //By default, Phpexcel will automatically create the first sheet to be set sheetindex=0 -  - $objExcel->setactivesheetindex (0); - $objActSheet=$objExcel-Getactivesheet (); in  - //sets the name of the currently active sheet to $objActSheet->settitle (' Test sheet '); +  - //Set cell contents the // * //By phpexcel automatically determine cell content type based on incoming content $ $objActSheet->setcellvalue (' A1 ', ' string content ');//string ContentPanax Notoginseng $objActSheet->setcellvalue (' A2 ', 26);//value - $objActSheet->setcellvalue (' A3 ',true);//Boolean value the $objActSheet->setcellvalue (' A4 ', ' =sum (A2:A2) ');//Formula +  A  the //Explicitly specify a content type + $objActSheet->setcellvalueexplicit (' A5 ', ' 847475847857487584 ', -Phpexcel_cell_datatype::type_string); $  $ //Merge Cells - //$objActSheet->mergecells (' b1:c22 '); -  the //Detach cells - //$objActSheet->unmergecells (' b1:c22 ');Wuyi  the //************************************* - //Set cell style Wu // -  About //Set width $ //$objActSheet->getcolumndimension (' B ')->setautosize (true); - //$objActSheet->getcolumndimension (' A ')->setwidth (+); -  - //$objStyleA 5 = $objActSheet->getstyle (' A5 '); A  + //sets the number format of the cell contents.  the // - //If PHPEXCEL_WRITER_EXCEL5 is used to generate content, $ //You need to note that the const variable in the Phpexcel_style_numberformat class defines the the //In various custom formats, other types can be used normally, but when Setformatcode the //For format_number, the actual effect is not formatted as "0". Need to the //Modify the GETXF ($style) method in the source code of the Phpexcel_writer_excel5_format class, the //Add one in front of if ($this->_biff_version = = 0x0500) {(Near line No. 363) - //Line code: in //if ($ifmt = = = ' 0 ') $ifmt = 1; the // the //format as Phpexcel_style_numberformat::format_number, avoid some large numbers About //used in scientific notation, with the following Setautosize method to let the contents of each line the //Both are displayed according to the original content.  the //$objStyleA 5 the //->getnumberformat () + //->setformatcode (phpexcel_style_numberformat::format_number); -  the //Set FontBayi //$objFontA 5 = $objStyleA 5->getfont (); the //$objFontA 5->setname (' Courier New '); the //$objFontA 5->setsize (ten); - //$objFontA 5->setbold (true); - //$objFontA 5->setunderline (phpexcel_style_font::underline_single); the //$objFontA 5->getcolor ()->setargb (' FF999999 '); the  the //Set alignment the //$objAlignA 5 = $objStyleA 5->getalignment (); - //$objAlignA 5->sethorizontal (phpexcel_style_alignment::horizontal_right); the //$objAlignA 5->setvertical (phpexcel_style_alignment::vertical_center); the  the //Set Border94 //$objBorderA 5 = $objStyleA 5->getborders (); the //$objBorderA 5->gettop ()->setborderstyle (Phpexcel_style_border::border_thin); the //$objBorderA 5->gettop ()->getcolor ()->setargb (' FFFF0000 ');//Color the //$objBorderA 5->getbottom ()->setborderstyle (Phpexcel_style_border::border_thin);98 //$objBorderA 5->getleft ()->setborderstyle (Phpexcel_style_border::border_thin); About //$objBorderA 5->getright ()->setborderstyle (Phpexcel_style_border::border_thin); - 101 //Set Fill color102 //$objFillA 5 = $objStyleA 5->getfill ();103 //$objFillA 5->setfilltype (phpexcel_style_fill::fill_solid);104 //$objFillA 5->getstartcolor ()->setargb (' ffeeeeee '); the 106 //Copy style information from the specified cell.107 //$objActSheet->duplicatestyle ($objStyleA 5, ' b1:c22 ');108 109  the //*************************************111 //Add picture the //$objDrawing = new phpexcel_worksheet_drawing ();113 //$objDrawing->setname (' zealimg '); the //$objDrawing->setdescription (' Image inserted by Zeal '); the //$objDrawing->setpath ('./zeali.net.logo.gif '); the //$objDrawing->setheight (+);117 //$objDrawing->setcoordinates (' C23 ');118 //$objDrawing->setoffsetx (ten);119 //$objDrawing->setrotation (); - //$objDrawing->getshadow ()->setvisible (true);121 //$objDrawing->getshadow ()->setdirection ();122 //$objDrawing->setworksheet ($objActSheet);123 124  the //Add a new worksheet126 $objExcel-Createsheet ();127 $objExcel->getsheet (1)->settitle (' Test 2 '); - 129 //Protect Cells the //$objExcel->getsheet (1)->getprotection ()->setsheet (true);131 //$objExcel->getsheet (1)->protectcells (' A1:c22 ', ' phpexcel '); the 133 134 //*************************************135 //output content136 //137 138 $filename= "Tttt.xlsx";139 $objWriter->save ($filename);//Save to server $ 141 Ob_end_clean();//clear buffer to avoid garbled characters142 $filename=Iconv(' Utf-8 ', ' GBK ',$filename);143 $filesize=filesize($filename);144 Header("Content-type:application/force-download;charset=utf-8");145 Header("Content-disposition:attachment; Filename= ".$filename);146 Header("Content-length:".)$filesize);147 Ob_clean();148 ReadFile($filename);149  Max unlink($filename);//Delete server files after download is complete151?>

PHP Export Excel

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.