How to Create a Complete Excel Spreadsheet From MySQL

This tutorial post will teach you about how to create an excel spreadsheet from MySQL. Thanks to MySQL excel or MySQL to excel that render it so easy and affable which was seem to confounded earlier. Moreover, you can export MySQL to excel as well employ MySQL import data in very easy and simple method.

If you are thinking about rendering your MySQL data to an Excel spreadsheet, then a CSV stands for Comma Separated Value would be sufficient towards this endeavor. An excel spreadsheet incorporates formulas, formatting and even graphics, on the other hand the CSV files are considered as data only.

However, in this coming section of this tutorial post you will learn to employ open source PHP components for generating a “real” Excel spreadsheet through MySQL SELECT statements. So, let’s go ahead and take a look over this-

Overview

PHP Excel is referred as a set of PHP classes which enables you to read and write distinct spreadsheet formats and also employ the spreadsheet themselves. However, prior to initiate, you will required PHP Excel copy.

The Fundamental Relationship Between Data Tables and Spreadsheets

A SQL SELECT statement delivers a set of results which incorporates labeled columns and rows with the data. However, in PHP/MySQL, each rows accompanied by result set most often revealed by an associative array which is also called as ‘column name‘.An Excel spreadsheet is defined as a series of worksheets which is accompanied by alphabetically labeled columns and numbered rows.

Therefore, the common technique implemented will be to map one SQL statement to one Excel worksheets/spreadsheet page, particularly by matching the name column with associative array delivered by POD retrieve command to alphabetical spreadsheet’s column labels.

Step 1: Comprehending How to Label Alphabetical Columns Accurately

The primary key behind this technique is the fundamental algorithm of column labeling, that will enable you to label alphabetical columns accurately regarding large quantity of columns arbitrarily. As you already know that in an Excel spreadsheet, rows are represented with numbers as well as columns with letters. However, if your query is seems to be comprise less than 26 columns, you can simply stop at “Column Z”. Regardless of this various queries incorporates substantially more columns.

Scheme of Column Labeling in Excel

Excel Spreadsheet from MySQL1
Excel Spreadsheet from MySQL2

The Column Labeling Algorithm

Here, let take a look over this PHP fragment that will say how to label column headers from “A” to “ZZ”.

The point should be noted that in account of implementing this alogrithm you can’t cross “ZZ”. However, this algorithm is has implemented in a marked distinct way in the accompanying source file in MySqlExcelBuilder::mapColumns member function.

Step 2: Format and Test Your SQL Statement

A Simple Schema

The below provided schema displays a simplified form of customer/order relationship. The SQL CREATE statement concerned to below given schema are added in the xls_sample.sql file in form of zip file which is incorporates this tutorial.

Excel Spreadsheet from MySQL3

A “Quick & Dirty” SELECT Statement

This is considered as the most simple practice in context of congregating data exponentially form these tables in respect of SQL statements and will be appear something like this:

However, the most significant features of this “quick & dirty” statement is considered as it’s rapid and affable to program. Though the results appears generally don’t have a good look.

PHPMyAdmin is a quite obvious tool for MySQL database administration which flaunts with various hosting plans. However, you are free to employ SQL composition tools in context of prototype and analyze your excel spreadsheet pages which is constituted by PHPMyAdmin.

The snapshot sits below resembles the result for the above query:

Excel Spreadsheet from MySQL4

At this place, I will show you a close up view of some columns names which seems to valuable for programmers but not effective to business user.

Excel Spreadsheet from MySQL5

Here, you may notice that the employed column names are not implemented to “real word” rules as well as instead of space ‘underscores’ has been used.

With SELECT Statement Get More Attractive Results

Now, we are required to format the SELECT statement in order to convey a business report view to a spreadsheet. So, by employing the PHPMyAdmin tool, we are going to edit SQL statement that eventually results in column names to appear in real words as well as only those columns will be displayed the users wish to see. Take a look over this reformatted SQL statement which appears like:

and results as:

Excel Spreadsheet from MySQL6

Therefore, the above prototype demonstrates an identical look of an Excel spreadsheet page.

Step 3: Displaying A MySQL Result Set on A Spreadsheet Page

The class MySqlExcelBuilder abbreviates the functionality necessary to include SQL statements to a spreadsheet page by means of employing PHPExcel and PDO. The full class is available in constituted zip file.

The MySqlExcelBuilder Class

This class provide a lot of SQL result sets to be assigned over named pages in an entire Excel spreadsheet. Therefore, the below sitting snipped code will shows the significant data members.

The $pdo data member is referred as PHP Data Object employed to query database.
The $phpExcel data member is the PHPExcel object which is useful to build as well manipulate an Excel spreadsheet.
The $sql_pagesarray comprises the SQL statement and the page naming/formating information.
The constructor which is hidden used to initializes the PHPExcel and PDO data members.

Preparing Each Page

The below provided image of a spreadsheet is a prototype built in Excel in order to show the desired look of a spreadsheet after being rendered.

Excel Spreadsheet from MySQL7

The add_page member function is employed to include SQL Statements to the specific pages.

Hence, the information we wish to employ, in order to include SQL on pages is retained by the sql_pages.

Example of Member Function Usage

The following snippet of code is an excellent example of employing add_page member function.

However, the below listed illustration displays how add_page member function should define to the spreadsheet.

Excel Spreadsheet from MySQL8

Step 4: Building the Spreadsheet

Comprehending PHPExcel

So, while you comprehend accurately about manipulating a spreadsheet with your keyboard and mouse, you will become adept at accessing PHPExcel. PHPExcel is created on the principle of manipulating the fundamental excel spreadsheet model employing those command which are identical to command you used to implement in Excel. However, The PHPExcel Developer Documentation comprises the details.

The getExcel() Member Function

The getExcel() member functions employ PHPExcel towards creating each of the worksheets from SQL statements which have discussed above in Step 3. After accomplishment of worksheets, it used to return the PHPExcel object to caller. So, have a look over following four primary sections of getExcel member function.

A. Iterate Through the Pages

The primary loop of this member function persist through those pages which were added earlier with add_page. However, in each iteration, it used to generate a concerning page in PHPExcel object and later add the data. The PHPExcel’s createSheet member function is obvious towards building a new worksheets for each those pages has been added earlier.

The below illustration says about how the code corresponds to an Excel spreadsheet. The column_map will be discussed later in the coming next section of this post.

Excel Spreadsheet from MySQL9

B. “Start of Page” Logic

Each page comprises a specific formatting at the beginning. When a row is fetched from the database for a specific page for the first time, it accomplish the necessary tasks to perform the formatting of start-of-page. However, it implore “mapColumns” member function which has been discussed above in Step 1. Likewise Excel, PHPExcel uses a Letternumber pair to define a specific cell. In MySqlExcelBuilder it is considered as a CellKey which is created by concatenating a row number and a column label.

Here I am listing some additional things with reference to the above snippet code that you must know about:

  • setCellValue: It assign an actual value in the field. Always note that a cell is referred as a member of a worksheet and represented by the cellKey variable.
  • GetStyle: It is obvious to delivers a reference to the style attribute to a specific cell, therefore it is affable to manipulate.
  • GetColumnDimension: It is considered as a method of worksheet object. col variable is associated with a column dimension (width).

C. Fill in the Data

Almost you done column mapping and preparation, however, the method of placing every data item in a cell is now relatively flimsy. Now you are supposed to look up an Excel spreadsheet column regarding a specific data column, create a cell key and next assign the value to this cell.

D. Add in the Formulas

The end part of getExcel () says how to add formulas to a spreadsheet of a PHPExcel. In this case, it is referred as column total. Likewise, in an Excel spreadsheet you put formulas into a specific cell, PHPExcel also do the same. The value prefixed by equal sign (=) in a cell is a formula. Here, see below to designate the SUM of a range of data cells.

Excel Spreadsheet from MySQL10

And the code is:

Step 5: Employ the Finishing Touches

After retrieving the data from your MySQL database to an Excel spreadsheet in account of getExcel, now you are supposed to convey some finishing touches on your spreadsheet. Here, in this illustration we are going to include a title to each and every worksheet.

Step 6: Save The File

PHPEXcel employ an object factory in context of building a writer which used to write the spreadsheet you have created in the appropriate format. Here, I have employed “Excel15” because it is capable to read even very earlier spreadsheet programs.

Final Product

The below assigned image states about a partial snapshot of a screen where final product has been created in account of included sample code. It comprises two spreadsheet pages, congregated from a sample database and formatted by employing custom titles on every page:

Excel Spreadsheet from MySQL11

However, when your data from the database has been retrieved in PHPExcel object, you can implement other arbitrarily features of PHPExcel class to accomplish additional formatting a well as assign more formulas and save this in distinct file formats as well as anything whatever PHPExcel allows.

David Meyer

As the most experienced developer of CSSChopper - PSD to HTML Conversion Company, David Meyer firmly believes in building the new ways that lead the people towards success. He focuses on an ideal approach and tries to deliver the perfect services close to the defined needs.