Friday, August 2, 2013

to generate an excel file in asp.net

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Private Sub GenerateExcel()
        Try
                    Dim excelApp As Microsoft.Office.Interop.Excel.Application
            Dim workBook As Microsoft.Office.Interop.Excel.Workbook
            Dim workSheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim sqlstring As String = String.Empty
            Dim sqlcon As SqlConnection
            Dim strConString As String
            Dim sqlcmd As SqlCommand
            Dim da As SqlDataAdapter
            Dim ds As DataSet
            Dim ExcelDT As DataTable
            Dim RowNum As Integer = 0
            Dim ColumnNum As Integer = 0
                       Dim MyExcel As Object = CreateObject("Excel.Application")
            If MyExcel Is Nothing Then
                Alert("Unable to generate excel file")
                Return
            End If
            System.Runtime.InteropServices.Marshal.ReleaseComObject(MyExcel)
            MyExcel = Nothing
            strConString = System.Configuration.ConfigurationManager.ConnectionStrings("CONSTRING").ConnectionString
            sqlstring = "SELECT * FROM TblTest"
            sqlcon = New SqlConnection(strConString)
            sqlcmd = New SqlCommand(sqlstring, sqlcon)
            da = New SqlDataAdapter(sqlcmd)
            ds = New DataSet()
            da.Fill(ds, "details")
            ExcelDT = ds.Tables("details")
            sqlcon.Close()
            sqlcmd.Dispose()
            sqlcon.Dispose()
            If Not Directory.Exists(Server.MapPath("~/Data")) Then
                Directory.CreateDirectory(Server.MapPath("~/Data"))
            End If
            Dim TargetFile As String = Server.MapPath("~/Data/") & DateTime.Now.ToString("ddMMyyyy-HHmmss.fff") & ".xlsx"
            excelApp = New Microsoft.Office.Interop.Excel.Application
            workBook = excelApp.Workbooks.Add()
            workSheet = workBook.Worksheets("Sheet1")
            workSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape
            With workSheet
                RowNum = 1
                .Range("A" & RowNum, "Q" & RowNum).Font.Bold = True
                .Range("A" & RowNum, "Q" & RowNum).Font.ColorIndex = 5
                .Range("A" & RowNum, "Q" & RowNum).Font.Size = 9
                .Range("A" & RowNum, "Q" & RowNum).Font.Name = "Tahoma"
                                .Cells(RowNum, 1).value = "No."
                For ColumnNum = 1 To ds.Tables("details").Columns.Count
                    If (ColumnNum = 1) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col1"
                        workSheet.Columns._Default(ColumnNum + 1).ColumnWidth = 25
                    ElseIf (ColumnNum = 2) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col2"
                    ElseIf (ColumnNum = 3) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col3"
                    ElseIf (ColumnNum = 4) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col4"
                    ElseIf (ColumnNum = 5) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col5"
                    ElseIf (ColumnNum = 6) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col6"
                    ElseIf (ColumnNum = 7) Then
                        .Cells(RowNum, ColumnNum + 1).value = "Col7"
                    Else
                        .Cells(RowNum, ColumnNum + 1).value = ds.Tables("details").Columns(ColumnNum - 1).ColumnName
                    End If
                    workSheet.Columns._Default(ColumnNum + 1).ColumnWidth = 25
                Next
                .Range("A" & RowNum, "Q" & RowNum).Font.Bold = True
                .Range("A" & RowNum, "Q" & RowNum).Font.ColorIndex = 5
                .Range("A" & RowNum, "Q" & RowNum).Font.Size = 8
                RowNum = RowNum + 1

                For Each DR As DataRow In ds.Tables("details").Rows
                    .Cells(RowNum, 1).value = ds.Tables("details").Rows.IndexOf(DR) + 1
                    For ColumnNum = 1 To ds.Tables("details").Columns.Count
                        .Cells(RowNum, ColumnNum + 1).value = DR(ColumnNum - 1).ToString().Trim()
                    Next
                    RowNum = RowNum + 1
                Next
            End With
            workSheet.SaveAs(TargetFile)
            workBook.Close()
            workBook = Nothing
            excelApp.Quit()
            excelApp = Nothing
            Alert("Excel File Generated Succesfully!")
        Catch ex As Exception
            Alert(ex.ToString)
        End Try
    End Sub

for alert
Public Sub Alert(ByVal msg As String)
        Page.ClientScript.RegisterStartupScript([GetType], "Alert", "<script>alert('" & msg & "')</script>")
    End Sub

No comments:

Post a Comment