Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Open("C:\Path\File.xls")
Dim xlWS As Microsoft.Office.Interop.Excel.Worksheet = xlWB.Worksheets("Sheet1")
DataGridView1.RowCount = xlWS.UsedRange.Rows.Count - 1
DataGridView1.ColumnCount = xlWS.UsedRange.Columns.Count
Try
For c As Integer = 1 To xlWS.UsedRange.Columns.Count
DataGridView1.Columns(c - 1).HeaderText = xlWS.UsedRange.Cells(1, c).Value
Next
For r As Integer = 2 To xlWS.UsedRange.Rows.Count
For c As Integer = 1 To xlWS.UsedRange.Columns.Count
With DataGridView1(c - 1, r - 2)
.Value = xlWS.UsedRange.Cells(r, c).Value
.Style.BackColor = ColorTranslator.FromOle(xlWS.UsedRange.Cells(r, c).Interior.Color)
End With
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
xlWS = Nothing
xlWB.Close(False)
xlWB = Nothing
xlApp.Quit()
xlApp = Nothing
End Try
|