Use PHPExcel to export the column chart in the YII2 framework.

Source: Internet
Author: User

Use PHPExcel to export the column chart in the YII2 framework.

Export result:

 

 

First, download the PHPExcel plug-in package from the official website. The downloaded folder is as follows:

Put the Classes folder into the public method of the project.

 

Create a controller (access export method): EntryandexitController

<? Php
Namespace app \ modules \ reportmanagement \ controllers;

Use Yii;
Use yii \ web \ Controller;
Use app \ modules \ reportmanagement \ components \ Summaryexport;

Class EntryandexitController extends BaseController
{
Public function actionIndex (){

// Query the data to be exported
$ Result = array ();

// Call the export Method
Summaryexport: export ($ result );
}
}

 

Create a controller (public method): Summaryexport


<? Php
Namespace app \ modules \ reportmanagement \ components;
// Introduce the PHPExcel plug-in
Require dirname (_ FILE _). '/components/phpexcel/PHPExcel. php ';

Class Summaryexport
{
Public static function export ($ result ){

Header ("content-type: text/html; charset = UTF-8 ");
Error_reporting (E_ALL );
Date_default_timezone_set ('Europe/London ');

// Create a sheet
$ ObjPHPExcel = new \ PHPExcel ();
$ ObjWorksheet = $ objPHPExcel-> setActiveSheetIndex (0 );

// $ Result encapsulation (select one method)-> This method can be used to output a table or assign values to a cell (table data)
// Method 1
$ ObjWorksheet-> fromArray (
Array (
Array ('hangzhou', 'e people ', 'W people', 'total number', 'Growth rate', 'e merchant ', 'W merchant', 'total merchant number ', 'group', 'average '),
Array ('123', '10', '12', '= SUM (B2: B3)', '= SUM (B2: B3)/B3 )', '10', '2', '12', '12', '12 '),
Array ('123', '10', '12', '= SUM (B2: B3)', '= SUM (B2: B3)/B3 )', '10', '2', '12', '12', '12 '),
...
)
);
// Method 2
// Title
$ Title_name = "monthly summary of entry/exit count report ";
$ ObjPHPExcel-> getActiveSheet ()-> mergeCells ('a1: j1 ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('a1', $ title_name );

$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('a2 ', 'hangzhou ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('b2', 'e count ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('c2 ', 'W students ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('d2 ', 'subtotal ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('E2', 'Growth rate ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('F2', 'e merchant ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('g2 ', 'W merchant ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('h2 ', 'subtotal ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('i2 ', 'group ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ('j2 ', 'General ');

For ($ I = 1; $ I <= 12; $ I ++ ){

$ Index = 'A'. ($ I + 2 );
$ Index_ B = 'B'. ($ I + 2 );
$ Index_c = 'C'. ($ I + 2 );
$ Index_d = 'D'. ($ I + 2 );
$ Index_e = 'E'. ($ I + 2 );
$ Index_f = 'F'. ($ I + 2 );
$ Index_g = 'G'. ($ I + 2 );
$ Index_h = 'H'. ($ I + 2 );
$ Index_ I = 'I'. ($ I + 2 );
$ Index_j = 'J'. ($ I + 2 );

$ Prev_index_d = 'D'. ($ I + 1 );
$ Growth_rate = $ I = 1? '-': '= SUM ('. $ index_d. '-'. $ prev_index_d. ')/'. $ prev_index_d .')';

$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index, 'August 1 ');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_ B, 12 );
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_c, 10 );
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_d, '= SUM ('. $ index_ B. ':'. $ index_c .')');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_e, $ growth_rate );
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_f, 10 );
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_g, 20 );
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_h, '= SUM ('. $ index_f. ':'. $ index_g .')');
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_ I, 10 );
$ ObjPHPExcel-> getActiveSheet ()-> SetCellValue ($ index_j, 10)
}
// Start chart
// Obtain the label for drawing the chart (LEGEND)
$ DataseriesLabels = array (
New \ PHPExcel_Chart_DataSeriesValues ('string', 'worksheet! $ D $2 ', NULL, 1), // 'total number'
New \ PHPExcel_Chart_DataSeriesValues ('string', 'worksheet! $ H $2 ', NULL, 1), // 'Merchant'
New \ PHPExcel_Chart_DataSeriesValues ('string', 'worksheet! $ I $2 ', NULL, 1), // 'group'
New \ PHPExcel_Chart_DataSeriesValues ('string', 'worksheet! $ J $2 ', NULL, 1), // 'average passenger'
);
// Obtain the scale of the X axis of the chart (fourth parameter value: cell length A3-A14)
$ XAxisTickValues = array (
New \ PHPExcel_Chart_DataSeriesValues ('string', 'worksheet! $ A $3: $ A $ 14', NULL, 12), // March to April
);
// Obtain the data required for drawing
$ DataSeriesValues = array (
New \ PHPExcel_Chart_DataSeriesValues ('number', 'worksheet! $ D $3: $ D $ 14', NULL, 12 ),
New \ PHPExcel_Chart_DataSeriesValues ('number', 'worksheet! $ H $3: $ H $ 14', NULL, 12 ),
New \ PHPExcel_Chart_DataSeriesValues ('number', 'worksheet! $ I $3: $ I $ 14', NULL, 12 ),
New \ PHPExcel_Chart_DataSeriesValues ('number', 'worksheet! $ J $3: $ J $ 14', NULL, 12 ),
);
// Create a chart framework based on the obtained items
$ Series = new \ PHPExcel_Chart_DataSeries (
\ PHPExcel_Chart_DataSeries: TYPE_SURFACECHART, // plotType
\ PHPExcel_Chart_DataSeries: GROUPING_CLUSTERED, // plotGrouping
Range (0, count ($ dataSeriesValues)-1), // plotOrder
$ DataseriesLabels, // plotLabel
$ XAxisTickValues, // plotCategory
$ DataSeriesValues // plotValues
);
// Set the parameters required to generate the chart (the coordinates are displayed horizontally or vertically: DIRECTION_COL | DIRECTION_BAR)
$ Series-> setPlotDirection (\ PHPExcel_Chart_DataSeries: DIRECTION_COL );

$ Layout = new \ PHPExcel_Chart_Layout ();
$ Layout-> setShowPercent (TRUE );
$ Plotarea = new \ PHPExcel_Chart_PlotArea ($ layout, array ($ series ));
$ Legend = new \ PHPExcel_Chart_Legend (\ PHPExcel_Chart_Legend: POSITION_RIGHT, NULL, false );

$ Title = new \ PHPExcel_Chart_Title ('comparison of votes purchased by various channels in October 1 ');
// $ XAxisLabel = new \ PHPExcel_Chart_Title ('financial Period '); // the title of the X axis
$ YAxisLabel = new \ PHPExcel_Chart_Title ('number of people ($ k) '); // y axis title

$ Chart = new \ PHPExcel_Chart (
'Chart1', // name
$ Title, // title, null
$ Legend, // legend
$ Plotarea, // plotArea
True, // plotVisibleOnly
0, // displayBlanksAs
Null, // xAxisLabel
$ YAxisLabel // yAxisLabel
);
// Specify the position of the chart in the table.
$ Chart-> setTopLeftPosition ('l2 ');
$ Chart-> setBottomRightPosition ('t15 ');
// Add the chart to the sheet
$ ObjWorksheet-> addChart ($ chart );
// -------------- Chart end;

$ ObjPHPExcel-> getActiveSheet ()-> setTitle ("total year ");

$ Filename = ' .xls ';

$ ObjWriter = \ PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 ');
$ ObjWriter-> setIncludeCharts (TRUE); // The chart must be

Header ("Pragma: public ");
Header ("Expires: 0 ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Content-Type: application/force-download ");
Header ("Content-Type: application/vnd. ms-execl ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ("Content-Disposition: attachment; filename =". $ filename );
Header ("Content-Transfer-Encoding: binary ");
$ ObjWriter-> save ("php: // output ");

}

Table data and chart data are not associated.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.