加载Excel数据
Private Function LoadExcelData(ByVal Path As String)
txtMsg.Text = "Excel数据导入中..."
Dim msgError As String = ""
Dim miss As Object = System.Reflection.Missing.Value
Dim wss As Excel.Sheets
Dim ws As Excel.Worksheet = Nothing
Dim excelApp As Excel.Application = New Excel.Application
Dim wbs As Excel.Workbooks = excelApp.Workbooks
Dim dt As New Data.DataTable
Try
Dim wb As Excel.Workbook = wbs.Open(Path, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss)
excelApp.Visible = False
wss = wbs(1).Worksheets
ws = wss.Item(1)
Dim rowNum As Integer = ws.UsedRange.Cells.Rows.Count
Dim colNum As Integer = ws.UsedRange.Cells.Columns.Count
'读取栏位标题,创建临时表
For i As Integer = 0 To colNum - 1 '取列值
Dim FieldName As String = ws.Range(ws.Cells(1, i + 1), ws.Cells(1, i + 1)).Text.ToString().Trim()
If FieldName = "" Then
'检查Excel格式是否正确
msgError = "Excel格式不正确,请检查"
Exit For '相当于break退出
End If
dt.Columns.Add(FieldName)
Next
If msgError = "" Then
For i As Integer = 1 To rowNum - 1
Dim CellValue As String = ws.Range(ws.Cells(i + 1, 1), ws.Cells(i + 1, 1)).Text.ToString().Trim()
If CellValue = "" Then
Exit For
End If
Dim dr As DataRow = dt.NewRow()
For j As Integer = 0 To colNum
CellValue = ws.Range(ws.Cells(i + 1, j + 1), ws.Cells(i + 1, j + 1)).Text.ToString().Trim()
dr(j) = CellValue
Next
'ds.Rows.Add(dr)
dt.Rows.Add(dr)
Next
dgvSizeLabel.DataSource = dt
txtMsg.Text = "Excel数据导入成功"
Else
txtMsg.Text = "读取Excel发生错"
End If
Catch ex As Exception
Throw ex
'txtMsg.Text = "读取Excel发生错误"
'dgvSizeLabel.DataSource = ds.Tables("temp")
End Try
excelApp.Quit()
End Function
Private Function LoadExcelData(ByVal Path As String)
txtMsg.Text = "Excel数据导入中..."
Dim msgError As String = ""
Dim miss As Object = System.Reflection.Missing.Value
Dim wss As Excel.Sheets
Dim ws As Excel.Worksheet = Nothing
Dim excelApp As Excel.Application = New Excel.Application
Dim wbs As Excel.Workbooks = excelApp.Workbooks
Dim dt As New Data.DataTable
Try
Dim wb As Excel.Workbook = wbs.Open(Path, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss)
excelApp.Visible = False
wss = wbs(1).Worksheets
ws = wss.Item(1)
Dim rowNum As Integer = ws.UsedRange.Cells.Rows.Count
Dim colNum As Integer = ws.UsedRange.Cells.Columns.Count
'读取栏位标题,创建临时表
For i As Integer = 0 To colNum - 1 '取列值
Dim FieldName As String = ws.Range(ws.Cells(1, i + 1), ws.Cells(1, i + 1)).Text.ToString().Trim()
If FieldName = "" Then
'检查Excel格式是否正确
msgError = "Excel格式不正确,请检查"
Exit For '相当于break退出
End If
dt.Columns.Add(FieldName)
Next
If msgError = "" Then
For i As Integer = 1 To rowNum - 1
Dim CellValue As String = ws.Range(ws.Cells(i + 1, 1), ws.Cells(i + 1, 1)).Text.ToString().Trim()
If CellValue = "" Then
Exit For
End If
Dim dr As DataRow = dt.NewRow()
For j As Integer = 0 To colNum
CellValue = ws.Range(ws.Cells(i + 1, j + 1), ws.Cells(i + 1, j + 1)).Text.ToString().Trim()
dr(j) = CellValue
Next
'ds.Rows.Add(dr)
dt.Rows.Add(dr)
Next
dgvSizeLabel.DataSource = dt
txtMsg.Text = "Excel数据导入成功"
Else
txtMsg.Text = "读取Excel发生错"
End If
Catch ex As Exception
Throw ex
'txtMsg.Text = "读取Excel发生错误"
'dgvSizeLabel.DataSource = ds.Tables("temp")
End Try
excelApp.Quit()
End Function