PHPExcel: PHP open source class library generated by documents such as Excel

Source: Internet
Author: User
Tags php excel
Documentation. It also supports the generation of PDF, HTML, and CSV documents. PHPExcel also supports filling data with existing Excel documents as templates to generate the above documents. In addition, its API documentation is equivalent to the demo

Documentation. It also supports the generation of PDF, HTML, and CSV documents. PHPExcel also supports filling data with existing Excel documents as templates to generate the above documents. In addition, its API documentation is equivalent to the demo

Documentation. It also supports the generation of PDF, HTML, and CSV documents. PHPExcel also supports filling data with existing Excel documents as templates to generate the above documents. In addition, its API documentation and demo are quite complete and very helpful for developers.

The official PHPExcel website is as follows:

Http://phpexcel.codeplex.com/

The current version is 1.7.6. The downloaded and decompressed PHPExcel directory structure is as follows:


Where:

Classes directoryThe following is the PHPExcel source code file:PHPExcel. php fileIs the class library interface, through which external PHP code calls PHPExcel;PHPExcel directoryThe specific operation code for reading and generating different documents (such as Excel, Excel2007, and PDF) is called by the PHPExcel. php file under the Classes directory in the factory mode.

Documentation directoryThe official PHPExcel help documentation, especiallyAPI directoryThe following web files are all PHPExcel API help documents, while others are about this open-source project.

Tests directoryThere are nearly 40 sample Applets in PHPExcel, which can be put together with the Classes directory in the Apache + PHP environment for testing and learning.

Below are some common PHP Excel Code segments:

 Load ("template.xls");/*** the instantiated PHPExcel object is similar to a file saved in memory, * You can perform operations on it to modify the document data * // set the document attributes $ phpexcel-> getProperties ()-> setCreator ("Liu Jian ") // Document Author-> setLastModifiedBy ("Liu Jian") // The Last modifier-> setTitle ("Office 2003 XLS Test Document ") // Title-> setSubject ("Office 2003 XLS Test Document") // topic-> setDescription ("Test document for Office 2003 XLS, generated using PHPExcel. ") // remarks-> setKeywords (" o Ffice 2003 openxml php ") // keyword-> setCategory (" Test result file "); // category // blank document created by default (new) there is only one Worksheet (sheet), and its number (index) is 0 // you can add a new worksheet $ phpexcel-> createSheet (1) as follows ); // obtain the worksheet $ sheet = $ phpexcel-> getSheet (1); // set the number of the currently activated worksheet $ phpexcel-> setActiveSheetIndex (1 ); // obtain the currently activated worksheet $ sheet = $ phpexcel-> getActiveSheet (); // after obtaining the worksheet, you can operate on its cell to modify the data. // modify the worksheet name $ sheet-> setTitle ("Test "); // set the cell A5 value $ sheet-> set CellValue ("A5", date ('Y-m-d h: I: s'); // set 3rd rows and 5th columns (E3) value $ sheet-> setCellValueByColumnAndRow (4, 3, date ('Y-m-d h: I: s ')); // get the value of cell A5 $ sheet-> getCell ("A5")-> getValue (); // merge cells $ sheet-> mergeCells ("C3: G6 "); // split the merged cell $ sheet-> unmergeCells ("C3: G6"); // set the attribute of row 3rd $ sheet-> getRowDimension (3)-> setRowHeight (100) // Row Height-> setVisible (true) // whether it is visible. The default value is true-> setRowIndex (6) // change the row number to 6-> setOutlineLevel (5 ); // priority. The default value is 0. The number must be 0 to 7 // set the attribute of column F // getColumnDimension ("F") can be replaced by $ sheet-> getColumnDimension ("F") with getColumnDimension ("F ") -> setWidth (200) // column width-> setColumnIndex ("I") // change the column number to I-> setVisible (false) // visible or not-> setAutoSize (true); // automatically adapts to the column width. // insert one row before the first row to change the row to the new 3rd row, the other rows move down 1 row in sequence $ sheet-> insertNewRowBefore (3, 1); // insert 1 column before Row C, and the column will become the new column C, for other columns, right shift 1 column $ sheet-> insertNewColumnBefore ("C", 1); // method 1 $ sheet-> insertNewColumnBeforeByIn Dex (2, 1); // method 2, column C is the 2nd Column // get the style object of cell D3 $ style = $ sheet-> getStyle ("D3 "); // equivalent to getStyleByColumnAndRow (3, 3) // set the font attribute of the cell $ style-> getFont ()-> setBold (true) // whether it is bold-> setSize (16) // font size-> setName ("Gungsuh") // The font name, only applicable to foreign fonts-> setItalic (true) // whether it is italic-> setStrikethrough (true) // whether there is a strikethrough-> setUnderline (TRIM: UNDERLINE_DOUBLEACCOUNTING) // The underline type-> getColor ()-> setARGB (PHPExcel_Style_Color :: CO LOR_BLUE); // font color // set the background filling attribute of the cell $ style-> getFill ()-> setFillType (PHPExcel_Style_Fill: FILL_SOLID) // fill mode-> getStartColor ()-> setARGB (PHPExcel_Style_Color: COLOR_YELLOW); // set the background color // set the format of the number in the cell $ style-> getNumberFormat () -> setFormatCode ("0.00"); // set the text alignment in the cell $ style-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER) // horizontal direction-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CE NTER); // $ sheet-> setCellValue ("D3", "12.3456"); // Save the PHPExcel_IOFactory: createWriter ($ phpexcel, 'excel5') locally ') -> save ("output.xls"); // output the document to the page header ('content-Type: application/vnd. ms-excel '); header ('content-Disposition: attachment; filename = "test.xls"'); header ('cache-Control: max-age = 0'); PHPExcel_IOFactory:: createWriter ($ phpexcel, 'excel5')-> save ('php: // output');?>

Note that:

1.PHPExcel itself has functions related to inserting images into cells and setting cell borders. However, I personally feel that it is easier to dynamically add images and set borders without using static excel templates to generate documents. For more information, see the final references.

2.When a new row or column is added, the style of the new row or column is automatically consistent with the previous row/column of the new row/column.

3.When setting the cell fill color, you must first set the fill mode, otherwise it will become invalid.

4.Through the static method createReader or createWriter of PHPExcel_IOFactory, you must input the parameter string corresponding to the document format, where:

  • Excel 2003(.xls)
  • Excel 2007(.xlsx)
  • Response (response)
  • Csv(csv(.csv)

When outputting a document, the file extension must match the parameter string.


References: http://apps.hi.baidu.com/share/detail/35261920

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.