开发者

Export ListView to Word

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();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜