首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp页面怎么弹出保存和打开Excel文件的对话框?

2013-08-01 
asp页面如何弹出保存和打开Excel文件的对话框???我是想在asp页面中单击导出按钮的时候我就可以把Dataset中

asp页面如何弹出保存和打开Excel文件的对话框???
我是想在asp页面中单击导出按钮的时候我就可以把Dataset中的数据导出来,然后就可以打开或者保存,求大哥们尽量给代码咯
[解决办法]
参考:

public void ExportResult(DataTable dt, string excelName)  
{  
    Response.Clear();  
    Response.Charset = "";  
    Response.ContentType = "applicationnd.ms-xls";  
    StringWriter sw = new StringWriter();  
    HtmlTextWriter htmlWrite = new HtmlTextWriter(sw);  
  
    DataGrid dg = new DataGrid();  
    dg.DataSource = dt;  
    dg.DataBind();  
    dg.RenderControl(htmlWrite);  
    Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(excelName));  
    Response.Write(sw.ToString());  
    Response.End();  

[解决办法]
以流的形式导出,并以附件的形式导出
如1楼代码
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(excelName));  
[解决办法]
   /// <summary>
        /// 导出Excel
        /// </summary>
        protected void btn_exl_Click(object sender, EventArgs e)
        {

            ToExcel();

        }
//必须有 不然会出错
        public override void VerifyRenderingInServerForm(Control control)
        {

        }




        public void ToExcel()//整个GRIDVIEW导出到EXCEL
        {
            gvSoft.Columns[0].Visible = false;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("决算完成【" + DateTime.Now.ToString("yyyy-MM-dd") + "】.xls", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-excel";

            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            int row = gvSoft.Columns.Count;

            //关闭控件的视图状态
            gvSoft.Page.EnableViewState = false;
            //初始化HtmlWriter
            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);

            gvSoft.RenderControl(htmlWriter);

            //输出

            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();

        }

 

热点排行