The simplest way is to export everything on the page.
Called during loading. Note that no other controls, including buttons, are available on the page.
Void converttoexcel ()
{
Response. Clear ();
Response. Buffer = true;
Response. Charset = "GB2312 ";
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + DateTime. Now. ToString ("yyyyMMddhhmmss") + ". xls ");
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Response. ContentType = "application/ms-excel ";
This. Page. EnableViewState = false;
System. IO. StringWriter oStringWriter = new System. IO. StringWriter ();
System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );
This. Page. RenderControl (oHtmlTextWriter );
Response. Write (oStringWriter. ToString ());
Response. End ();
}
ASP. NET (C #) export data to Word or Excel
Namespace:
Using System. IO;
Using System. Text;
Export data from the DataGrid to Excel
String excelname = "excel file name ";
HttpContext. Current. Response. Charset = "GB2312 ";
HttpContext. Current. Response. ContentEncoding = Encoding. UTF8;
HttpContext. Current. Response. ContentType = "application/ms-excel ";
HttpContext. Current. Response. AppendHeader ("Content-disposition", "attachment; filename =" + excelname + ". xls ");
Dr1.Page. EnableViewState = false;
StringWriter sw = new StringWriter ();
HtmlTextWriter tw = new HtmlTextWriter (sw );
Dr1.RenderControl (tw );
HttpContext. Current. Response. Write (sw. ToString ());
HttpContext. Current. Response. End ();
Export data from the DataGrid to the Word
String excelname = "word file name ";
HttpContext. Current. Response. Charset = "GB2312 ";
HttpContext. Current. Response. ContentEncoding = Encoding. UTF8;
HttpContext. Current. Response. ContentType = "application/ms-winword ";
HttpContext. Current. Response. AppendHeader ("Content-disposition", "attachment; filename =" + excelname + ". doc ");
Dr1.Page. EnableViewState = false;
StringWriter sw = new StringWriter ();
HtmlTextWriter tw = new HtmlTextWriter (sw );
Dr1.RenderControl (tw );
HttpContext. Current. Response. Write (sw. ToString ());
HttpContext. Current. Response. End ();
ASP. NET 2.0, C # ---- use the GridView control to export other files (export Excel files and export Word files)
// Note: if you use the GridView to export files on the Visual Studio2005 platform,
// The VerifyRenderingInServerForm method must be reloaded.
Public override void VerifyRenderingInServerForm (Control control Control)
{
}
///
/// Method for exporting data to a file,
///
/// Model = 1: Export to Execl, Model = 2: Export to Word
Private void toFiles (int Model)
{
String strFileName = DateTime. Now. ToString ("yyyyMMdd-hhmmss ");
System. Web. HttpContext HC = System. Web. HttpContext. Current;
HC. Response. Clear ();
HC. Response. Buffer = true;
HC. Response. ContentEncoding = System. Text. Encoding. UTF8; // set the output stream to simplified Chinese
If (Model = 1)
{
// --- Export as an Excel file
HC. Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (strFileName, System. Text. Encoding. UTF8) + ". xls ");
HC. Response. ContentType = "application/ms-excel"; // set the output file type to an excel file.
}
Else
{
// --- Export as a Word file
HC. Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (strFileName, System. Text. Encoding. UTF8) + ". doc ");
HC. Response. ContentType = "application/ms-word"; // set the output file type to Word.
}
System. IO. StringWriter sw = new System. IO. StringWriter ();
System. Web. UI. HtmlTextWriter htw = new System. Web. UI. HtmlTextWriter (sw );
This. GridView1.RenderControl (htw );
HC. Response. Write (sw. ToString ());
HC. Response. End ();
}
//-Export as an Excel file
Protected void ToExecl_Click (object sender, EventArgs e)
{
ToFiles (1 );
}
//-Export as a Word file
Protected void button#click (object sender, EventArgs e)
{
ToFiles (2 );
}
FROM: http://hi.baidu.com/jg_%B3%C2/blog/item/4f0edf188851c50135fa41ce.html
C # operate on Word [to]
Import COM Library: Microsoft word 11.0 Object Library.