Answered by:
Export Gridview Databound Data to Excel

Question
-
Hey guys, i have a ASP.Net program that can export data of Databound Gridview innto Excel. Upon running and compiled in Visual Studio 2010 my program runs 100%, it export the data display in Gridview. I made my workstations as temporary server b enabling IIS features in it, now upon deploying it in my workstations when client tries to click the Export_button, the Exportfile comesout but not in Client PC, but in Server? how can i make this to show up in Client pc? can someone help me..
Here is my codebehind Export_button:
protected void Export_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = true;
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
int StartCol = 1;
int StartRow = 1;
int j = 0, i = 0;
//Write Headers
for (j = 0; j < GridView1.Columns.Count; j++)
{
Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow, StartCol + j];
myRange.Value2 = GridView1.Columns[j].HeaderText;
}
StartRow++;
//Write datagridview content
for (i = 0; i < GridView1.Rows.Count; i++)
{
for (j = 0; j < GridView1.Columns.Count; j++)
{
try
{
Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow + i, StartCol + j];
myRange.Value2 = GridView1.Rows[i].Cells[j].Text == null ? "" : GridView1.Rows[i].Cells[j].Text;
}
catch
{
;
}
}
}
}Wednesday, July 13, 2016 7:57 AM
Answers
-
>>>the Exportfile comesout but not in Client PC, but in Server? how can i make this to show up in Client pc? can someone help me..
According to your description, since your code is server code, this exportfile is in server not in client. So I suggest that you could download file from server to client.You can use an HTTP Handler (.ashx) to download a file, like this:
DownloadFile.ashx:public class DownloadFile : IHttpHandler { public void ProcessRequest(HttpContext context) { System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ClearContent(); response.Clear(); response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment; filename=Test.xlsx"); response.TransmitFile(context.Server.MapPath("Test.xlsx")); response.Flush(); response.End(); } public bool IsReusable { get { return false; } } }
Then you can call the HTTP Handler from the button click event handler, like this:
Markup:
<asp:Button ID="btnDownload" runat="server" Text="Download File" OnClick="btnDownload_Click"/>
Code-Behind:
protected void btnDownload_Click(object sender, EventArgs e) { Response.Redirect("PathToHttpHandler/DownloadFile.ashx"); }
Otherwise since this is the forum to discuss questions and feedback for Excel for Developers, this issue is more related to ASP.Net, I suggest that you could post your question on ASP.Net forum
http://forums.asp.net/
Thanks for your understanding.- Edited by David_JunFeng Thursday, July 14, 2016 7:57 AM
- Proposed as answer by David_JunFeng Wednesday, July 20, 2016 2:35 PM
- Marked as answer by David_JunFeng Tuesday, August 2, 2016 2:50 PM
Thursday, July 14, 2016 2:01 AM
All replies
-
>>>the Exportfile comesout but not in Client PC, but in Server? how can i make this to show up in Client pc? can someone help me..
According to your description, since your code is server code, this exportfile is in server not in client. So I suggest that you could download file from server to client.You can use an HTTP Handler (.ashx) to download a file, like this:
DownloadFile.ashx:public class DownloadFile : IHttpHandler { public void ProcessRequest(HttpContext context) { System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ClearContent(); response.Clear(); response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment; filename=Test.xlsx"); response.TransmitFile(context.Server.MapPath("Test.xlsx")); response.Flush(); response.End(); } public bool IsReusable { get { return false; } } }
Then you can call the HTTP Handler from the button click event handler, like this:
Markup:
<asp:Button ID="btnDownload" runat="server" Text="Download File" OnClick="btnDownload_Click"/>
Code-Behind:
protected void btnDownload_Click(object sender, EventArgs e) { Response.Redirect("PathToHttpHandler/DownloadFile.ashx"); }
Otherwise since this is the forum to discuss questions and feedback for Excel for Developers, this issue is more related to ASP.Net, I suggest that you could post your question on ASP.Net forum
http://forums.asp.net/
Thanks for your understanding.- Edited by David_JunFeng Thursday, July 14, 2016 7:57 AM
- Proposed as answer by David_JunFeng Wednesday, July 20, 2016 2:35 PM
- Marked as answer by David_JunFeng Tuesday, August 2, 2016 2:50 PM
Thursday, July 14, 2016 2:01 AM -
Please see this URL.
http://csharp.net-informations.com/excel/csharp-excel-datagridview.htm
That should give you pretty much everything you need. Also, see the links at the bottom of that page!!
Saturday, July 16, 2016 4:25 PM