Export Web Control as Excel or Word

zhaozj2021-02-16  130

///

/// Export a web control

///

/// Control instance

/// Type: Excel or Word

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.

转载请注明原文地址:https://www.9cbs.com/read-8535.html

New Post(0)