Вы находитесь на странице: 1из 3

am i works?

all about my work


Web www.amiworks.co.in Google Search

Home

Reading/Parsing Excel Spreadsheet using PHP


June 15th, 2008 | Save to del.icio.us now(38) This is second article in the series of common PHP problems that a beginner faces. You can check the first article Warning: header data already sent. In this tutorial we will see, how we can easily parse or read an Excel sheet and display the parsed data to user. Even though we are going to display the output, the other extension of this technique is that we can parse an excel sheet filled with data, in set format, and convert it into SQL statement and insert the data into database. Now this is an easy task on an windows based server, to do the same thing in Linux we need to use some other libraries. This tutorial assumes that you know PHP. So lets get into the details, PHP-ExcelReader is used for parsing the excel sheet.

First of all download the PHP-ExcelReader from SourceForge, if you PEAR configured properly or you can download the files E c l R a e from which I have removed the xe edr PEAR dependency (note: this is a bad practice, I only use this in my DEV environment for simple parsing and testing, please do not use this file in production environment).
rqieoc 'edrpp; eur_ne rae.h'

I just included the class file. Now we will use following function for parsing the file, This function basically returns the data parsed from the excel sheet in an associative array. Off-course you have to remember that it is just a quick function to get the job done in DEV environment and you should not use it in production environment, without suitable modifications.
fnto prexe(eclfl_aewt_ah ucin asEcl$xe_ienm_ihpt) { $aa=nwSrasetEclRae(; dt e pedhe_xe_edr) $aa>eOtuEcdn(C15'; dt-stuptnoig'P21) $aa>ed$xe_ienm_ihpt) dt-ra(eclfl_aewt_ah;

$onm=ra(NM''K''RDGOP,WRHUECD''AE,LNT''IT''OO''EGTL''AKGN''OT,RTI''HLSL' claeary'AE,SU,PO RU''AEOS OE,DT''EGH,WDH,CLR,WIH B,PCAIG,CS''EAL,WOEAE $trlgn=as; satoigfle $=; k0 fr(i=1 $ < $aa>hes0[nmos] $+){ o $ ; i = dt-set[]'uRw'; i+ fr(j=1 $ < $aa>hes0[nmos] $+){ o $ ; j = dt-set[]'uCl'; j+ i(dt-set[]'el'[i[j='AE) f$aa>hes0[cls]$]$]=NM' { $trlgn=re satoigtu; bek ra; } i(satoig f$trlgn) { $rdc[k1[j1=dt-set[]'el'[i[j; pout$-]$-]$aa>hes0[cls]$]$] $rdc[k1[clae$-]=dt-set[]'el'[i[j; pout$-]$onm[j1]$aa>hes0[cls]$]$]

} } i(satoig f$trlgn) { $=k1 k$+; } } rtr $rdc; eun pout }

In first line we initialize the Spreadsheet_Excel_Reader, then we set the output encoding. Finally we read the Excel file using read method of Spreadsheet_Excel_Reader class. This basically completes the process of reading the excel spreadsheet. Simple isnt it? Rest of the logic is basically parsing the output array of spreadsheet class, to more user friendly and easy to understand array. The first FOR loop is counting the rows in spreadsheet, while second FOR loop is counting the number of columns in each row.
i(dt-set[]'el'[i[j='AE) f$aa>hes0[cls]$]$]=NM'

This condition is used to find the right row and column while parsing the data array. This ensures that we only return the relevant data from the excel sheet and not everything. In your case replace NAME with the name of the first column in your spreadsheet.

Are you satisfied with your knowledge? No, then spent 15 minutes every day on PHPCamp.net a knowledge sharing website for our own PHP community
Once we figure out the correct row( from where we need to pick the data) we save it in both the numeric array and also as associative array. Here is a variation of this function, where our excel sheet does not have any column name mentioned.

fnto prexe(eclfl_aewt_ah ucin asEcl$xe_ienm_ihpt)

{ $aa=nwSrasetEclRae(; dt e pedhe_xe_edr) / Stotu Ecdn. / e upt noig $aa>eOtuEcdn(C15'; dt-stuptnoig'P21) $aa>ed$xe_ienm_ihpt) dt-ra(eclfl_aewt_ah; $onm=ra(i''ae) claeary'd,nm'; fr(i=1 $ < $aa>hes0[nmos] $+){ o $ ; i = dt-set[]'uRw'; i+ fr(j=1 $ < $aa>hes0[nmos] $+){ o $ ; j = dt-set[]'uCl'; j+ $rdc[i1[j1=dt-set[]'el'[i[j; pout$-]$-]$aa>hes0[cls]$]$] $rdc[i1[clae$-]=dt-set[]'el'[i[j; pout$-]$onm[j1]$aa>hes0[cls]$]$] } } rtr $rdc; eun pout }

If you know some other alternatives please share.

Related posts
1. 2. 3. 4. 5. akWpUploader: Alternative WordPress Image Uploader PHP Sucks ADS: Automatic Deployment Script How to get Latitude/Longitude from an address (or Geocoding ) using PHP Enable Your Joomla To Write In Hindi, Marathi, Bengoli, Gujarati, Malyalam, Telugu and Punjabi

Wondering what to do next?

7 Grab my feed

Catch Me On Twitter

Filed under php | Comments (70) Search

(Search web development related contents)

70 Responses to Reading/Parsing Excel Spreadsheet using PHP 1. June 16, 2008 5:15 pm Binny V A says My general advice to client is to use the CSV format instead of Excel files for import/export purposes. CSV has native PHP support. But some clients insist on using Excel in those cases, this will come in handy. 2. June 16, 2008 11:15 pm roScripts - Webmaster resources and websites says Reading/Parsing Excel Spreadsheet using PHP This is second article in the series of common PHP problems that a beginner faces You can check the first article Warning header data already sent In this 3. July 9, 2008 10:22 am PHP : Dead Easy Excel Export | Edmonds Commerce Blog says [...] to Excel by configuring the MIME Type Profile Option Power your PHP Business Logic with Excel Reading/Parsing Excel Spreadsheet using PHP Bookmark this [...]

4. July 10, 2008 2:28 pm Jasonwoon says

Вам также может понравиться