In this tutorial we convert a csv file containing sales information into a excel sheet. The excel sheet will also contain a pivot table and a pie chart with a summary about the sales data.

The input file (*.csv)

The input file is a simple csv file with a header row and the sales data. Please note the header columns as they are used to generate the pivot table and the chart.

Download data file

The PHP code

The PHP code sends the data file and an options file to the PivotableHandler using the library httpfull. Download the file httpfull.phar and include it in the php script.

Then there is an array containing the option for the pivot table and the chart. In this example we use the columns "Product" and "Store" as row fields. "Customer" and "Turnover" are the 'pivoted' fields with additional options "format" and "pivot_function". Please take a look at the documentation for all available options. The options are saved as json file.

Next the script sends the options and a data file to the webservice. As result it will return either the generated exel file are an error message. Using the http status code the script checks if the request was successfull (status = 200). Then the result is stored as excel file. In case of an error the error message is in $result->body.

Download source code

The result file (*.xlsx)

The result of the webservice call is an excel file containing the data as first worksheet. There is also an sheet with the pivot table to show aggregated sales numbers per "Store" and "Product" and a pie chart to visualize the data.

If the pivot table is not visible in the download file please click "Enable Editing".

Download result file