Create an Excel file in PHP and write data to it

Source: Internet
Author: User
: This article mainly introduces how to use PHP Excel to create an Excel file and write data. For more information about PHP tutorials, see.
Step 1 download the PHPExcel class library http://phpexcel.codeplex.com/?dir = str_replace ('\', '/', dirname (_ FILE __));Require_ Once $ dir. '/PHPExcel. php '; $ excelObj = new PHPExcel (); // equivalent to creating an Excel file on the desktop. // create three built-in tables for ($ I = 1; $ I <= 3; $ I ++) {if ($ I> 1) {// because there is a page by default, all from the second start $ excelObj-> createSheet (); // create a built-in table} $ excelObj-> setActiveSheetIndex ($ i-1); // start from 0 $ currentSheet = $ excelObj-> getActiveSheet (); // get the current active sheet $ currentSheet-> setCellValue ('A1', 'parent ID')-> setCellValue ('b1 ', 'model ') -> setCellValue ('C1 ', 'method')-> setCellValue ('d1', 'name '); // A1 indicates the first column B1 of the first row represents the second column of the first row, and so on... $ data = DB: getData ($ I); // This is the data read from the database, and the data whose type is equal to $ I is retrieved every cycle, write the Excel file $ j = 2; foreach ($ data as $ val) {$ currentSheet-> setCellValue ('A '. $ j, $ val ['parentid'])-> setCellValue ('B '. $ j, $ val ['model'])-> setCellValue ('C '. $ j, $ val ['action'])-> setCellValue ('D '. $ j, $ val ['name']); $ j ++; // change one row to write data every cycle} // the following two lines of code are written to the Excel file $ sheeetWrite = PHPExcel_IOFactory: createWriter ($ excelObj, 'excel2007 '); $ sheeetWrite-> save ($ dir. '/testexcel.xlsx ');

The above describes how to create an Excel file and write data in PHP Excel, including The require content. if you are interested in the PHP Tutorial.

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.