ComputerUser.com

Read and write Excel data with PHP Using XML support Goodyear AZ

The advance of Microsoft Office 2003 was the addition of XML file formats. With Office 2003, you can save your Microsoft Excel spreadsheet as XML and use the file just as you would the binary equivalent. The same goes for Microsoft Word.

Embry-Riddle Aeronautical University
(623) 935-4000
7017 N. Litchfield Road 401
Glendale, AZ
Peoria/West Valley Women's Club
623979-6866
10510 N. 83rd Ave
Peoria, AZ
Louis the King School
(623) 939-4260
4331 W. Maryland
Peoria, AZ
Glendale Union High School District
(623) 435-6000
7650 N. 43rd Avenue
Glendale, AZ
Arizona Automotive Institute
(623) 934-7273
6829 N. 46th Avenue
Glendale, AZ
Copper Canyon & Desert
(623) 930-1734
7785 W. Peoria
Peoria, AZ
West-Mec
(623) 873-1860
4949 W. Indian School
Peoria, AZ
Glendale Elementary Schools
(623) 842-8100
7301 N. 58th Avenue
Glendale, AZ
Our Lady of Perpetual Help Church/School
(623) 931-7288
7521 N. 57th Avenue
Glendale, AZ
Montessori Kingdom of
(623) 876-1463
13111 N. 94th
Peoria, AZ
Provided By:

Read and write Excel data with PHP Using XML support

Written by Jack Herrington   

Microsoft Office 2003 for the Microsoft Windows® operating system opened a whole new set of opportunities that non-Microsoft engineers have yet to realize. Of course, you had the usual set of new features. But the big new advance was the addition of XML file formats. With Office 2003, you can save your Microsoft Excel spreadsheet as XML and use the file just as you would the binary equivalent. The same goes for Microsoft Word.

Why are XML file formats so important? Because for years, the true power of Excel or Word was locked in binary file formats that required elaborate converters to access. Now, you can read or write Excel or Word files using XML tools like Extensible Stylesheet Language Transformation (XSLT) or the XML Document Object Model (DOM) functions built into the PHP programming language.

In this article, I show how to build a PHP Web application that uses these formats to read data into a database from an Excel spreadsheet and to export the contents of a database table to an Excel spreadsheet.

Create the database

For this article, I use a simple Web application so you can clearly see the Excel XML mechanism. This application is a table of names and e-mail addresses.

The schema in MySQL syntax looks like the code in Listing 1.

Listing 1. SQL for the database

DROP TABLE IF EXISTS names;
CREATE TABLE names (
id INT NOT NULL AUTO_INCREMENT,
first TEXT,
middle TEXT,
last TEXT,
email TEXT,
PRIMARY KEY( id )
);
This file is a single-table database in which the table -- names -- has five fields: an auto-incrementing ID field, followed by first, middle, and last name fields, and an e-mail field.

To set up the database, create the database using the Mysqladmin command-line tool: mysqladmin --user=root create names. You then load the database from the schema file: mysql --user=root names < schema.sql. The user and password authentication you use varies depending on your installation, but the idea remains the same. First, create the database. Then use the SQL file to create the tables with the required fields.

Create the import data

The next step is to create some data for import. Create a new Excel file. In the first workbook, call the top row of columns First, Middle, Last, and Email. Then, add a few rows of data to the list (see Figure 1).

Figure 1. Data for import

 Data for import

You can make the list as long as you like or change the fields however you see fit. The PHP import script in this article ignores the first line of data unconditionally, because it assumes that it's the header line. In a production application, you would probably want to read and parse the header line to determine which fields are in which columns and make the appropriate changes to your import logic.

The last step is to save the file as XML by clicking File > Save As and then, in the Save As window, selecting XML Spreadsheet from t...

Click here to read the rest of this article from Computer User