The easiest way to do this is to export everything on the page.
Called during load, note that there are no other controls in the page, including buttons
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
Namespaces:
Using System.IO;
Using System.Text;
Export data from 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 Word
String Excelname= "word filename";
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 additional files (export Excel, export Word files)
Note that under the visual Studio2005 platform, if you export a file using the GridView,
You must overload the Verifyrenderinginserverform method
public override void Verifyrenderinginserverform (Control control)
{
}
///
Method of exporting to a file,
///
Model=1: Exporting to execl,model=2: exporting 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 output stream to Simplified Chinese
if (Model = = 1)
{
---exported 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 Output file type to excel file.
}
Else
{
---exported 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 Output file type is Word file.
}
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 to Excel file
protected void Toexecl_click (object sender, EventArgs e)
{
Tofiles (1);
}
-Export to Word file
protected void Button1_Click (object sender, EventArgs e)
{
Tofiles (2);
}
From:http://hi.baidu.com/jg_%b3%c2/blog/item/4f0edf188851c50135fa41ce.html
C # operations word[turn]
Import COM library: Microsoft Word 11.0 Object Library.