PHP export excelphp use phpexcel to export excel files
// Load the PHPExcel class
- Require './phpexcel/PHPExcel. php ';
// Create an excel Object instance
- $ ObjPHPExcel = new PHPExcel ();
// Set the basic attributes of the document
- $ ObjProps = $ objPHPExcel-> getProperties ();
- $ ObjProps-> setCreator ("Lao Mao ");
- $ ObjProps-> setLastModifiedBy ("Lao Mao ");
- $ 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 ");
// Set 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 and set SheetIndex = 0
- $ ObjPHPExcel-> setActiveSheetIndex (0 );
// Set the name of the current active sheet
- $ ObjActSheet = $ objPHPExcel-> getActiveSheet ();
- $ ObjActSheet-> setTitle ('test Sheet ');
// Set the cell content
- // The data here can be read from the database and processed cyclically.
- // By bbs.it-home.org
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('A1', 'A1 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('A2 ', 'A2 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('A3 ', 'A3 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('A4 ', 'A4 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('a5 ', 'a5 ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('b1 ', 'b1 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('B2', 'B2 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('b3', 'b3 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('B4 ', 'B4 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('b5 ', 'b5 ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('C1', 'C1 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('C2 ', 'C2 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('C3', 'C3 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('C4 ', 'C4 ');
- $ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('c5 ', 'c5 ');
// Output document
- $ ObjWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel );
// Set the header information and output it to the browser
- // Header ('content-Type: application/vnd. ms-excel ');
- // Header ("Content-Disposition: attachment; filename=demo.xls ");
- // Header ('cache-Control: max-age = 0 ');
- // $ ObjWriter-> save ('php: // output ');
// Save to a location
- $ ObjWriter-> save (dirname (_ FILE _). '/demo.xls ');
|