There are at least three ways to export tables. The first method is to use Excel to support HTML and directly use the control's RenderControl () method to output the control's HTML code to the client as Excel; the second method is Excel's com interface library. The third method is to use data access objects to Operate Excel files to export Excel files.
The following is the first method.
[Csharp]
// Table output
Public void Report (System. WEB. UI. Pagepage, Repeater rpt, string title, string type)
{
// Clear all content output in the buffer
HttpContext. Current. Response. Clear (parameter ){......};
// Set the HTTP character of the output stream
HttpContext. Current. Response. Charset = "uft-8 ";
// Add the HTTP header to the output stream
HttpContext. Current. Response. AddHeader ("Content-Disposition ",
"Attachment; filename =" + HttpUtility. urlEncode (DateTime. now. toString ("yyyyMMddHHMMss") + ". xls ", Encodin g. UTF8 ). toString (parameter ){......});
// Set the HTTPMIME type of the output stream
HttpContext. Current. Response. ContentType = "application/ms-excel ";
// Set whether the view status of the current page is maintained at the end of the page request and the view status of any server controls it contains.
Page. EnableViewState = false;
System. IO. StringWriteroStringWriter = new System. IO. StringWriter (parameter ){......};
System. WEB. UI. HtmlTextWriter oHtmlTextWriter =
New System. WEB. UI. HtmlTextWriter (oStringWriter );
// Output the content of the server control to the provided HtmlTextWriter object
Rpt. RenderControl (oHtmlTextWriter );
// Write the information to the HTTP Response output stream
HttpContext. current. response. write ("
String temp = oStringWriter. ToString (parameter ){......};
HttpContext. Current. Response. Write (temp );
HttpContext. Current. Response. Write ("</tbody> </table> </body>
// Output to the client
HttpContext. Current. Response. End (){......};
}
Html on the front-end asp.net page:
<Table> <thead> header </thead> <tbody> Repeater control </tbody> </table>
From chenpeggy