Phpexcel processing Excel file is really a powerful tool, I have a report, with pie chart, need to turn to Excel, Phpexcel has a relevant example, reference and modified to achieve this effect,
Allows users to generate and download Excel files during a click on the download process and generate pie charts in Excel mid-range.
There are two main changes to the example:
1. Change to fetch data from MySQL database
2. Added the Chinese file name in some browsers, such as IE, download the name garbled solution.
The PHP reports are as follows:
Converted to XLS:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4kpha+tprc68jnz8i6pc9wpgo8cd4gphbyzsbjbgfzcz0= "Brush:java;" > GetProperties ()->setcreator ("Xiongchuanliang")->setlastmodifiedby ("Xiongchuanliang")->settitle ("Summary table") ; $objActSheet = $objPHPExcel->getactivesheet (); $objActSheet->getcolumndimension (' A ')->setwidth (50); $objActSheet->getcolumndimension (' B ')->setwidth (50); $objActSheet->getrowdimension (1)->setrowheight (30); $objActSheet->getrowdimension (2)->setrowheight (16); $objActSheet->mergecells (' a1:c1 '); $objActSheet->mergecells (' a2:c2 '); Set the center alignment $objActSheet->getstyle (' A1 ')->getalignment ()->sethorizontal (phpexcel_style_alignment:: Horizontal_center); $objActSheet->getstyle (' A2 ')->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_ CENTER); $objFontA 1 = $objActSheet->getstyle (' A1 ')->getfont (); $objFontA 1->setsize (18); $objFontA 1->setbold (TRUE); $sql = mysql_query ("SELECT * as State_name , COUNT (*) as Stat_count from (...) k GROUP by STatus ORDER by Status "); $info = Mysql_fetch_array ($sql); $objActSheet->setcellvalue (' A1 ', ' summary table '); if (strlen (Trim ($sdev _model)) > 0) {$objActSheet->setcellvalue (' A2 ', "Model: XXXXXX");} $row = 3; $objActSheet->setcellvalue (' A '. $row, ' state '); $objActSheet->setcellvalue (' B '. $row, ' total quantity '); $row = 4; do{$objActSheet->setcellvalueexplicit (' A '. $row, $info [' State_name '],phpexcel_cell_datatype::type_string); $ Objactsheet->setcellvalueexplicit (' B '. $row, $info [' Stat_count '],phpexcel_cell_datatype::type_numeric); $objActSheet->setcellvalue (' A '. $row, $info [' state_name ']); $objActSheet->setcellvalue (' B '. $row, $info [' Stat_count ']); $row + +; }while ($info =mysql_fetch_array ($sql));//////////////////////////////////////////////////////////////////////// /for ($currrow = 3; $currrow < $row; $currrow + +) {//Set Border $objActSheet->getstyle (' A '. $currrow)->getborders ()-&G T;gettop ()->setborderstyle (Phpexcel_style_border::border_thin); $objActSheet->getstyle (' A '. $currrow)->getbOrders ()->getleft ()->setborderstyle (Phpexcel_style_border::border_thin); $objActSheet->getstyle (' A '. $currrow)->getborders ()->getright ()->setborderstyle (phpexcel_style_ Border::border_thin); $objActSheet->getstyle (' A '. $currrow)->getborders ()->getbottom ()->setborderstyle (phpexcel_style_ Border::border_thin); $objActSheet->getstyle (' B '. $currrow)->getborders ()->gettop ()->setborderstyle (Phpexcel_style_border :: Border_thin); $objActSheet->getstyle (' B '. $currrow)->getborders ()->getleft ()->setborderstyle (phpexcel_style_ Border::border_thin); $objActSheet->getstyle (' B '. $currrow)->getborders ()->getright ()->setborderstyle (phpexcel_style_ Border::border_thin); $objActSheet->getstyle (' B '. $currrow)->getborders ()->getbottom ()->setborderstyle (phpexcel_style_ Border::border_thin); }////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////Set the Labels for each data series we want to plot//datatype//cells reference for data//Format Cod e//number of datapoints in series//data values//data MARKER$DATASERIESLABELS1 = Array (new Phpexcel_chart_dataseriesval UEs (' String ', ' worksheet! $B $ $ ', NULL, 1),);//Set the x-axis labels//datatype//Cell reference for data//Format code// Number of datapoints in series//data values//data marker$xaxistickvalues1 = Array (new Phpexcel_chart_dataseriesvalues (' String ', ' worksheet! $A $4: $A $ '. $row, NULL, 4);//Set the Data values for each data series we want to plot//datatype//C Ell reference for data//Format code//number of datapoints in series//data values//data marker$dataseriesvalues1 = Arra Y (New Phpexcel_chart_dataseriesvalues (' number ', ' worksheet! $B $4: $B $ '. $row, NULL, 4),);//Build the Dataseries$series1 = new Phpexcel_chart_dataseries (Phpexcel_chart_dataseries::type_piechart,//PlotType phpexcel_chart_dataseries:: Grouping_standard,//plotgrouping Range (0, COUNT ($dataSeriesValues 1)-1),//Plotorder $dataseriesLabels 1,//Plotlabel $xAxisTickValues 1,//Plotcategory $dataSeriesValues 1//plotvalues);//Set up a layout object for the Pie chart$layout1 = new Phpexcel_chart_layout (); $layou T1->setshowval (true); $layout 1->setshowpercent (TRUE);//Set the series in the plot area$plotarea1 = new Phpexcel_ Chart_plotarea ($layout 1, Array ($series 1));//Set the chart legend$legend1 = new Phpexcel_chart_legend (phpexcel_chart_ Legend::P osition_right, NULL, False), $title 1 = new Phpexcel_chart_title (' summary table ');//Create The CHART$CHART1 = new Phpexcel_ Chart (' Chart1 ',//name $title 1,//title $legend 1,//Legend $plotarea 1,//PlotArea true,//Plotvisibleonly 0,//disp Layblanksas null,//Xaxislabel NULL//Yaxislabel-pie charts t have a y-axis);//Set the position where the chart s Hould appear in the Worksheet$row + = 2; $chart 1->settopleftposition (' A '. $row); $row + = $chart 1-> Setbottomrightposition (' C '. $row);//ADD the chart to THe Worksheet$objphpexcel->getactivesheet ()->addchart ($chart 1);/////////////////////////////////////////// Set Active sheet Index to the first sheet, so Excel opens this as the fi RST sheet$objphpexcel->setactivesheetindex (0); $filename = ' Summary table _ '. Date ("Y_m_d"). ". Xlsx ";//Redirect output to a client ' s Web browser (Excel2007) header (' content-type:application/ Vnd.openxmlformats-officedocument.spreadsheetml.sheet ');//header (' Content-disposition:attachment;filename= "'. $ filename. ' "'); devrent.xlsx//////////////////////////////////////////processing Chinese file name garbled problem $ua = $_server["Http_user_agent"]; $encoded _filename = UrlEncode ($filename), $encoded _filename = str_replace ("+", "%20", $encoded _filename); Header (' Content-type:application/octet-stream '), if (Preg_match ("/msie/", $ua)) {header (' content-disposition:attachment; Filename= "'. $encoded _filename. '"');} else if (Preg_match ("/firefox/", $ua)) {header (' content-disposition:attachment; filename*= "UTF8\ ' \ '. $filename. '"');} else {header (' content-disposition:attachment; Filename= '. $filename. '"');} Header (' cache-control:max-age=0 ');//If you ' re serving to IE 9 and then the Following May Neededheader (' cache-control:max-age=1 ');/If you ' re serving to IE over SSL and then the following May n Eededheader (' Expires:mon, Jul 1997 05:00:00 GMT '); Date in the Pastheader (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT '); Always Modifiedheader (' Cache-control:cache, Must-revalidate '); Http/1.1header (' pragma:public '); Http/1.0$objwriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); $objWriter Setincludecharts (TRUE); $objWriter->save (' Php://output '); exit;Another point to note is that the Excel pie chart, by specifying its label, the value of the corresponding cell range, is automatically generated, so the main is in the code to calculate well. On another non-Windows server, the build will fail.
Mail:xcl_168@aliyun.com
blog:http:/./blog.csdn.ent/xcl168
http://www.bkjia.com/PHPjc/766057.html www.bkjia.com true http://www.bkjia.com/PHPjc/766057.html techarticle Phpexcel processing Excel file is really a powerful tool, I have a report, with pie chart, need to turn to Excel, Phpexcel has a relevant example, reference and modified after the implementation of this effect, can ...