Private Sub GirdToExcel(ByRef dgvw As DataGridView)
Dim excel As New Microsoft.Office.Interop.Excel.Application
Dim Libro As Microsoft.Office.Interop.Excel.Workbook
Dim Hoja As Microsoft.Office.Interop.Excel.Worksheet
Dim col As Integer = dgv_2.ColumnCount
Dim row As Integer = dgv_2.RowCount
Libro = excel.Workbooks.Add
Hoja = excel.Worksheets.Add()
excel.Workbooks(1).Worksheets(1).cells(1, 1).columnWidth = 10
excel.Workbooks(1).Worksheets(1).cells(1, 1).RowHeight = 20
For i As Integer = 1 To col
Hoja.Cells.Item(1, i) = dgv_2.Columns(i - 1).Name.ToString
excel.Workbooks(1).Worksheets(1).cells(1, 1).value = ""
excel.Workbooks(1).Worksheets(1).cells(1, 2).value = ""
excel.Workbooks(1).Worksheets(1).cells(1, 3).value = ""
excel.Workbooks(1).Worksheets(1).cells(1, 4).value = "รายงานงานเสร็จแล้ว"
excel.Workbooks(1).Worksheets(1).cells(1, 5).value = ""
excel.Workbooks(1).Worksheets(1).cells(1, 6).value = ""
excel.Workbooks(1).Worksheets(1).cells(1, 7).value = ""
excel.Workbooks(1).Worksheets(1).cells(2, 1).value = "ลำดับที่"
excel.Workbooks(1).Worksheets(1).cells(2, 2).value = "รหัสงาน"
excel.Workbooks(1).Worksheets(1).cells(2, 3).value = "คำนำหน้า"
excel.Workbooks(1).Worksheets(1).cells(2, 4).value = "ชื่อ"
excel.Workbooks(1).Worksheets(1).cells(2, 5).value = "จังหวัด"
excel.Workbooks(1).Worksheets(1).cells(2, 6).value = "ผู้ประเมิน"
excel.Workbooks(1).Worksheets(1).cells(2, 7).value = "ราคาประเมิน"
Next
For fila As Integer = 0 To row - 1
For e As Integer = 0 To col - 1
Hoja.Cells.Item(fila + 3, e + 1) = dgv_2.Rows(fila).Cells(e).Value
Next
Next
Hoja.Rows.Item(1).font.bold = 2
Hoja.Rows.Item(2).font.bold = 1
Hoja.Rows.Item(1).horizontalAlignment = 3
Hoja.Columns.AutoFit()
excel.Application.Visible = True
excel.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized
Hoja = Nothing
Libro = Nothing
excel = Nothing
End Sub
Tag : .NET, Excel (Excel.Application), VB.NET, VS 2010 (.NET 4.x)