: 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.