<? Php // Write content to the xls file Error_reporting (E_ALL ); Ini_set ('display _ errors ', TRUE ); Include 'classes/PHPExcel. php '; Include 'classes/PHPExcel/IOFactory. php '; // $ Data: the body of the xls file // $ Title: The content title of the xls file // $ Filename: exported file name // $ Data and $ title must be UTF-8; otherwise, FALSE values are written. Function write_xls ($ data = array (), $ title = array (), $ filename = 'report '){ $ ObjPHPExcel = new PHPExcel (); // Set document attributes. garbled characters are generated when Chinese characters are set... // $ ObjPHPExcel-> getProperties ()-> setCreator ("yunshu ") //-> SetLastModifiedBy ("yunshu ") //-> SetTitle ("Product URL export ") //-> SetSubject ("Product URL export ") //-> SetDescription ("Product URL export ") //-> SetKeywords ("Product URL export "); $ ObjPHPExcel-> setActiveSheetIndex (0 );
$ Cols = 'abcdefghijklmnopqrstuvwxy '; // Set www.111cn.net title For ($ I = 0, $ length = count ($ title); $ I <$ length; $ I ++ ){ // Echo $ cols {$ I}. '1 '; $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ($ cols {$ I}. '1', $ title [$ I]); } // Set the title Style $ TitleCount = count ($ title ); $ R = $ cols {0}. '1 '; $ C = $ cols {$ titleCount}. '1 '; $ ObjPHPExcel-> getActiveSheet ()-> getStyle ("$ r: $ c")-> applyFromArray ( Array ( 'Font' => array ( 'Bold '=> true ), 'Alignment '=> array ( 'Horizontal '=> PHPExcel_Style_Alignment: HORIZONTAL_RIGHT, ), 'Borders' => array ( 'Top' => array ( 'Style' => PHPExcel_Style_Border: BORDER_THIN ) ), 'Fill' => array ( 'Type' => PHPExcel_Style_Fill: FILL_GRADIENT_LINEAR, 'Rotation' => 90, 'Startcolor' => array ( 'Arg' => 'ffa0a0' ), 'Endcolor' => array ( 'Arg' => 'ffffffff' ) ) ) );
$ I = 0; Foreach ($ data as $ d) {// use foreach here. It supports associating arrays and Numerical index arrays. $ J = 0; Foreach ($ d as $ v) {// use foreach here. It supports associating arrays and Numerical index arrays. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ($ cols {$ j}. ($ I + 2), $ v ); $ J ++; } $ I ++; } // Generate an xls file in 2003excel format Header ('content-Type: application/vnd. ms-excel '); Header ('content-Disposition: attachment; filename = "'.w.filename.'.xls "'); Header ('cache-Control: max-age = 0 '); $ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 '); $ ObjWriter-> save ('php: // output '); } $ Array = array ( Array (1111, 'name', 'brand', 'commodity name', 'HTTP: // www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name', 'HTTP: // www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name', 'HTTP: // www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name', 'HTTP: // www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name', 'HTTP: // www.baidu.com '), ); Write_xls ($ array, array ('item id', 'Supplier name', 'brand', 'item name', 'url'), 'report ');
?> |