The Web service has now created an XML document. Returning to this document directly, you find that the browser sees only an XML document rendered with the default stylesheet (see Figure 2
). To render this document as a spreadsheet, simply manipulate the MIME type of the returned content. This effectively fools the browser into thinking it's being sent a workbook, when in fact you are sending it an XML document.
Here's the code:
Establish the response stream using the HttpContext.get_Current() call within j# and manipulate it with the three lines of code above. The first line clears the response stream, the second sets the MIME type to that of an Excel document, and the third outputs the contents of the newly manipulated XML document. The Excel document is now rendered in the browser space.
In some cases, the user will get a warning about opening the file. To override this, open explorer and open the tools->folder options dialog. From this dialog, under the 'File Types' tab, select XLS as the file extension, click 'Advanced,' and uncheck the 'Confirm Open after Download' check box.
Using Other Office Documents
Presently, Excel is the only Office application to have a pure XML implementation of its base document type. However, Word and Powerpoint have HTML representations containing inline XML of the document contents. It's possible for you to manipulate these documents by loading the HTML of your template document into a string buffer and then use search and replace to put your data into the correct locations.
The MIME types for these applications are:
XML's Usefulness Is Growing
As the industry adoption of XML grows, companies like Microsoft are conceiving innovative new ways to use it. There is a clear opportunity here to take advantage of the flexibility XML provides. Besides solving data interchange problems, XML can help streamline the development process, making us more efficient as developers. This article presents just one of the countless possibilities.