///
/// Export a web control
/// summary>
/// Control instance param>
/// Type: Excel or Word param>
Public void expertControl (System.Web.ui.Control Source, DocumentType Type)
{
// Set the header information of HTTP, encoding format
IF (Type == DocumentType.excel)
{
// Excel
Response.Appendheader ("Content-Disposition", "Attachment; FileName = Result.xls");
Response.contentType = "Application / MS-Excel";
}
ELSE IF (Type == DocumentType.Word)
{
// Word
Response.Appendheader ("Content-Disposition", "Attachment; FileName = result.doc");
Response.contentType = "Application / MS-Word";
}
Response.Charset = "UTF-8";
Response.contentencoding = system.text.Encoding.utf8;
// Turn off the status of the control
Source.page.enableviewState = false;
// Initialize HTMLWRITER
System.io.stringWriter Writer = new system.io.stringwriter ();
System.Web.ui.htmlTextWriter htmlwriter = new system.Web.ui.htmlTextWriter (Writer);
Source.RenderControl (HTMLWRITER);
// output
Response.write (Writer.toString ());
Response.end ();
}
// Document type
Public Enum DocumentType PUBLIC ENUM
{
Word,
Excel
}
Call method:
ExpertControl (this, DocumentType.Word);
This is exporting the entire page to Word.