Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Excel's Web Query

Excel's Web Query

In today's world of e-commerce, simply viewing data in a Web browser is rarely enough. Clients constantly demand reporting, charting, graphing, and analysis of complex data. Although Macromedia has made great strides in this direction by incorporating graphing capabilities (the new CFGRAPH) and access to a reporting engine (CFREPORT), these are all server-side solutions. A simple request for a new chart can send developers scrambling. Wouldn't it be great if end users could manipulate and analyze data themselves, using a powerful suite of tools that they're already familiar with!

Since its inception in 1988, Microsoft Excel has grown to become the Swiss Army knife of data analysis. While not always the best tool for the job, users tend to make it do just about anything. Nontechnical people can easily store inventories, financial records, even data best suited for a relational database. Excel then gives them the power to twist this data into any imaginable chart or report.

Getting the Data
With so many users familiar with the Excel interface, it would be useful to push live data into a spreadsheet, leaving the actual manipulation to the discretion of the user. But how do we feed live data into Excel from a Web server? Supply the users with tab delimited files? Excel has no problems there, but the user would have to download a new file every time the data changes. Copy and paste? Far too tedious. Office XP promises powerful new integration with the Web-based XML data.

Fortunately, an extremely powerful method of data acquisition has been around since Excel 97. The woefully underpublicized Web query allows Excel to fetch data from an HTML table anywhere on the Internet. This data is dropped directly into a worksheet, and behaves as if the user entered it manually. A simple click of the mouse instructs Excel to open an HTTP connection and get the latest data.

For this article, I've set up a special ColdFusion page that when given a part number generates a table of inventory data (see Figure 1). Excel can also handle preformatted text, blocked in the PRE tag.

For Excel to feed off this data, we must instruct the spreadsheet exactly where to find the table. From the Data menu, click Get External Data > New Web Query. A dialog box will then walk you through the steps necessary to import data. Excel 97 users will need to install Microsoft Query, included on the Microsoft Office 97 CD.

There are plenty of advanced options in the new Web Query window, and I'll let you explore them on your own. Once you tell Excel where to find its data, simply click OK, and watch the magic unfold. Excel will open a connection and parse the HTML file. Any formatting, whether HTML- or CSS-based, will be preserved (to a degree), so you can reliably set fonts, color, borders, etc.

Once the download is complete, right click on any imported cell and you'll see a new choice on the context menu. "Refresh Data" instructs Excel to go out and fetch the latest data, dropping it back into the table. There's also a Refresh Data icon on the toolbar (a red exclamation mark) that refreshes all the Web queries in the document. If your HTML table has changed sizes, Excel is even smart enough to expand or contract its range of cells to accommodate the change without overwriting other content on the sheet.

Notice that you no longer have to tell Excel where to get its data. The URL and any configuration parameters have been embedded into the document. You can convert a Web query back to plain data by right-clicking on the cell, choosing Data Range Properties, and clearing the "Save query definition" checkbox.

Excel Extensions
When composing the live Web page, Excel provides a few extensions to normal HTML. These extensions, invisible when viewing the page in a Web browser, provide extra functionality when the document is interpreted by Excel. We'll cover two of them here: filters and formulas. Additional extensions allow you to properly format data and work with PivotTables.

The first extension allows the user to quickly filter data based on a set of parameters. This applies if you have a row of column headers. In our case, the following code generates a header:

<TR>
<TD Filter="ALL">Warehouse Location</td>
<TD Filter="ALL">Quantity In Stock</td>
</tr>
By supplying the "filter" attribute, Excel will generate a drop-down menu of filter options (see Figure 2). The user can then choose to filter for a particular value, the top 10 values, etc. All of this is accomplished using Excel's built-in AutoFilter feature. Note that for filtering to work correctly, Excel needs to use HTML formatting. This is an option in the new Web Query window.

Excel also allows you to build a formula in a cell. For instance, to find the total stock of a given item across 10 warehouses, we could use:

<TD Formula="=Sum(B2:B11)"></td>
In this case, Excel will sum the values of the range B2 to B11 and display the result in the cell. If the user decides to change one of the values, the sum will update to show the change. Although not required in a formula attribute, the equal sign inside the quotes matches Excel's function notation.

It's important to remember that a user can drop the results of a Web query anywhere on a spreadsheet. It may seem like summing the results of cells B2 to B11 could be unreliable. What happens if the user puts the query at cell D35? Fortunately, Excel corrects this. When the formula appears on the sheet, it will use the data relative to your table. Therefore, if you specify cell A1 in a formula, it will always represent the upper-left corner of the HTML table, regardless of where the query was placed on the spreadsheet.

IQY Files
Although Web queries are obviously powerful, they still require a degree of configuration on the part of the user. Do you really want people to have to worry about URLs and query parameters? Luckily, Microsoft has devised a standard for defining a Web query. An IQY file, a simple text file, allows system administrators to package a query for distribution. The user can then run the query from the Data menu (click Get External Data > Run Saved Query). Excel 2000 and XP users can even double-click an IQY file and Excel will open a new spreadsheet and import the data. Unfortunately, this feature was left out of Excel 97.

To understand IQY files, let's look at a simple example:

WEB
1
http://www.myserver.com/inventory_
grid.cfm?partnum=10
The first two lines of an IQY file are easy, in fact, they're always the same. The first instructs Excel how it will import the data. Notice that "WEB" is uppercase. The second line represents the version of the IQY definition. Fortunately, Microsoft has created only one version, so we'll stick with 1. Running this IQY file will produce the same results as manually configuring a Web query. In the previous example, Excel imported and displayed inventory data for a particular part. What if we don't want to limit the user to a specific part? Let's consider a more sophisticated IQY file:

WEB
1
http://www.myserver.com/inventory_
grid.cfm?partnum=["Part Number","Please enter a part number:"]
Now the user will be prompted for a part number to display. The first argument ("Part Number") is a descriptive name of the variable. The second argument is displayed as a prompt when the user loads the data (see Figure 3). The user's entry is passed to the Web server as a URL parameter. To pass multiple URL variables, simply separate them by ampersands, as you would an ordinary URL:

WEB
1
http://www.myserver.com/inventory_
grid.cfm?partnum=["Part Number","Please enter a part number:"]
&warehouse= ["Warehouse ID",
"Please enter a warehouse ID:"]
If, on the other hand, you want to pass data as form variables, simply place the prompt on the fourth line:

WEB
1
http://www.myserver.com/inventory_grid.cfm
partnum=["Part Number","Please enter a part number:"]
Multiple form variables are delineated by ampersands, much like URL parameters:

WEB
1
http://www.myserver.com/inventory_grid.cfm
partnum=["Part Number","Please enter a part number:"]
&warehouse=["Warehouse ID","Please enter a warehouse ID:"]
There are a number of miscellaneous options you can specify on subsequent lines, such as "Selection=<table name>", which controls which table on the HTML page gets parsed. The best way to explore these extra parameters is to build a Web query in Excel, save it to an IQY file, and examine the results. Individual options may or may not be compatible with older versions of Excel, so be sure to test your files before distributing them!

Dynamic IQY Files ,br> Although it's fairly simple to write an IQY file and place it on the company intranet for users to download, there are times where it would be handy to generate IQY files on the fly. For instance, a Web page that displays inventory on a particular part could have a link "Connect to Excel". This could download a static IQY file, although an administrator would have to make sure that an individual IQY file exists for every part in the database.

If we could pass the part number in as a URL variable, we could write the following file, saving it as "connect_to_excel.cfm":

WEB
1
http://www.myserver.com/inventory_grid.cfm?partnum=
<CFOUTPUT>#URL.PartNumber#</cfoutput>
When users click the link to this CFM page, however, their Web browsers will likely display the file on screen as plain text. They would then have to save the file with an IQY extension, and then open it in Excel.

There are two distinct problems with this solution. First, the file is displayed on screen and the user must know to save it locally. To remedy this problem, we'll employ the CFCONTENT tag, which changes the MIME type of the document. Originally created to handle e-mail, the MIME type instructs the browser how to display a document. The default type for Web pages is "text/html".

In this case, we'll utilize the "application/octet-stream" type that is reserved for files the browser can't display, such as binary data. This will force a "Save As" dialog box to appear. The following CFML will return the file with the new MIME type:

<CFCONTENT type="application/octet-stream">
WEB
1
http://www.myserver.com/inventory_grid.cfm?partnum=
<CFOUTPUT>#URL.PartNumber#</cfoutput>
Notice that the CFCONTENT and the "WEB" specification are on the same line. If we were to put a linefeed between the two, an empty line would appear in the resulting file and confuse Excel. Once the "Save As" dialog box appears, the file will be saved as "link_to_excel.cfm". Ideally, the file should come with an IQY extension, ready to be loaded into Excel.

Instead of directly linking to connect_to_excel.cfm (our dynamic IQY file), we'll employ the following trick:

<A HREF="http://www.myserver.com/connect_to_excel.cfm
/DynamicLink.iqy">

ColdFusion is smart enough to process connect_ to_excel.cfm, but the browser sees the file as DynamicLink.iqy.

For More Information
Unfortunately, documentation for Web queries is hard to find, especially regarding the IQY file syntax. Microsoft published the Web Connectivity Kit for Excel 97, which, to my knowledge, was its last official publication on the matter, but there is another excellent resource at www.15seconds.com/issue/991021.htm. Microsoft also includes some great examples used to fetch stock reports.

By feeding live data into Excel, you can empower users to manipulate data on the client side without requiring constant changes to the Web application. They can then use Excel's capabilities to analyze and chart data. By using Web queries, users can effortlessly update their data.

More Stories By Norman Elton

Norman Elton is a computer science student and the ColdFusion guru at the College of William & Mary in Williamsburg, Virginia.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Anthony Frey 01/28/03 02:23:00 PM EST

Does anyone know how to get this to work in MX? I had .iqy files downloading fine with CF 5, but after the MX upgrade it says it can find anything..

Thanks,
Tony

@ThingsExpo Stories
SYS-CON Events announced today that Enzu will exhibit at SYS-CON's 21st Int\ernational Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enzu’s mission is to be the leading provider of enterprise cloud solutions worldwide. Enzu enables online businesses to use its IT infrastructure to their competitive advantage. By offering a suite of proven hosting and management services, Enzu wants companies to focus on the core of their ...
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), provided an overview of various initiatives to certify the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldwide re...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
When growing capacity and power in the data center, the architectural trade-offs between server scale-up vs. scale-out continue to be debated. Both approaches are valid: scale-out adds multiple, smaller servers running in a distributed computing model, while scale-up adds fewer, more powerful servers that are capable of running larger workloads. It’s worth noting that there are additional, unique advantages that scale-up architectures offer. One big advantage is large memory and compute capacity...
New competitors, disruptive technologies, and growing expectations are pushing every business to both adopt and deliver new digital services. This ‘Digital Transformation’ demands rapid delivery and continuous iteration of new competitive services via multiple channels, which in turn demands new service delivery techniques – including DevOps. In this power panel at @DevOpsSummit 20th Cloud Expo, moderated by DevOps Conference Co-Chair Andi Mann, panelists examined how DevOps helps to meet the de...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
The Internet giants are fully embracing AI. All the services they offer to their customers are aimed at drawing a map of the world with the data they get. The AIs from these companies are used to build disruptive approaches that cannot be used by established enterprises, which are threatened by these disruptions. However, most leaders underestimate the effect this will have on their businesses. In his session at 21st Cloud Expo, Rene Buest, Director Market Research & Technology Evangelism at Ara...
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...
Artificial intelligence, machine learning, neural networks. We’re in the midst of a wave of excitement around AI such as hasn’t been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. Will this time be different? Most likely. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Furthermore, the funding and research going into AI now comes from a wide range of com...
In this presentation, Striim CTO and founder Steve Wilkes will discuss practical strategies for counteracting fraud and cyberattacks by leveraging real-time streaming analytics. In his session at @ThingsExpo, Steve Wilkes, Founder and Chief Technology Officer at Striim, will provide a detailed look into leveraging streaming data management to correlate events in real time, and identify potential breaches across IoT and non-IoT systems throughout the enterprise. Strategies for processing massive ...
SYS-CON Events announced today that GrapeUp, the leading provider of rapid product development at the speed of business, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Grape Up is a software company, specialized in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market acr...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
SYS-CON Events announced today that Ayehu will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara California. Ayehu provides IT Process Automation & Orchestration solutions for IT and Security professionals to identify and resolve critical incidents and enable rapid containment, eradication, and recovery from cyber security breaches. Ayehu provides customers greater control over IT infras...
SYS-CON Events announced today that MobiDev, a client-oriented software development company, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MobiDev is a software company that develops and delivers turn-key mobile apps, websites, web services, and complex software systems for startups and enterprises. Since 2009 it has grown from a small group of passionate engineers and business...
SYS-CON Events announced today that Cloud Academy named "Bronze Sponsor" of 21st International Cloud Expo which will take place October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara, CA. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud com...
SYS-CON Events announced today that CA Technologies has been named "Platinum Sponsor" of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business - from apparel to energy - is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...