02
Sat, Nov
2 New Articles

Making the Most of Excel Web Query

Microsoft
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
If your company is like most, you probably have a handful of devoted Microsoft Excel users who are constantly looking for data from your iSeries. If your users currently use the iSeries Access data transfer function to get this data into Excel, the problem is that the data becomes outdated as soon as the transfer is complete. Microsoft Query is another option, but it can be a bit overwhelming for people who don't know your database.

What if you could prepare data queries for your users without worrying about ODBC data sources? That's where Excel Web Query comes in. If you're not already familiar with Web Query, check out "Getting Data into Microsoft Excel Using the Web." This functionality allows you to retrieve data from a Web page directly into an Excel spreadsheet.

In this article, I'll examine the benefits of using Web Query. I'll also step through a simple example of creating a Web Query, and then I'll explore how to create Active Server Page (ASP) Web pages that can be used to prepare data for use in an Excel Web Query. Finally, I'll explain how to use VBScript code within your Excel spreadsheet to customize the data retrieved by your Web Query.

Why Web Query?

The uses for this powerful tool are numerous. One great example is if you have a customer or supplier who allows you to retrieve order information through the Internet. Using Web Query, you can pull that order information directly into Excel and manipulate it into whatever format you need. When you pull data using Web Query, you have the option of retaining the HTML formatting of the data, including embedded hyperlinks. This means that if the data you're retrieving contains a hyperlink to a page containing information related to the data you've retrieved, that link will be available within the spreadsheet. This not only saves time but also adds some great functionality to your spreadsheet. I'll step you through the basic process of importing HTML data into an Excel spreadsheet.

Getting the Data

With Office XP, the process of importing Web data into Excel has been simplified a bit because Microsoft has built a browsing function into Excel that allows you to point to the specific page and table from which the data is to be extracted. To create a new Web Query, select the Data menu, then select Get External Data, and finally select New Web Query. The screen display (shown in Figure 1) allows you to browse to the location of the page containing the data you want to import into Excel. The big change in Office XP is that this browsing can be done directly in Excel; you don't have to launch a browser window to get the location of the page.

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV400.jpg

Figure 1: This wizard retrieves Web data into Excel. (Click images to enlarge.)

When the page is loaded into this window, several arrow icons will allow you to select one or more tables within the page to be imported into Excel. When a table is selected for import, the icon changes to a check mark. This added functionality makes it much easier to incorporate data from a single table into your spreadsheet. When you consider that HTML tables are used not only for displaying columns of data but also for other aspects of page design, the ability to select which table you want to retrieve data from can make the import process much more useful. You can import only the required data into your spreadsheet instead of the entire page. Click on the Options button to display the dialog shown in Figure 2.

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV401.jpg

Figure 2: You can define how the data is imported from this dialog.

At this top of this dialog box, you can define how much of the Web data formatting (font sizes and colors, background colors, etc.) will be retained within Excel. If you select None, the data is imported into Excel without any of the formatting that existed in the Web page. This data will appear as though it were simply typed into Excel. The Rich Text Formatting Only option incorporates some, but not all, of the Web page formatting by converting the data to Microsoft Rich Text format prior to importing. The final option is Full HTML Formatting. This option copies the data into your spreadsheet exactly as it appears in the Web page, including hyperlinks, which can be used directly from within the spreadsheet.

The middle section of this dialog allows you to define how blocks of data within HTML

 tags are imported. The 
 tag identifies data that has been preformatted for display in the 
browser.  

The two selections at the bottom of the dialog let you prevent Excel from automatically detecting cells containing date values and allow you to ignore any Web Query redirections defined in the page. It is possible within an HTML table definition to identify a different location. Figure 3 shows how the table in Figure 1will appear when imported into Excel. You'll notice in this figure that you have retained all of the HTML formatting, including background coloring and fonts.


http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV402.jpg

Figure 3: Your Web data will look like this in your Excel spreadsheet.

Once the data is in Excel, you can use any of the standard Excel functions to manipulate and massage the data into whatever format you need. For example, you could easily take the data shown above and create an Excel chart. Then, to update the Excel spreadsheet with current data, you simply right-click within the area of the spreadsheet containing the Web data and select Refresh Data from the Context menu.

Building a Better Web Query

The one downside to what I've covered so far is that it relies on data contained in a Web page outside of your control. You can extend this concept by using Web Query to access custom-made ASPs. These ASPs will handle the database access for you. The advantage to this method is that, unlike Microsoft Query and ODBC, it allows you to take control of what data is made available to Excel users. To use this method, you must be running a Web server that supports ASPs. The most likely option here would be Microsoft's Internet Information Server (IIS). The ASP source shown in Figure 4 loads data from the selected fields in the SYSTABLES file on the iSeries and displays these fields in an HTML table.

"
    for x=0 to rs.fields.count-1
Response.Write ""
    next

    ' Read through and output data
    do until rs.EOF
    
    Response.Write ""
    for x=0 to rs.fields.count-1
Response.Write ""
    next
    
    rs.movenext
    loop
   
    ' clean up and close
    rs.close: set rs = nothing  
    conn.close: set conn = nothing  
%>

SYSTABLES


<% 

    on error resume next
 
    ' Create Our ADO Connection Object
    set conn = Server.CreateObject("ADODB.Connection") 
    conn.open "DRIVER=Client Access ODBC Driver (32-bit); " & _ 
              " UID=user; PWD=secret; System=192.168.0.2;" 
 
    ' Create The Source for The Recordset and open it
   SQL = "SELECT TABLE_NAME, TABLE_SCHEMA, COLUMN_COUNT, ROW_LENGTH " & _ 
               " FROM QSYS2.SYSTABLES"

    set rs=Server.CreateObject("ADODB.Recordset")
    
    rs.Open SQL,conn

    ' Output columns headings
    Response.Write "
" & rs.Fields(X).Name & "
" & rs.Fields(x).Value & "


Figure 4: This source creates a simple Active Server Page.

Just to clarify, ASPs are Web pages with embedded VBScript or JavaScript code that dynamically generates HTML source for the Web page. The VBScript or JavaScript is executed on the Web server and therefore allows for a more secure means by which to access a data source. In this example, I've used ActiveX Data Objects (ADOs) to retrieve data from the iSeries. The VBScript then reads this data and outputs it to the browser within the cells of an HTML table. The technique used here is the same technique you would use to read data from any file on your system and display it in an HTML table. You pull this data into Excel using the method described earlier in this article, but this time, you point to the URL that references the page in Figure 4. Figure 5 shows how this data will appear in Excel when loaded using the None option for formatting.

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV403.jpg

Figure 5: This data was loaded from the iSeries through an ASP.

In this case, you'll notice that the data appears without any of the formatting shown in the earlier example. It's also possible to create an ASP that accepts parameters, which can be used to filter the data displayed in the table and ultimately displayed in Excel. These parameters are passed through what is called a Querystring. The Querystring portion of a URL is identified by a question mark (?). Each parameter passed in is identified in the format Parm=value.

When specifying more than one variable, an ampersand (&) is placed between variables. For example, the following Querystring passes two variables in to the ASP identified in the URL.

http://webserver/myfirst.asp?lib=QSYS2&file=SYSTABLES


In this example, the value of the parameter lib would be QSYS2, and the value of the variable file would be SYSTABLES. The code in Figure 6 shows how these variables would be read into and used within an ASP.

"
    for x=0 to rs.fields.count-1
Response.Write ""
    next

    ' Read through and output data
    do until rs.EOF
    
    Response.Write ""
    for x=0 to rs.fields.count-1
Response.Write ""
    next
    
    rs.movenext
    loop
   
    ' clean up and close
    rs.close: set rs = nothing  
    conn.close: set conn = nothing  
%>

 SYSCOLUMNS 


<%     
    ' Read in Querystring Variables
     lib=Request.Querystring("lib") 
     file=Request.Querystring("file") 

    ' Create Our ADO Connection Object
    set conn = Server.CreateObject("ADODB.Connection") 
    conn.open "DRIVER=Client Access ODBC Driver (32-bit); " & _ 
              " UID=user; PWD=secret; System=192.168.0.2;" 
 
   ' Create The Source for The Recordset and open it
   SQL = "SELECT COLUMN_NAME, LABELTEXT, COLTYPE, LENGTH " & _ 
               " FROM QSYS2.SYSCOLUMNS WHERE TABLE_NAME='" & file & "' AND " & _
" TABLE_SCHEMA='" & lib & "' " 

    set rs=Server.CreateObject("ADODB.Recordset")
    
    rs.Open SQL,conn

    ' Output columns headings
    Response.Write "
" & rs.Fields(X).Name & "
" & rs.Fields(x).Value & "

Figure 6: This ASP example accepts two Querystring variables.

This page is similar to the example used earlier. The difference is that I read in two Querystring variables and used them to build the WHERE clause for the SQL statement that acts as the source for the ADO Recordset. This example selects field data from the file and library supplied through the Querystring. The Querystring variables are read into the ASP through the Request.Querystring statement. In each case, the parameter supplied to the Request.Querystring statement represents the name of the variable to be read in. To incorporate this page into an Excel spreadsheet, you specify the full URL, including the Querystring variables as shown earlier. Figure 7 shows the resulting data displayed in Excel.

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV404.png

Figure 7: This Excel spreadsheet is retrieved using Querystring variables.

This method gives you the flexibility of being able to select the information to be imported. The downside is that you have to modify the URL in your Web Query every time you want to change the data. You can get around this little problem by incorporating some VBScript code into your Excel spreadsheet.

Using VBScript with Web Query

By incorporating VBScript code into an Excel spreadsheet, you can control elements of the spreadsheet, including Web Query options. You can add a button to your spreadsheet and build the Querystring dynamically, based on values from cells within the spreadsheet.

Now, I'll explain the process of creating an Excel spreadsheet with embedded VBScript code that will be used to load data from the page in Figure 6. To start off, you'll need to go into Excel and create a new (blank) spreadsheet. Next, you'll add headings in cells A1 and C1. These headings will identify the cells to contain the values to be passed through your Querystring variables into the ASP. Type Library: into cell A1 and File: into cell C1. Now, you're ready to drag a command button onto the page. You'll find the command button icon on the toolbox, which can be displayed by right-clicking on the menu bar area of the screen and selecting Control Toolbox. This will cause the VB toolbox to be displayed. Locate the icon in the toolbox that looks like a small command button as shown highlighted here:

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV405.png

Now, to create your command button, click and drag the pointer onto an area of the spreadsheet from E1 to F2. Right-click on the command button and select Properties. Change the Caption property to read "Display Field Data." Your spreadsheet should appear as shown in Figure 8.

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV406.png

Figure 8: Prepare your spreadsheet for VBScript code.

After closing the Properties dialog, right-click on the command button and select View Code to display the VBScript development environment for the CommandButton1_Click event. This will allow you to define the code to be executed when the command button is clicked. Figure 9 contains the code to be placed in the CommandButton1_Click event.

Private Sub CommandButton1_Click()

    Dim WebQ, Rp, ErrFl As Boolean

    'Make sure values have been entered - Display error
    Sheet1.Range("A3", "D999").ClearContents
    ErrFl = False
    Sheet1.Range("B1").Interior.Color = RGB(255, 255, 255)
    Sheet1.Range("D1").Interior.Color = RGB(255, 255, 255)
    If Trim(Sheet1.Range("B1")) = "" Then
        ErrFl = True
        Sheet1.Range("B1").Interior.Color = RGB(255, 0, 0)
    End If
    If Trim(Sheet1.Range("D1")) = "" Then
        ErrFl = True
        Sheet1.Range("D1").Interior.Color = RGB(255, 0, 0)
    End If
     If ErrFl Then
        Rp = MsgBox("An Entry Is Required in the Highlighted Cell(s)", vbOKOnly, "Error!")
        Exit Sub
    End If
     
    'Clear Existing Data
    Sheet1.Range("A3", "D999").ClearContents
    
    If Sheet1.QueryTables.Count > 0 Then
        Sheet1.QueryTables(1).Delete
    End If
    
    ' Add new query table using Web URL.
    ' Insert data from spreadsheet cells for querystring varaibles
    Set WebQ = Sheet1.QueryTables.Add("URL;http://webserver/syscolumns.asp?lib=" & _
                        UCase(Sheet1.Range("B1").Value) & "&file=" & _
                        UCase(Sheet1.Range("D1").Value), Range("A3"))
    
    ' Set properties for web query
    WebQ.Name = "ColumnData"
    WebQ.WebFormatting = xlWebFormattingNone
    WebQ.RefreshOnFileOpen = False
    WebQ.BackgroundQuery = True
    WebQ.TablesOnlyFromHTML = True
    
    ' Load Web Data
    WebQ.Refresh
    
End Sub 

Figure 9: This code will build a Web Query by inserting cell values into the URL.

This script first clears the contents of the destination range. Then, it creates a new Querytable within your spreadsheet. A Querytable (not to be confused with a Querystring) object is the link to the Get External Data menu option. The first parameter identifies the URL from which the data is to be retrieved. In this case, the values for the two Querystring variables are fed in from spreadsheet cells B1 and D1. Once your Querytable object (WebQ) has been created, you define some properties that control how the data will be imported into Excel. The .Name property simply gives an identifier to the new Querytable. The .WebFormatting option identifies what, if any, of the HTML formatting should be included. These are the possible values:

  • xlWebFormattingAll:--All HTML formatting is included.
  • xlWebFormattingNone:--None of the HTML formatting is imported.
  • xlWebFormattingRTF:--The HTML formatting is converted to Rich Text Format prior to importing.


The .RefreshOnFileOpen property defines whether or not the data is automatically refreshed when the spreadsheet is opened. The .BackgroundQuery property identifies whether data refreshes are performed in the background or users must wait for the refresh to complete before they can continue working with the spreadsheet. The .TablesOnlyFromHTML property defines whether only the HTML table data should be imported or the entire page should be imported. Finally, the .Refresh method is used to initiate the Web data import.

Once you've entered the code for your CommandButton1_Click event, close the source window and return to your spreadsheet. To activate your command button, click on the icon labeled Exit Design Mode (shown here) from the Control toolbox:

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV407.png

Your new command button will now be active. To test your new button, enter a valid library name in cell B1 and a valid file name in D1. Then, click on the Display Field Data command button to retrieve the data from the ASP. The results should appear as shown in Figure 10.

http://www.mcpressonline.com/articles/images/2002/ExcelWebASPV408.png

Figure 10: This spreadsheet will automatically retrieve data from an ASP.

As you can see, you can easily extend the use of Excel Web Query functionality using VBScript code. You can reuse the technique shown in the simple example by modifying the URL and Querystring variables to point to your own ASP. You can also easily modify the ASP example shown in Figure 7 to retrieve whatever data is required from your iSeries. With a little tweaking, you can take the examples shown here and change them to retrieve any data that your Excel users need. The best part is that you won't ever again have to explain data transfers or ODBC!

Mike Faust is MIS Manager for The Lehigh Group in Macungie, Pennsylvania. Mike is also the author of The iSeries and AS400 Programmer's Guide to Cool Things from MC Press. For more information on Active Server Page programming, watch for Mike's upcoming book entitled Active Server Pages Primer from MC Press . You can contact Mike at This email address is being protected from spambots. You need JavaScript enabled to view it..

Mike Faust

Mike Faust is a senior consultant/analyst for Retail Technologies Corporation in Orlando, Florida. Mike is also the author of the books Active Server Pages Primer, The iSeries and AS/400 Programmer's Guide to Cool Things, JavaScript for the Business Developer, and SQL Built-in Functions and Stored Procedures. You can contact Mike at This email address is being protected from spambots. You need JavaScript enabled to view it..


MC Press books written by Mike Faust available now on the MC Press Bookstore.

Active Server Pages Primer Active Server Pages Primer
Learn how to make the most of ASP while creating a fully functional ASP "shopping cart" application.
List Price $79.00

Now On Sale

JavaScript for the Business Developer JavaScript for the Business Developer
Learn how JavaScript can help you create dynamic business applications with Web browser interfaces.
List Price $44.95

Now On Sale

SQL Built-in Functions and Stored Procedures SQL Built-in Functions and Stored Procedures
Unleash the full power of SQL with these highly useful tools.
List Price $49.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: