< Require_once '. /.. /.. /libs/phpexcel/classes/phpexcel.php '; Require_once '. /.. /.. /libs/phpexcel/classes/phpexcel/writer/excel5.php '; Include_once '. /.. /.. /libs/phpexcel/classes/phpexcel/iofactory.php '; Include '. /common/config.php '; Create a Processing object instance (this object is the same for 2003 2007) $objExcel = new Phpexcel (); Set properties (This code doesn't matter, the content can be replaced with what you need) $objExcel->getproperties ()->setcreator ("Office 2003 Excel"); $objExcel->getproperties ()->setlastmodifiedby ("Office 2003 Excel"); $objExcel->getproperties ()->settitle ("Office 2003 XLS Test Document"); $objExcel->getproperties ()->setsubject ("Office 2003 XLS Test Document"); $objExcel->getproperties ()->setdescription ("Test document for Office 2003 XLS, generated using PHP classes."); $objExcel->getproperties ()->setkeywords ("Office 2003 OPENXML PHP"); $objExcel->getproperties ()->setcategory ("Test result file"); Start processing data (index starting from 0) $objExcel->setactivesheetindex (0); $conn = Mssql_connect ($config [' MSSQL '] [' host '], $config [' MSSQL ' [' User '], $config [' MSSQL '] [' password ']); mssql_select_db ($config [' MSSQL '] [' dbname '], $conn); $tm =$_request[' TM ']; $sql = "exec hnow05_getttspace ', '". $tm. "', ' ', 1"; $sql =mb_convert_encoding ($sql, ' GBK ', ' UTF-8 '); $res =mssql_query ($sql); $i = 0; $k = Array (' station code ', ' Station name ', ' river system ', ' to quote time ', ' water level ', ' Waters '); $count = count ($k); $arrs = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F '); Add a table header for ($i =0; $i < $count; $i + +) { $objExcel->getactivesheet ()->setcellvalue ($arrs [$i]. 1 "," $k [$i] "); } /*--------read data from the database-------*/ $i = 0; while ($arr =mssql_fetch_array ($res)) { $STCD = $arr ["Stcd"]; $STNM = $arr ["STNM"]; $RVNM = $arr ["Rvnm"]; $tm = $arr ["TM"]; $tdz = $arr ["Tdz"]; $TDPTN = $arr ["TDPTN"]; if ($TDPTN = = ' 6 ') { $TDPTN = ' flat '; }else if ($tdptn = = ' 5 ') { $TDPTN = ' rise '; }else if ($tdptn = = ' 4 ') { $TDPTN = ' fall '; } $u 1= $i +2; $STNM =iconv ("GBK", "Utf-8", $STNM); $rvnm =iconv ("GBK", "Utf-8", $rvnm); $tm =iconv ("GBK", "Utf-8", $TM); /*----------Write content-------------*/ $objExcel->getactivesheet ()->setcellvalue (' a '. $u 1, "$stcd"); $objExcel->getactivesheet ()->setcellvalue (' B '. $u 1, "$stnm"); $objExcel->getactivesheet ()->setcellvalue (' C '. $u 1, "$rvnm"); $objExcel->getactivesheet ()->setcellvalue (' d '. $u 1, "$tm"); $objExcel->getactivesheet ()->setcellvalue (' E '. $u 1, "$tdz"); $objExcel->getactivesheet ()->setcellvalue (' F '. $u 1, "$TDPTN"); $i + +; } /*----------Set the cell border and color-------------*/ $rows = Mssql_num_rows ($res); for ($i =0; $i < ($rows + 1); $i + +) { for ($j =0; $j < $count; $j + +) { $a = $i +1; $objExcel->getactivesheet ()->getstyle ($arrs [$j]. $a)->getborders ()->getallborders () Setborderstyle (Phpexcel_style_border::border_thin); $objExcel->getactivesheet ()->getstyle ($arrs [$j]. $a)->getborders ()->getallborders ()->getcolor ()- >setargb (' ff00bbcc '); Center horizontally $objExcel->getactivesheet ()->getstyle ($arrs [$j]. $a)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_center); } } Width of the high column $objExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (10); $objExcel->getactivesheet ()->getcolumndimension (' B ')->setwidth (15); $objExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (15); $objExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (20); $objExcel->getactivesheet ()->getcolumndimension (' E ')->setwidth (10); $objExcel->getactivesheet ()->getcolumndimension (' F ')->setwidth (10); Set up headers and footers. If there are no different headings odd/even use a single-head assumption. $objExcel->getactivesheet ()->getheaderfooter ()->setoddheader (' &l&bpersonal cash register& rprinted on &d '); $objExcel->getactivesheet ()->getheaderfooter ()->setoddfooter (' &l&b '. $objExcel GetProperties ()->gettitle (). ' &rpage &p of &n '); Set page orientation and size $objExcel->getactivesheet ()->getpagesetup ()->setorientation (phpexcel_worksheet_pagesetup::orientation _portrait); $objExcel->getactivesheet ()->getpagesetup ()->setpapersize (phpexcel_worksheet_pagesetup::P apersize_a4) ; Renaming a table $objExcel->getactivesheet ()->settitle (' real-time tidal condition '); Set Active sheet Index to the first sheet, so Excel opens this as the first sheet $objExcel->setactivesheetindex (0); Redirect output to a client ' s Web browser (EXCEL5) saved to excel2003 format Set the name of Excel $excelName = ' real-time tidal conditions ('. $tm. ') '; $excelName = ' excel_ '. Date ("Ymdhis"); Header (' Content-type:application/vnd.ms-excel '); Header (' cache-control:max-age=0 '); Header (' content-disposition:attachment; Filename= '. Iconv ("Utf-8", "GBK", $excelName). XLS '); $objWriter = Phpexcel_iofactory::createwriter ($objExcel, ' Excel5 '); $objWriter->save (' php://output '); Exit
?>
|