Php exports excel data. Two excel export methods are provided. 1. the easiest way to export excelheader (Content-Type: applicationvnd. ms-excel); you can set the file type to vnd. ms-excel to xml (export xml file) provides two ways to export excel
1. simplest excel export
Header ('content-Type: application/vnd. ms-excel '); // you can change vnd. ms-excel' to xml to export an xml file)
Header ('content-Disposition: attachment; filename = "cloud platform user report .xls" '); // you can specify the name of the exported excel file.
Header ('cache-Control: max-age = 0 ');
Echo iconv ("UTF-8", "gbk", "Time \ t error code \ t occurrence times \ t error code meaning \ n"); // \ t is a tab, \ n is a line break
Foreach ($ arr as $ key => $ val) {// $ arr is the data to be exported
Echo iconv ("UTF-8", "gbk", date ("Y-m-d", $ val ["time"]). "\ t ". $ val ["error_code"]. "\ t ". $ val ["num"]. "\ t ". $ val ["code_mean"]. "\ n ");
}
2. use phpexcel to export excel
Download the phpexcel package from the Internet and put the phpexcel. php and phpexcel packages into your project.
Instance:
Require_once ('phpexcel. php'); // reference the two files
Require_once ('phpexcel/Writer/excel2007.php ');
// Export excel
$ ObjExcel = new PHPExcel ();
$ 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-> setCategory ("Test ");
$ 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-> 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
$ ObjExcel-> setActiveSheetIndex (0 );
$ ObjActSheet = $ objExcel-> getActiveSheet ();
// Online games
// Set the name of the current active sheet
$ ObjActSheet-> setTitle ('Online Games class ');
$ ObjActSheet-> setCellValue ('A1', 'Game name'); // The first row of data in the first sheet in the generated excel file
$ ObjActSheet-> setCellValue ('b1 ', 'specific domain name ');
$ ObjActSheet-> setCellValue ('C1', 'server description ');
$ ObjActSheet-> setCellValue ('d1 ', 'line attribute ');
$ ObjActSheet-> setCellValue ('e1 ', 'submitted server IP address ');
$ ObjActSheet-> setCellValue ('F1', 'corresponding to the optimized route entry ');
$ ObjActSheet-> setCellValue ('G1', 'keyword optimiz ');
$ ObjActSheet-> setCellValue ('h1 ', 'optimized line ');
$ ObjActSheet-> setCellValue ('i1', 'optimized status ');
Foreach ($ result_webgame as $ k1 => $ v1) {// traverse data read from the database
$ ObjActSheet-> setCellValue ('A'. ($ k1 + 2), $ v1 ["gamename"]);
$ ObjActSheet-> setCellValue ('B'. ($ k1 + 2), $ v1 ['domain ']);
$ ObjActSheet-> setCellValue ('C'. ($ k1 + 2), $ v1 ['gameserver ']);
$ ObjActSheet-> setCellValue ('D'. ($ k1 + 2), $ v1 ['line _ attribute']);
$ ObjActSheet-> setCellValue ('E'. ($ k1 + 2), $ v1 ['server _ IP']);
$ ObjActSheet-> setCellValue ('F'. ($ k1 + 2), $ v1 ['optimize _ route ']);
$ ObjActSheet-> setCellValue ('g'. ($ k1 + 2), $ v1 ['keyword']);
$ ObjActSheet-> setCellValue ('H'. ($ k1 + 2), $ v1 ['line']);
$ ObjActSheet-> setCellValue ('I'. ($ k1 + 2), $ v1 ['optimize _ status']);
}
// Website type
// Add a new worksheet
$ ObjExcel-> createSheet ();
$ Reource_ready = $ objExcel-> getSheet (1)-> setTitle ('website class ');
$ Reource_ready-> setCellValue ('A1', 'website name ');
$ Reource_ready-> setCellValue ('b1 ', 'specific domain name ');
$ Reource_ready-> setCellValue ('C1', 'line attribute ');
$ Reource_ready-> setCellValue ('d1 ', 'Ip segment ');
$ Reource_ready-> setCellValue ('e1 ', 'optimized line ');
$ Reource_ready-> setCellValue ('F1', 'optimized status ');
Foreach ($ result_website as $ k1 => $ v2 ){
$ Reource_ready-> setCellValue ('A'. ($ k1 + 2), $ v2 ["name"]);
$ Reource_ready-> setCellValue ('B'. ($ k1 + 2), $ v2 ['domain ']);
$ Reource_ready-> setCellValue ('C'. ($ k1 + 2), $ v2 ['line _ attribute']);
$ Reource_ready-> setCellValue ('D'. ($ k1 + 2), $ v2 ['IP']);
$ Reource_ready-> setCellValue ('E'. ($ k1 + 2), $ v2 ['line']);
$ Reource_ready-> setCellValue ('F'. ($ k1 + 2), $ v2 ['optimize _ status']);
}
// Similarly, you can export an excel file containing multiple sheets.
When 1, the simplest export excel header (Content-Type: application/vnd. ms-excel); // you can set the file type to vnd. change ms-excel to xml (export xml file )...