As a requirement to have result data in xls file from ASP.Net application for some reporting purpose, Here is the simple process to get the same output.
The sample code is provided below to download the records in the excel file in the same table format i.e data in each cell of the sheet. We assume to have huge amount of data in DataTable object and in same table format it is required to download in xls sheet.
To create a different cell of data here we have used the Html Table and its Row and Column concept.
Here is the code snippet in VB.Net language.
Protected Sub GenerateXls(ByVal fileName As String, ByVal dtData As DataTable)
Try
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName + "")
HttpContext.Current.Response.Flush()
Dim colCount, rowCount As Integer
HttpContext.Current.Response.Write("<Table>")
HttpContext.Current.Response.Write("<TR>")
For colCount = 0 To dtData.Columns.Count - 1
HttpContext.Current.Response.Write("<TD>")
HttpContext.Current.Response.Write(dtData.Columns(colCount).ColumnName.ToString())
HttpContext.Current.Response.Write("</TD>")
Next
HttpContext.Current.Response.Write("</TR>")
For rowCount = 0 To dtData.Rows.Count - 1
HttpContext.Current.Response.Write("<TR>")
For colCount = 0 To dtData.Columns.Count - 1
HttpContext.Current.Response.Write("<TD>")
HttpContext.Current.Response.Write(dtData.Rows(rowCount)(colCount).ToString())
HttpContext.Current.Response.Write("</TD>")
Next
HttpContext.Current.Response.Write("</TR>")
Next
HttpContext.Current.Response.Write("</Table>")
HttpContext.Current.Response.End()
Catch ex As Exception
End Try
End Sub
The sample code is provided below to download the records in the excel file in the same table format i.e data in each cell of the sheet. We assume to have huge amount of data in DataTable object and in same table format it is required to download in xls sheet.
To create a different cell of data here we have used the Html Table and its Row and Column concept.
Here is the code snippet in VB.Net language.
Protected Sub GenerateXls(ByVal fileName As String, ByVal dtData As DataTable)
Try
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName + "")
HttpContext.Current.Response.Flush()
Dim colCount, rowCount As Integer
HttpContext.Current.Response.Write("<Table>")
HttpContext.Current.Response.Write("<TR>")
For colCount = 0 To dtData.Columns.Count - 1
HttpContext.Current.Response.Write("<TD>")
HttpContext.Current.Response.Write(dtData.Columns(colCount).ColumnName.ToString())
HttpContext.Current.Response.Write("</TD>")
Next
HttpContext.Current.Response.Write("</TR>")
For rowCount = 0 To dtData.Rows.Count - 1
HttpContext.Current.Response.Write("<TR>")
For colCount = 0 To dtData.Columns.Count - 1
HttpContext.Current.Response.Write("<TD>")
HttpContext.Current.Response.Write(dtData.Rows(rowCount)(colCount).ToString())
HttpContext.Current.Response.Write("</TD>")
Next
HttpContext.Current.Response.Write("</TR>")
Next
HttpContext.Current.Response.Write("</Table>")
HttpContext.Current.Response.End()
Catch ex As Exception
End Try
End Sub
No comments:
Post a Comment
Put your comments here