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 = dgv1.ColumnCount
Dim row As Integer = dgv1.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
With excel.ActiveSheet.Range("A1:J1")
.MergeCells = True
End With
For i As Integer = 1 To col
Hoja.Cells.Item(1, i) = dgv1.Columns(i - 1).Name.ToString
excel.Workbooks(1).Worksheets(1).cells(1, 1).value = "รายงานงานเสร็จแล้วตามวันที่"
excel.Workbooks(1).Worksheets(1).cells(3, 1).value = "รหัสลูกค้า"
excel.Workbooks(1).Worksheets(1).cells(3, 2).value = "รหัสงาน"
excel.Workbooks(1).Worksheets(1).cells(3, 3).value = "วันที่รับงาน"
excel.Workbooks(1).Worksheets(1).cells(3, 4).value = "ชื่อ"
excel.Workbooks(1).Worksheets(1).cells(3, 5).value = "ตำบล"
excel.Workbooks(1).Worksheets(1).cells(3, 6).value = "อำเภอ"
excel.Workbooks(1).Worksheets(1).cells(3, 7).value = "จังหวัด"
excel.Workbooks(1).Worksheets(1).cells(3, 8).value = "ผู้ประเมิน 1"
excel.Workbooks(1).Worksheets(1).cells(3, 9).value = "ผู้ประเมิน 2"
excel.Workbooks(1).Worksheets(1).cells(3, 10).value = "วันที่ส่งงาน"
Next
For fila As Integer = 0 To row - 1
For e As Integer = 0 To col - 1
Hoja.Cells.Item(fila + 4, e + 1) = dgv1.Rows(fila).Cells(e).Value
Next
Next
Hoja.Rows.Item(1).font.bold = 5
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), VS 2008 (.NET 3.x), VS 2010 (.NET 4.x), VS 2012 (.NET 4.x)