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
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
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 the Save as type drop-down list (see Figure 2).
Figure 2. Save the file as an XML spreadsheet
With the XML file in hand, you can begin to develop your PHP application.
Import the data
The import system starts easily enough with a page in which you specify the input Excel XML file (see Figure 3).
Figure 3. Specify the input Excel XML file
The page logic is simple, as shown in Listing 2:
Listing 2. The upload page code
I’ve named the file with a .php extension, but it’s really not PHP at all. It’s just an HTML file that allows the user to specify a file and submits that file to the import.php page, which is where the real fun occurs.
Read the Excel XML data
To make it a little easier to follow, I’ve written the import.php page in two phases. In the first phase, I simply parse the XML data and output it as a table. In the second phase, I add the logic that inserts the records into the database.
Listing 3 shows an example Excel 2003 XML file.
Listing 3. Sample Excel XML file
I’ve chopped out a couple of rows in the middle, but otherwise, the file is verbatim what comes out of Excel. It’s relatively clean XML. Note the document header portion at the beginning that describes the document and who is writing it, lays down some visual information, lists styles, an so on. Then, the data comes as a set of worksheets within the main Workbook object.
The first Worksheet object contains the real data. Within that object, the data resides inside the Table tag in a set of Row and Cell tags. Each Cell tag has a Data tag associated with it that holds the data for the cell. In this case, the data is always formatted as String type.
By default, when you create a new document, Excel creates three worksheets named Sheet1, Sheet2, and Sheet3. I didn’t delete the second and third worksheets, so you see these empty workbooks at the end of the document.
Listing 4 shows the first version of the import.php script.
Listing 4. The first version of the import script
The script starts by reading in the uploaded temporary file into a DOMDocument object. Then the script finds each Row tag. The first row is ignored using the logic associated with the $first_row variable. After the first row, an inside loop parses each Cell tag within the row.
The next tricky bit is to figure out which column you’re in. As you can see in the XML, the Cell tag doesn’t specify the row or column number. The script needs to keep track of that itself. Actually, it’s a bit more complicated than that, even. In fact, the Cell tag has an ss:Index attribute that tells you what column the cell is on if there are blank columns in this row. That’s what the getAttribute(‘index’) code is looking for.
After determining the index, the code is simple. Place the cell value into a local value associated with that field. Then, at the end of the row, call the add_person function to add the person to the data set.
At the end of the page, the PHP outputs the data that was found into an HTML table using familiar PHP mechanisms (see Figure 4).
Figure 4. Data output into an HTML table
The next step is to load this data into the database.
Add the data to the database
After the script has the row data in a PHP data structure, it needs to add that data to the database. To do that, I’ve added some code that uses the Pear DB module (see Listing 5).
Listing 5. The second version of the import script
Figure 5 shows the output in Firefox.
Figure 5. The database
It’s not much on looks, but that’s not the point. The point is that through use of the database object’s prepare and execute statements, you can add the data into the database. To prove it, I’ve created another page called list.php that shows the data in the database (see Listing 6).
Listing 6. List.php
This simple page starts by executing a SQL select operation against the names table. Then it creates a table and adds every row in the table to it using the fetchInto method to get the row data.
Figure 6 shows the output of the page.
Figure 6. Output from list.php
Again, not a beauty contest winner, but with this page, I have explained the basics of how to get to the data into the database. That, in turn, provides the basis for the script that will generate the Excel XML file for export.
Generate the export Excel XML
The final step is to generate the Excel XML. For me, that started with copying the Excel XML into a PHP script (see Listing 7). I know that’s lazy, but it’s the easiest way to get to an Excel XML file that parses properly. (Excel is picky about its XML.)
Listing 7. The XML export page
The script starts with setting the content type of the output to XML. That’s important because browsers will think this code is simply bad HTML otherwise.
I’ve changed the SQL query portion of the code to save the results of the query into an array. Typically, I wouldn’t do that with this type of report page, but in this case, I need to put the number of rows, plus one, into the ss:ExpandedRowCount attribute. The plus one is to account for the header row.
Figure 7 shows the result of clicking the link.
Figure 7. The export XML in Firefox
Not terribly impressive. But look what happens when I click the same link in Internet Explorer (see Figure 8):
Figure 8. The exported XML in Internet Explorer
What a difference. This is a full spreadsheet — formatting and all right — inside the browser. (Of course, in Firefox, you can right-click the link, save the XML to a file, and launch it that way.)
A technique with possibilities
As with anything on the bleeding edge, this technique has some pitfalls. For example, it doesn’t work on Macintosh yet because the latest Office for Mac version doesn’t support XML files.
Another hitch is that debugging these files can be a problem. If the XML is even slightly wrong, the embedded Excel object get into a kind of bad state in which Excel already thinks it’s running and refuses to launch. This can only be fixed only by restarting the application.
That said, this technique does offer unparalleled integration possibilities for PHP programmers. How often do you find that the source of the data is in something like Excel or Word and needs to be hand-migrated — cell by cell or paragraph by paragraph — into a Web application? With import technology like this, the problem is solved. You can read the data directly from the worksheets or document.
The same can be said of the export side. HTML is great for articles and papers, but was never designed to render spreadsheet information properly. With the techniques shown here, you can generate a spreadsheet — formulae, formatting, and all — in a way users expect to see it.
- PHP.net is the place to learn about the latest news about PHP, find downloads, and learn from other users.
- Microsoft Office Online is the best place to start for Office information, including support.
- An authoritative source for XML standards is the World Wide Web Consortium (W3C).
- XML in Office 2003: Information Sharing with Desktop XML (Prentice Hall, 2003), by Charles F. Goldfarb and Priscilla Walmsley, is a complete and wide-ranging guide.
- This November 2003 developerWorks article “Convert Excel data to XML” shows how to unlock data from Excel files to process them in XML, and examines the pros and cons of different solutions. It is appropriate for Excel 2002 and Excel XP client software.
- Find more resources for XML developers in the developerWorks XML zone.
- Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM’s products.
About the author
An engineer with with more than 20 years of experience, Jack Herrington is currently Editor-in-Chief of the Code Generation Network. He is the author of Code Generation in Action .