Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    My projects

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Reuse Excel business logic with PHPExcel

    Price calculationIn many companies, business logic resides in Excel. This business logic is sometimes created by business analysts and sometimes by business users who want to automate parts of their everyday job using Excel. This same Excel-based business logic is often copied into an application (i.e. a website) and is maintained on 2 places: if the Excel logic changes, the application should also be modified. Did you know you can use PHPExcel to take advantage of the Excel-based business logic without having to worry about duplicate business logic?

    Here's a scenario: You are working in a company which sells "dream cars". For every model, the company has created an Excel spreadsheet which is used to calculate the car's price based on customer preferences. These spreadsheets are updated frequently in order to reflect the car manufacturer's pricing schemes.

    Your manager asks you to create a small website which accepts some input fields (Does the customer want automatic transmission? What colour should the car be painted? Does the customer want leather seats? Does the customer want sports suspension?). Based on these questions, the car's price should be calculated. Make sure all prices on the website are in sync with this Excel sheet!

    Download example source code
    View live example

    1. Create the Excel sheet containing business logic

    Defined names First of all, we'll create an Excel sheet containing business logic. If you're lazy, download my example here. To make things easy for yourself when scripting, make sure you add some defined names on each field you want to use as input/output. Of course it's possible to work with the sheet's cell references later on, but if you want to be able to change the location of cells within the worksheet later, these defined names are much easier!

    2. Download the latest PHPExcel version

    You can find PHPExcel on www.phpexcel.net. If you want a stable version, download an official release. The source code tab on CodePlex reveals the latest Subversion source code if you want to use it.

    3. Create the web based front-end

    Next thing we'll do is creating a simple webpage containing an HTML form which corresponds woth the parameters you want to pass to the Excel sheet.

    4. Let's do some PHP coding!

    Since you came here for the good stuff, here it is! What we'll do is load the Excel sheet, pass in the parameters and use some calculated values on our resultign page. First things first: include the necessary class references:

    /** Include path **/
    set_include_path(get_include_path() . PATH_SEPARATOR . './PHPExcel/Classes/');

    /** Class requirements */
    require_once('PHPExcel.php');
    require_once('PHPExcel/Reader/Excel2007.php');

    PHPExcel is the base library which represents an in-memory spreadsheet. Since we need to interface with an Excel2007 file, we also include the required reader class.

    Now load the Excel sheet into a PHPExcel object:

    // Load price calculation spreadsheet
    $objReader = new PHPExcel_Reader_Excel2007();
    $objPHPExcel = $objReader->load("price_calculation.xlsx");

    All data from the web form is passed in via the $_REQUEST array. Let's pass these to the Excel sheet. I named all form fields equal to my defined names in Excel which makes coincidence of all array keys and cell names being the same intentional.

    // Set active sheet
    $objPHPExcel->setActiveSheetIndex(0);

    // Assign data
    $objPHPExcel->getActiveSheet()->setCellValue('automaticTransmission', $_REQUEST['automaticTransmission']);
    $objPHPExcel->getActiveSheet()->setCellValue('carColor', $_REQUEST['carColor']);
    $objPHPExcel->getActiveSheet()->setCellValue('leatherSeats', $_REQUEST['leatherSeats']);
    $objPHPExcel->getActiveSheet()->setCellValue('sportsSeats', $_REQUEST['sportsSeats']);

    PHPExcel is great success!This is actually about it. The only thing left is to fetch the formula's calculated values and we're done!

    // Perform calculations
    $_VIEWDATA['totalPrice'] = $objPHPExcel->getActiveSheet()->getCell('totalPrice')->getCalculatedValue();
    $_VIEWDATA['discount'] = $objPHPExcel->getActiveSheet()->getCell('discount')->getCalculatedValue();
    $_VIEWDATA['grandTotal'] = $objPHPExcel->getActiveSheet()->getCell('grandTotal')->getCalculatedValue();

    You can use these values to print the result on your web page:

    Based on your chosen preferences, your car will cost <?php echo number_format($_VIEWDATA['grandTotal'], 2); ?> EUR.

    5. Summary

    Embedding business logic in Excel and re-using it in PHP is not that hard. The PHPExcel library helps you simplify development: your application logic and business logic is separated. Business logic can be maintained by a business expert or key user in Excel. As an application developer, you can easily pass data in the sheet and make use of PHPExcel's calculation engine.

    kick it on DotNetKicks.com


    Categories: General | OpenXML | PHP | XML

    Comments

    blogs.msdn.com | Reply

    Thursday, April 17, 2008 5:03 PM

    pingback

    Pingback from blogs.msdn.com

    Doug Mahugh : Open XML links for 04-17-2008

    developercast.com | Reply

    Tuesday, May 06, 2008 2:58 PM

    pingback

    Pingback from developercast.com

    Maarten Balliauw’s Blog: Reuse Excel business logic with PHPExcel | Development Blog With Code Updates : Developercast.com

    zoeandgavin.com | Reply

    Sunday, May 11, 2008 8:20 AM

    pingback

    Pingback from zoeandgavin.com

    Web Developers vs. Business People | Gavin on PHP

    Arik Israel | Reply

    Sunday, May 11, 2008 5:45 PM

    Arik

    Wouldn't it be hard on performance if for every calculation you will have to do it via the Excel file? I think that better option would be to parse to logic out of the Excel formula and save it as PHP code (and apply some caching to this formula so it won't be needed to read every time).

    Maarten Belgium | Reply

    Tuesday, May 13, 2008 8:11 AM

    Maarten

    That's actually what's happening under the hood. Each formula is parsed into corrwsponding PHP code and executed.

    ck862 People's Republic of China | Reply

    Tuesday, May 20, 2008 10:12 AM

    ck862

    Well Done! Thank u MaartenBalliauw

    blogs.vinuthomas.com | Reply

    Saturday, June 07, 2008 6:36 AM

    pingback

    Pingback from blogs.vinuthomas.com

    Power your PHP Business Logic with Excel | VT's Tech Blog

    sireesh.blog.co.in | Reply

    Thursday, June 19, 2008 1:52 PM

    pingback

    Pingback from sireesh.blog.co.in

    Power your PHP Business Logic with Excel

    alexey_baranov Russia | Reply

    Monday, August 25, 2008 12:17 PM

    alexey_baranov

    Good!

    RealDolmen blogs | Reply

    Wednesday, December 31, 2008 3:43 PM

    trackback

    Trackback from RealDolmen blogs

    Top blog posts in 2008

    Clarkson Mexico | Reply

    Tuesday, January 06, 2009 10:40 PM

    Clarkson

    Great post, but what's with the wierd photo of Borat ?

    serdarb Turkey | Reply

    Wednesday, January 07, 2009 3:31 PM

    serdarb

    very nice post...

    sheshagirig India | Reply

    Friday, June 05, 2009 10:08 AM

    sheshagirig

    Will it take care when 40 people use the form simultaneously?

    Ilyas Kazi India | Reply

    Friday, October 09, 2009 8:00 AM

    Ilyas Kazi

    @sheshagirig

    Your question is interesting... Well I wonder how it will handle in case of 2 people from different region opening the same excel which is on the server needs to be opened and updated at the same time...

    jaceju.net | Reply

    Wednesday, November 25, 2009 3:41 AM

    pingback

    Pingback from jaceju.net

    網站製作學習誌 » [Web] 連結分享

    topsy.com | Reply

    Wednesday, June 16, 2010 12:09 PM

    pingback

    Pingback from topsy.com

    Twitter Trackbacks for
            
            Reuse Excel business logic with PHPExcel
            [maartenballiauw.be]
            on Topsy.com

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading