从网格视图导出数据到asp

从网格视图导出数据到asp

本文介绍了从网格视图导出数据到asp.net中的excel文件中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨............

我正在尝试将网格视图数据导出到excel文件.我已成功完成此操作,但是问题是Excel文件存储在解决方案本身中.但是我想将该excel文件保存在downloads文件夹中.我该如何解决呢?请任何人帮助我....

这是我的代码


Hi............

I am trying to export grid view data to excel file.I have done this successfully but the problem is Excel file is storing in solution itself.but i want to save that excel file in downloads folder.how can i solve this? Please help me anyone....

here is my code


 protected void btnExport_Click(object sender, EventArgs e)
    {

        DataTable dt = new DataTable();
        dt = Session["data"] as DataTable;

        if (dt == null)
        {
            throw new Exception("No Records to Export");
        }
        string Path = "D:\\ImportExcelFromDatabase\\Karvyexcel_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";

        //string Path = "D:\\Sujatha\\Karvy_27Nov2012\\Inventory_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";
        FileInfo FI = new FileInfo(Path);
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
        DataGrid DataGrd = new DataGrid();
        DataGrd.DataSource = dt;
        DataGrd.DataBind();

        DataGrd.RenderControl(htmlWrite);
        ////string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
        ////if (!Directory.Exists(directory))
        ////{
        ////    Directory.CreateDirectory(directory);
        ////}

        //System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
        stringWriter.ToString().Normalize();
        //vw.Write(stringWriter.ToString());
        //vw.Flush();
        //vw.Close();
        WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());


    }
    public static void WriteAttachment(string FileName, string FileType, string content)
    {
        HttpResponse Response = System.Web.HttpContext.Current.Response;
        Response.ClearHeaders();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = FileType;
        Response.Write(content);
        Response.End();

    }

private void BindCustomerData()
    {
        try
        {
            string strSelect = "select * from CustomerData";
            DataSet ds = new DataSet();
            cnn.ConnectionString = connStr;
            // if the connection will be closed the below code poen the connection when the page will be loaded.
            if (cnn.State == ConnectionState.Closed)
            {
                cnn.Open();
            }
            // here i am using the store procedure named tb_gallery_insert to insert the record inside the database.
            SqlDataAdapter da = new SqlDataAdapter(strSelect, cnn);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvData.DataSource = ds.Tables[0];
                gvData.DataBind();

                Session["data"] = ds.Tables[0];

            }
            else
            {
                lblMessage.Text = "No Records Found";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

推荐答案


这篇关于从网格视图导出数据到asp.net中的excel文件中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:42