Tuesday 11 March 2014

Create Excel File in vb.net


 Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO

 Private Sub btnexcelreport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexcelreport.Click
        Dim proc As System.Diagnostics.Process
        For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
            proc.Kill()
        Next
        Dim xlapp As Microsoft.Office.Interop.Excel.Application
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim xlworkbook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlworksheet As Microsoft.Office.Interop.Excel.Worksheet
        If DataGridView1.Rows.Count = 0 Then Exit Sub
        If DataGridView1.Rows.Count >= 1 Then
            Dim path = "d:\Affidavit Data Report(State-Wise)"
            Dim dr As New DirectoryInfo(path)
            If Not dr.Exists Then
                Directory.CreateDirectory(path)
            End If
            Dim finalpath As String = path & "\" & "Statewise Report.xls"
            xlapp = New Microsoft.Office.Interop.Excel.Application
            xlworkbook = xlapp.Workbooks.Add(misValue)
            'xlworksheet = xlworkbook.Sheets("sheet1")
            xlworkbook = xlapp.Workbooks.Add(misValue)
            xlworksheet = xlworkbook.Worksheets(1)
            xlworksheet.Cells(1, 1).Font.Size = 20
            xlworksheet.Cells(1, 1).Font.Bold = True
            '  xlworksheet.Cells(1, 1).Font.Color = Color.Red
            xlworksheet.Cells(1, 1) = "State Wize Affidavit Data ;"
            ' xlworksheet.Cells(1, 1).Font.Color = Color.Red
            xlworksheet.Cells(2, 1) = "State"
            xlworksheet.Cells(2, 2) = "Total Collges In Affidavit"
            xlworksheet.Cells(2, 3) = "Total Colleges In Master"
            xlworksheet.Cells(2, 4) = "Total Affidavits"
            xlworksheet.Rows(2).Font.Bold = True
            xlworksheet.Rows(2).Font.Size = 15
            ' xlworksheet.Rows(2).Font.Color = Color.Blue
            xlworksheet.Rows(2).WrapText = True
            xlworksheet.Columns(1).ColumnWidth = 23
            xlworksheet.Columns(2).ColumnWidth = 17
            xlworksheet.Columns(3).ColumnWidth = 17
            xlworksheet.Columns(4).ColumnWidth = 17
            Dim j As Integer = 3
            For i = 0 To DataGridView1.RowCount - 1
                xlworksheet.Cells(j, 1) = DataGridView1.Rows(i).Cells(0).Value.ToString()
                xlworksheet.Cells(j, 1).WrapText = True
                xlworksheet.Cells(j, 2) = DataGridView1.Rows(i).Cells(1).Value.ToString()
                xlworksheet.Cells(j, 3) = DataGridView1.Rows(i).Cells(2).Value.ToString()
                xlworksheet.Cells(j, 4) = DataGridView1.Rows(i).Cells(3).Value.ToString()
                j = j + 1
            Next
            xlworksheet.Rows(j - 1).Font.Bold = True
            xlworksheet.Rows(j - 1).Font.Size = 11
            ' xlworksheet.Rows(j - 1).Font.Color = Color.Blue
            xlworksheet.Rows(2).AutoFilter()
            xlworksheet.Rows(3).Activate()
            xlworksheet.Rows(2).WrapText = True
            xlworksheet.PageSetup.PrintGridlines = True
            xlapp.ActiveWindow.FreezePanes = True
            xlworksheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape
            Dim fileinfo As New DirectoryInfo(path)
            For Each ff In fileinfo.GetFiles()
                If ff.Exists Then
                    ff.Delete()
                End If
            Next
            xlworkbook.SaveAs(finalpath)
            Dim proc1 As System.Diagnostics.Process
            For Each proc1 In System.Diagnostics.Process.GetProcessesByName("EXCEL")
                proc1.Kill()
            Next
            Dim res As MsgBoxResult
            res = MsgBox("Process Completed.Would you like to Open File?", MsgBoxStyle.YesNo)
            If res = MsgBoxResult.Yes Then
                Process.Start(finalpath)
            End If
        End If
    End Sub

No comments:

Post a Comment

Followers