从datagrid中导出数据到excel?
在窗体中,点击“查询”按钮,在datagrid1中显示出来符合条件的记录,再单击“打印”按钮,把这些记录导出到excel 表格中,并打印,如何做?毕业设计用!在线等!
[解决办法]
Dim ExcelFileName As String
CmDg_out.CancelError = True
On Error GoTo CancelHandler
CmDg_out.DefaultExt = ".xls "
CmDg_out.Flags = cdlOFNExtensionDifferent + cdlOFNHideReadOnly
CmDg_out.Filter = "Excel Files (*.xls)|*.xls "
CmDg_out.FilterIndex = 1
CmDg_out.ShowSave
ExcelFileName = CmDg_out.FileName
VFGd_out.SaveGrid ExcelFileName, flexFileCustomText, True
MsgBox "导出Excel文件完成! ", vbOKOnly + vbExclamation, "提示 "
Exit Sub
CancelHandler:
MsgBox "未导出Excel文件! ", vbOKOnly + vbExclamation, "提示 "
End Sub
C#版
Response.Clear();
Response.Buffer= true;
Response.Charset= "utf-8 ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename= "+HttpUtility.UrlEncode( "企业资质统计.xls "));
Response.ContentEncoding=System.Text.Encoding.GetEncoding( "utf-8 ");//设置输出流为简体中文
// Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel ";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo( "ZH-CN ",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.dgrd_qiye.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();