PHPExcel export excel_PHP tutorial

Source: Internet
Author: User
Tags getcolor
Export excel in PHPExcel. If garbled characters occur during Chinese export, you can convert the string to gb2312. for example, convert $ yourStr from UTF-8 to gb2312: $ yourStrmb_convert_encoding (gb2312, UTF-
If garbled characters occur when exporting Chinese characters, you can try to convert the string to gb2312. for example, convert $ yourStr from UTF-8 to gb2312: $ yourStr = mb_convert_encoding ("gb2312", "UTF-8 ", $ yourStr); summary php export Excel php import Excel PhpExcel use instructions PhpExcel User Manual 1: First, we recommend the cool PHPExcel, official website: http://www.codeplex.com/phpexcel, you can export the office2007format, compatible. The downloaded package contains documents and examples, which you can study on your own.
 SetOffice2003Compatibility (true); // set the basic attributes of the document $ objProps = $ objExcel-> getProperties (); $ 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-> setCate Gory ("Test"); // sets 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 with the SheetIndex set to 0 $ objExcel-> setActiveSheetIndex (0); $ objActSheet = $ objExcel-> getActiveSheet (); // Set the name of the current active sheet $ objActSheet-> setTitle ('test Sheet '); // Set the cell content. PHPExcel automatically determines the cell content type based on the input content $ objActSheet-> setCellValue ('A1', 'string content '); // String content $ objActSheet-> setCellValue ('A2 ', 26); // value $ objActSheet-> setCellValue ('A3', true ); // boolean value $ objActSheet-> setCellValue ('A4 ',' = SUM (A2: A2) '); // Formula // explicitly specify the content type $ objActSheet-> setCellValueExplicit ('a5', '123', PHPExcel_Cell_DataType: TYPE_STRING ); // merge cells $ objActSheet-> mergeCells ('b1: c22'); // separate cells $ objActSheet-> unmergeCells ('b1: c22 '); // set the width $ objActSheet-> getColumnDimension ('B')-> setAutoSize (true); $ objActSheet-> getColumnDimension ('A')-> setWidth (30 ); // Set the numeric format of the cell content. // If PHPExcel_Writer_Excel5 is used to generate content, // note that in the const variable definition of the PHPExcel_Style_NumberFormat class, other types can be used normally, but when setFormatCode // is FORMAT_NUMBER, the actual effect is not set to "0 ". You need to // modify the getXf ($ style) method in the PHPExcel_Writer_Excel5_Format class source code, // in if ($ this-> _ BIFF_version = 0x0500) {(near 363rd rows) first add a // line of code: // if ($ ifmt = '0') $ ifmt = 1; // set the format to PHPExcel_Style_NumberFormat: FORMAT_NUMBER, to prevent some large numbers from being displayed in scientific notation, you can use the setAutoSize method below to display all the content of each row according to the original content. $ ObjStyleA5 = $ objActSheet-> getStyle ('a5 '); $ objStyleA5-> getNumberFormat ()-> setFormatCode (PHPExcel_Style_NumberFormat: FORMAT_NUMBER ); // set the font $ objFontA5 = $ objStyleA5-> getFont (); $ objFontA5-> setName ('courier new'); $ objFontA5-> setSize (10 ); $ objFontA5-> setBold (true); $ objFontA5-> setUnderline (PHPExcel_Style_Font: UNDERLINE_SINGLE); $ objFontA5-> getColor ()-> setARGB ('ffff0000 '); $ objFontA5-> getCol Or ()-> setARGB (PHPExcel_Style_Color: COLOR_WHITE); // $ objFontA5-> getFont ()-> setColor (PHPExcel_Style_Color: COLOR_RED ); // Set the alignment mode $ align = $ objStyleA5-> getAlignment (); $ align-> setHorizontal (direction: HORIZONTAL_RIGHT); $ objAlignA5-> setVertical (direction: VERTICAL_CENTER ); // set the border $ objBorderA5 = $ objStyleA5-> getBorders (); $ objBorderA5-> getTop ()-> setBorderStyl E (PHPExcel_Style_Border: BORDER_THIN); $ objBorderA5-> getTop ()-> getColor ()-> setARGB ('ffffff0000'); // border color $ objBorderA5-> getBottom () -> setBorderStyle (border: BORDER_THIN); $ objBorderA5-> getLeft ()-> setBorderStyle (border: BORDER_THIN); $ objBorderA5-> getRight ()-> setBorderStyle (border:: BORDER_THIN); // Set the CELL fill color $ objFillA5 = $ objStyleA5-> getFill (); $ objFill A5-> setFillType (PHPExcel_Style_Fill: FILL_SOLID); $ objFillA5-> getStartColor ()-> setARGB ('ffeeeeee'); // copy the style information from the specified cell. $ objActSheet-> duplicateStyle ($ objStyleA5, 'b1: C22 '); // add an image $ objDrawing = new drawing (); $ objDrawing-> setName ('zealimg '); $ objDrawing-> setDescription ('image inserted by zeal'); $ objDrawing-> setPath ('. /zeali.net.logo.gif '); $ objDrawing-> setHeight (36); $ 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'); // protect cells $ objExcel-> getSheet (1)-> getProtection () -> setSheet (true); $ objExcel-> getSheet (1)-> protectCells ('a 1: c22', 'phpexcel '); // Display Gridlines: $ objPHPExcel-> getActiveSheet ()-> setShowGridlines (true ); // Display hidden columns $ objPHPExcel-> getActiveSheet ()-> getColumnDimension ('C')-> setVisible (true); $ objPHPExcel-> getActiveSheet () -> getColumnDimension ('D')-> setVisible (false); // Display hidden rows $ objPHPExcel-> getActiveSheet ()-> getRowDimension ('10 ') -> setVisible (false); // default column width $ objPHPExcel-> getActiveSheet ()-> getdefacolumcolumndimension ()-> setWidth (12 ); // Default row width $ objPHPExcel-> getActiveSheet ()-> getDefaultRowDimension ()-> setRowHeight (15); // worksheet default style settings (different from the default one, which must be set separately) $ objPHPExcel-> getActiveSheet ()-> getDefaultStyle ()-> getFont ()-> setName ('arial'); $ objPHPExcel-> getActiveSheet ()-> getdefastyle style () -> getFont ()-> setSize (8); $ objPHPExcel-> getActiveSheet ()-> getDefaultStyle ()-> getAlignment (); $ objPHPExcel-> getActiveSheet () -> getDefaultStyle ()-> getAlignment ()-> SetHorizontal (usage: HORIZONTAL_CENTER); $ objPHPExcel-> getActiveSheet ()-> getDefaultStyle ()-> getAlignment ()-> setVertical (usage: VERTICAL_CENTER ); // Output Content $ outputFileName = "output.xls"; // to the file // $ objWriter-> save ($ outputFileName ); // or // enter the browser header ("Content-Type: application/force-download"); header ("Content-Type: application/octet-stream "); header ("Content-T Ype: application/download "); header ('content-Disposition: inline; filename = "'. $ outputFileName. '"'); header (" Content-Transfer-Encoding: binary "); 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');?>

Convert $ yourStr from utf-8 to gb2312: $ yourStr = mb_convert_encoding ("gb2312", "UTF -...

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.