日期:2014-05-18  浏览次数:20461 次

将数据从datagridview中导出到excel中,导出完毕后自动保存到给定的路径中。
当导出完毕后自动保存到给定的路径中。
请问如何实现?


------解决方案--------------------
C# code
Response.Clear();
        Response.Buffer = true;
        Response.Charset = "UTF-8";
        Response.AppendHeader("Content-Disposition", "attachment;filename=UploadTenantFailure.xls");
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "application/ms-excel"; 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
        this.gvBusinessList.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();

public override void VerifyRenderingInServerForm(Control control)
    {
        // base.VerifyRenderingInServerForm(control);
    }

------解决方案--------------------
Response.Clear();
Response.Buffer = true;
Response.Charset = "gb2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=users.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"); 
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
------解决方案--------------------
导出EXCEL
C# code
/// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="dgExport">要导出的GridView</param>
    /// <param name="dtData">导出的数据源</param>
    public static void Export(GridView dgExport, DataTable dtData)
    {
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        // IO用于导出并返回excel文件 
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            // 设置编码和附件格式 
            curContext.Response.Clear();
            curContext.Response.ClearContent();
            curContext.Response.Buffer = true;
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//utf-7可以解决一定问题
            curContext.Response.Charset = "GB2312";
            curContext.Response.AppendHeader("content-disposition", "filename=\"" + System.Web.HttpUtility.UrlEncode(dtData.TableName, System.Text.Encoding.UTF8) + ".xls\"");


            // 导出excel文件 
            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            HtmlForm frm = new HtmlForm();
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(dgExport);


            dgExport.DataSource = dtData.DefaultView;
            dgExport.DataBind();
            // 返回客户端 
            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    }

------解决方案--------------------
保存在服务器端:
HTML code
<input id="fileExcel" runat="server" type="file" />

------解决方案--------------------