Browse DevX
Sign up for e-mail newsletters from DevX


Serve Office Documents Using MIME Types and J#.NET : Page 4

The conversion of the native office document format to XML allows developers to create documents on the fly and serve them from a Web service.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

HttpContext.get_Current().get_Response().Clear(); HttpContext.get_Current().get_Response()._ set_ContentType("application/vnd.ms-excel"); HttpContext.get_Current().get_Response()._ Write(xmlOutputDoc.get_InnerXml());

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:

application/msword application/vnd.ms-powerpoint

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.

Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date