Results 1 to 3 of 3

Thread: Explore ways to pass a Microsoft Excel file to the client side

  1. #1
    Senior Member
    Join Date
    May 2003
    Posts
    472

    Explore ways to pass a Microsoft Excel file to the client side

    Web developers can produce a Microsoft Excel file and pass that file to the client in a variety of ways. To introduce you to some of your options, we'll examine a couple of methods here. Although this tutorial is written in VBScript, it should be simple to convert it into any other scripting language you want to use.



    First, we'll look at a simple approach in which we pass over an HTML table and then just change the Content Type we're sending to the browser to that of Excel. In the second, more complex and powerful solution, we'll use an ActiveX component to directly link between our code and the local instance of MS Excel.

    A simple approach
    If you want to use only a single Excel worksheet, you can use this approach. We create a standard HTML table and pass it back from our Server Side Script—in this case, a VBScript Active Server Pages (ASP) script—with the MIME type of Excel and not HTML. This call is then managed by the browser.

    The first thing we do in our script is to set the Content Type we are going to return to the client browser:
    Response.ContentType = "application/vnd.ms-excel"

    Now that we've told the browser that we're sending back an Excel file, we simply create our standard HTML table and send that back. The table in Listing A could be generated from the results of a database query, for example, or simply from static HTML.

    We can also include some simple Excel commands in the data. For instance, we could create three columns: first value, second value, and a total column, as shown in Listing B.

    The results you get depend on how your Web browser is set up to handle .XLS files. In some cases, it will render in the Web browser; in others, it will open Excel and load the file in there. This activity is determined by the local client machine settings for MIME types and is outside the control of a Web page.

    This technique should suffice many developers. But if you need a little more flexibility or complexity, you will have to use an ActiveX component.

    An ActiveX approach
    If you require more than a single worksheet, or if you require access to some of the VBA functions, you need to use ActiveX to create your file. In this approach, we'll create an instance of Excel on the client and then pass data and function calls into it.

    For this to work, end users must have their ActiveX settings in Internet Explorer set to Prompt or Enable for the correct zone. (At present, this functionality works only in Internet Explorer.)
    <script language='VBScript'>
    Dim Xcl

    Sub StartExcel()
    Set Xcl = CreateObject("Excel.Application")
    Xcl.Visible = true
    Set newBook = Xcl.Workbooks.Add

    First, we create our Excel object ,Xcl. This will be the link between our code and Excel. The rest of our code will reside within a VBScript subroutine called StartExcel. Next, we use the Server.CreateObject function to create a link to Excel and assign that to our object Xcl. Then, we set the visibility of the object to true and add a Workbook to our object:
    newBook.Worksheets.Add

    A standard Excel Workbook has three worksheets. For each additional sheet you require, you will need to call the Add function. For our example, we require only the standard number of sheets.

    As shown in Listing C, we will activate the first worksheet in the workbook so that it becomes the active sheet. We set the width of the first two columns to 50 characters and also enable word wrapping on these columns. We set a format mask for numeric data and set the cell alignment to left for the range of cells from A1 to B1000. The reason we use the actual values—such as -4131—and not the Excel Constant of xlLeft is that as we are interacting via ActiveX, so we don't have the visibility of these constants.

    Next, we set the background color of the first cell and enter some values into the worksheet to represent the data you may want to add. The last thing we do in this section is name the current worksheet My First Worksheet.

    Finally, we call the Save As dialog, passing in the suggested file name of Testing Excel Extraction. If the user selects another filename, that's fine too. But if he or she cancels the dialog, we detect that and use our preferred name. We then destroy our object by setting its value to nothing and redirect the use to another page. To get the code to run, we set the function to run when the HTML page has loaded by using the onLoad handler.

    In some cases, we may need to pass Excel function calls, such as SUM, in this way. We can do this by passing the full string just as we would type it into Excel. We can also use VBA code to achieve the same results, as shown in Listing D.

    In this example, we sum the values in the first two cells in the second column and place the results in the third row of the second column. In the forth row, we multiply that total value by 2.

    In response to a request from a fellow developer, I began working on converting some of the functionality into JavaScript. As Listing E shows, this conversion is quite simple—almost as simple as replacing the Dims with Vars and adding a semicolon at the end of each line.

    As you can see, there is little difference between this and the VBScript version. The main changes involve setting some of the layout values in our VBA code.

    It is certainly possible to extend the functionality. Recently, I worked on a method of pulling data from a data source into a form, allowing the user to amend the data—with the field resizing to accommodate the data entered—and then export the data plus any changes into Excel. I accomplished this by using client-side JavaScript to move through the form for its data rather than have it preset in a function.

    Creating the file on the server side
    If your output data file is large or complex, getting a client computer to do all the work may not be the best option. You can also produce the workbook on the file server side and then either provide a link to it or begin downloading the file automatically on its creation. This method allows you to process the file using the server, which is likely to be a more powerful computer.

    To demonstrate, we'll create a comma-separated values (CSV) file and provide a link to it. Step one is to get all of our data out of the data source. For the example, shown in Listing F, we use the getString function, which returns a recordset as a single string. For more complex processes, you may need to extract each element of the recordset in turn to perform an action on it, such as adding to a total or formatting.

    This gives us the file created on the server and allows us to return a link to that file to the user. One of the possible downsides with this approach is that the user is required to run the code to produce the file and then click on a link to begin the file download. The other main problem with this approach is that it will, over time, fill up disk space on your server. So you need to either manually clean up the files at some point or create a batch job to do this for you.

    Ready to create
    I hope you found this tutorial helpful. You should now be able to create an Excel file on the client browser containing your information, as well as to perform at least some simple functions on that data. Here are some of the sites I find most useful for this type of application:
    Source : http://builder.com.com/5100-6373-5072712.html?tag=crm
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

  2. #2
    Senior Member nihil's Avatar
    Join Date
    Jul 2003
    Location
    United Kingdom: Bridlington
    Posts
    17,188
    Does this mean that you can set up an excel spreadsheet with a macro virus and distribute it via websites?

    I remember a while back when Office 97 Macro viruses were all the rage, but you needed some sort of human vector to spread them from site to site. it was a slow process, which probably explains why they fell out of fashion fairly quickly...........also Office 2000 & 2002 became better protected, AND MORE EXPENSIVE

    If you can find a rapid mass distribution mechanism, like the net, this could bode trouble and a rebirth of macro viruses. People have sort of forgotten about them?

    I need to do more research, and see what warnings I get (if any) by trying to open such an object from a web page.

    Well spotted Null Device

    Cheers

  3. #3
    Senior Member
    Join Date
    Oct 2003
    Posts
    234
    If the user's ActiveX settings are set to prompt or disable, they will recieve a message box saying that the content might be unsafe or it will not open it at all. IE defaults to prompt, and anyone in the know enough to know how to change it will likely change it to disable. So, you will still need a "human vector" to get your malicious macros out on the web.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •