views:

296

answers:

1

To export a gridview to Excel, the reponse content type can be changed to "application/vnd.xls" and then rendered using gridName.RenderControl. Is there any reason this same approach cannot/should not be taken for rendering a listview out to word?

Is there another preferred method for exporting a listview to word?

UPDATE: I have verified that this will work with Word; however, when opening the file, Word displays the html tags (along with the content) from the listview. Below is the code.

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=test.doc");
Response.Charset = "";
Response.ContentType = "application/msword";

var stringWriter = new StringWriter();
var htmlWriter = new HtmlTextWriter(stringWriter);   
listView.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
+1  A: 

The same technique will also work for Microsoft Word. The reason this works the way it does, it that both applications will open and read HTML as the source document format. The filename and the content type are set by the website so that the browser knows the application needed to open the file.

Gabriel McAdams