简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM