The text of this article is not in this database — only its details are. The old site it was published on is gone, but the Internet Archive kept a copy: How to Export ASP.NET DataGrid Contents to Excel or Word
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Heinrich HendriksPosted Oct 22, 2010, 9:22 AM
Is it possible to view a .xlsm (excel file with macro) through a web browser? I want to view the data of the file through a web page. Problem is that when i want to view the file, the download pop-up comes up. I don't want to down load it, i want users to view it! Any hint, any help would be greatly appreciated! My skype name is "heinrichhudson" if you would rather want to explain a little better. Kind regards
PeroPosted Sep 16, 2010, 4:30 AM
This method doesn't produce binary Excel files (XLS, XLSX). If you require this functionality, I recommend you take a look at this Excel .NET component. Here is a sample Excel C# code how to export DataGrid to Excel from your Excel ASP.NET application: // Get DataTable that DataGrid is bound to. var dataTable = (DataTable)dataGrid.DataSource; // Create new ExcelFile. var ef = new ExcelFile(); // Add new worksheet to the file. var ws = ef.Worksheets.Add(dataTable.TableName); // Insert the data from DataTable to the worksheet starting at cell "A1". ws.InsertDataTable(dataTable, "A1", true); // Stream file to browser. Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls"); ef.SaveXls(Response.OutputStream); Response.End();
Geetha MouliPosted Jun 14, 2010, 7:36 AM
Hi Mahesh, I have export to excel option for all my reports. In few of report, if i export to excel file the file just has 'No records found' the text which i have mensioned in <EmptyDataTemplate> In few other reports i am not getting the proper value or proper format. I don't have any clue why it is happening like that. Here is my code. .aspx file <asp:GridView ID="GridView1" runat="server" CssClass="txt" AlternatingRowStyle-BorderStyle="Ridge" BorderStyle="Ridge" CellSpacing="2" BorderColor="#333333" CellPadding="2"> <EmptyDataTemplate> No records found. </EmptyDataTemplate> </asp:GridView> .vb page On page load... Dim strQuery As String = "SELECT Gate, COUNT(TicketInfo) AS 'People Entered' FROM tbl_access_info" Dim cmd As SqlCommand = New SqlCommand(strQuery, New SqlConnection(strConnectionString)) cmd.Connection.Open() GridView1.DataSource = cmd.ExecuteReader() GridView1.DataBind() cmd.Connection.Close() cmd.Connection.Dispose() Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView) Response.Clear() Response.Buffer = True Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim oStringWriter As New System.IO.StringWriter Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter) GridView1.RenderControl(oHtmlTextWriter) Response.Write(oStringWriter.ToString()) Response.[End]() End Sub Can u help me to identify the problem? Thanks in advance. Geetha
Do JungPosted Oct 6, 2008, 3:09 PM
Thanks for the code. I get this error however: Exception Details: System.ArgumentException: 'TableRow' cannot have children of type 'LiteralControl'. My data grid is inside a master template in the content section. Does this have anything to do with it? Thank you
senthilPosted Mar 5, 2008, 5:53 PM
Mahesh, How do I export it to a template. My template just has header and footer with company logo. If I pass the path my template, it doesnt read it. So, is there any solution?
swetha hariniPosted Jan 26, 2008, 12:13 PM
Hi, Do you have any idea of export data into .mpp. Please help me in this regard. Thanks Harini.
pratik ramaniPosted Dec 27, 2007, 4:33 AM
sir how to export datagrid to word format in C# in windows appl.
AravindPosted Sep 7, 2007, 12:32 PM
Hi Mahesh..thanks for your column.... I have a requirement to export my data into a .mpp( Microsoft project plan). Is that can be done?? Do we have Response. contenttype in .mpp?? Please help me in this...Thanks my email id [email protected]
bunnsPosted Sep 6, 2007, 4:15 AM
i am using REPORTOBJECT to generate crystal report problem is while openeign it in the pdf it is not asking for the open/save dialogue box directly opening in the same browser Dim strExportFile As String Dim objExOpt As ExportOptions Dim objDiskOpt As New DiskFileDestinationOptions strExportFile = ReportName & ".pdf" 'Response.AddHeader("content-disposition", "attachment;filename=ReportName.pdf") myReportDocument1.Load(Server.MapPath("MonthlyDetailsReport.rpt")) ApplyInfo(myReportDocument1) GetDetails() objDiskOpt.DiskFileName = Server.MapPath(".") & "/" & strExportFile objExOpt = myReportDocument1.ExportOptions objExOpt.ExportDestinationType = ExportDestinationType.DiskFile objExOpt.ExportFormatType = ExportFormatType.PortableDocFormat objExOpt.DestinationOptions = objDiskOpt myReportDocument1.Export() myReportDocument1.Close() Return strExportFile i want to open it in a different pdf file not in the same browser to do that i am not getting the dialoguebox plese help me its urgent
bunnsPosted Sep 5, 2007, 11:43 PM
i want to export datagrid to pdf while doing this i am getting an error [Adobereader (7.0 version)couldnt not open this file ("FILENAME")because it is not either a supported file type or because the file has been damaged] my code is Response.Clear() Response.Buffer = True Dim attach As String = "attachment;filename =export.pdf" Response.ClearContent() Response.ContentType = "application/pdf" Response.AddHeader("content-disposition", attach) Dim str As New StringWriter Dim htr As New HtmlTextWriter(str) grid1.RenderControl(htr) Response.Write(str.ToString()) Response.End() i searched in the web i didnt find any solution for this, could u please help me to solve this its very urgent thanks in advance
anjali kakumaniPosted May 16, 2007, 3:43 AM
I solved my problem.Thank u
gabriel rinconPosted Apr 16, 2007, 8:19 AM
Good morning. I am trying to get controls to not show up in the export. When I use your code, I get this error: System.ArgumentException was caught Message="'TableRow' cannot have children of type 'LiteralControl'." Source="System.Web" StackTrace: at System.Web.UI.WebControls.TableRow.CellControlCollection.Add(Control child) at _Default.ClearControls(Control ctrl) in F:\projects\pioneer\mms\Default.aspx.vb:line 33 at _Default.ClearControls(Control ctrl) in F:\projects\pioneer\mms\Default.aspx.vb:line 15 at _Default.ClearControls(Control ctrl) in F:\projects\pioneer\mms\Default.aspx.vb:line 15 at _Default.ClearControls(Control ctrl) in F:\projects\pioneer\mms\Default.aspx.vb:line 15 at _Default.Submit1_ServerClick(Object sender, EventArgs e) in F:\projects\pioneer\mms\Default.aspx.vb:line 135 I am also trying to get the column names to be just column names - not column names that are"clickable" url links Any ideas? thanks
SenthilPosted Feb 1, 2007, 12:40 PM
hi, how can i export two datagrid values into a single Excel Sheet