Click here to Skip to main content
15,945,334 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ............

I want to Export grid view data to excel.I have no problem in this but only problem is
that Excel file is storing in Solution itself.I want to show a dialog box for asking user to select path to store that excel file.

here is my code

protected void btnExport_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = Session["data"] as DataTable;
string FolderPath;

StreamWriter objSw;
//Create CSV file
if (Directory.Exists(Server.MapPath("~/Reports")))
{
FolderPath = Server.MapPath("~/Reports");

objSw = new StreamWriter(FolderPath + "/TrackRequest" + DateTime.Now.ToString("ddMMMyyyyHHmmss") + ".csv");
}
else
{
FolderPath = Server.MapPath("~/Reports");

Directory.CreateDirectory(FolderPath);

objSw = new StreamWriter(FolderPath + "/TrackRequest" + DateTime.Now.ToString("ddMMMyyyyHHmmss") + ".csv");
}

//get table from GridView1
DataTable objDt = dt; //((DataSet)gvPayDetail.DataSource).Tables[0];

//Get No Of Column in GridView
int NoOfColumn = objDt.Columns.Count;

//Create Header
for (int i = 0; i < NoOfColumn; i++)
{
objSw.Write(objDt.Columns[i]);

//check not last column
if (i < NoOfColumn - 1)
{
objSw.Write(",");
}
}
objSw.Write(objSw.NewLine);

//Create Data
foreach (DataRow dr in objDt.Rows)
{
for (int i = 0; i < NoOfColumn; i++)
{
objSw.Write(dr[i].ToString());

if (i < NoOfColumn - 1)
{
objSw.Write(",");
}
}
objSw.Write(objSw.NewLine);
}
objSw.Close();
}
Posted

Use datatable to excel function
private void DataTableToCSVFile(string filename, DataTable dt)
   {
       HttpContext context = HttpContext.Current;
       context.Response.Clear();
       DataTable dtExcel = dt;
       foreach (DataColumn column in dtExcel.Columns)
       {
           context.Response.Write(column.ColumnName + ",");
       }
       context.Response.Write(Environment.NewLine);
       foreach (DataRow row in dtExcel.Rows)
       {
           for (int i = 0; i < dtExcel.Columns.Count; i++)
           {
               context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
           }
           context.Response.Write(Environment.NewLine);
       }
       context.Response.ContentType = "text/csv";
       context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "_" + DateTime.Now + ".csv");
       context.Response.End();
   }
 
Share this answer
 
Include this :
C#
using System.Windows.Forms;
OpenFileDialog openFileDialog = new OpenFileDialog();
 
Share this answer
 
Comments
Dave Kreskowiak 26-Oct-15 23:25pm    
Ummm... WRONG on so many levels it's not funny.

You're seriously going to tell him to put up an OpenFile dialog on a web server where no human will ever see it?? This is a ASP.NET application, not Windows Forms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900