protected
void
Button1_Click(
object
sender, EventArgs e)
{
Export( " application/ms-excel " , " 打印报表.excl " );//导出到Excl
Export("application/ms-word", "打印报表.word");//导出到word
}
private void Export( string FileType, string FileName)
{
Response.Charset = " GB2312 " ;
Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
Response.AppendHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this .EnableViewState = false ;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
{
Export( " application/ms-excel " , " 打印报表.excl " );//导出到Excl
Export("application/ms-word", "打印报表.word");//导出到word
}
private void Export( string FileType, string FileName)
{
Response.Charset = " GB2312 " ;
Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
Response.AppendHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this .EnableViewState = false ;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
数据都来源于GridView1中,FileType可以根据需要修改类型.....