public void ToExcelsaa(System.Web.UI.WebControls.DataGrid dgExport)
{
dgExport.Visible = true;
dgExport.BorderColor = Color.Black;
dgExport.HorizontalAlign = HorizontalAlign.Center;
dgExport.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dgExport.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
dgExport.EditItemStyle.HorizontalAlign = HorizontalAlign.Center;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Report.xls");
//HttpContext.Current.Response.Charset = "UTF-8" '"gb2312" ' ;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
// HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default
HttpContext.Current.Response.ContentType = "application/ms-excel";
dgExport.Page.EnableViewState = false;
System.IO.StringWriter tw= new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw= new System.Web.UI.HtmlTextWriter(tw);
dgExport.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
//以下是以前写的vb.net, 把页面内容导出到excel(为整理)
Dim oRpt As ReportDocument = New ReportDocument
Dim msg As New message.Info
Dim diskOpts As New DiskFileDestinationOptions
oRpt.ExportOptions.ExportFormatType = ExportFormatType.Excel
oRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
''这里我们导出成为.pdf格式文件,你也能选择上面的其它类型文件
'' 导出成为其它文件时也需要这个选项
'' 如Microsoft Exchange, MAPI等
diskOpts.DiskFileName = "c://cc.xls" ''如果你不指定确切的目录,那么文件就会保存到[Windows]/System32目录中去了
oRpt.ExportOptions.DestinationOptions = diskOpts
''水晶报表文件不包含直接的FileName属性,因此你不能直接指定保存的文件名
''所以你不得不使用DiskFileDestinationOptions对象,设置它的DiskFileName属性
''为你想要的路径,最后将水晶报表的DestinationsOptions属性指定为上面的DiskFileDestinationOption
oRpt.Export()
msg.getMsg("c://" & Trim(DpDealer.SelectedItem.Text) & ".xls文件导出成功!")
Response.ClearContent()
Response.ClearHeaders()
Response.WriteFile("c://cc.xls")
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=bb.xls")
HttpContext.Current.Response.ContentType = "application/ms-excel"
Response.Flush()
Response.Close()
System.IO.File.Delete("c://cc.xls")