您的位置:首页 > 其它

DataGrid中的数据倒入Excel

2005-08-19 10:20 309 查看
Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
Dim App As New Excel.Application
Dim Wk As Excel.Workbook
Dim Ws As Excel.Worksheet
Dim openFile As OpenFileDialog = New OpenFileDialog
Dim filename As String
Dim iCount As Integer
Dim i As Int16
Dim Gstr As String
Dim strSql As String
Dim strSql1 As String
Dim dsSearch As DataSet
Try
openFile.DefaultExt = "xls"
openFile.Filter = "Word documents (*.xls)|*.xls"
openFile.ShowDialog()
If (openFile.FileNames.Length > 0) Then
For Each filename In openFile.FileNames
Wk = App.Workbooks.Open(filename)
Ws = Wk.Sheets("Sheet1")
iCount = App.ActiveSheet.usedrange.rows.count
ProgressBar1.Maximum = iCount
ProgressBar1.Minimum = 0
For i = 4 To App.ActiveSheet.usedrange.rows.count
strSql1 = "select * from DateReport where PeopleId='" & Ws.Cells(i, 3).value & "' and ProjId='" & Ws.Cells(i, 4).value & "' and WorkId='" & Ws.Cells(i, 5).value & "'" & " and DepartId='" & Ws.Cells(i, 1).value & "'"
dsSearch = QueryTable(strSql1)
If dsSearch.Tables(0).Rows.Count > 0 Then
strSql1 = "delete from DateReport where PeopleId='" & Ws.Cells(i, 3).value & "' and ProjId='" & Ws.Cells(i, 4).value & "' and WorkId='" & Ws.Cells(i, 5).value & "'" & " and DepartId='" & Ws.Cells(i, 1).value & "'"
executeSql(strSql1)
End If
strSql = "insert into DateReport(PeopleName,PeopleId,DepartId,ProjId,WorkId,PlanSDate,PlanEDate,FactSDate,FactFWork,AccidentId,IntendDate,Mark) values('" & _
Ws.Cells(i, 2).value & "','" & Ws.Cells(i, 3).value & "','" & Ws.Cells(i, 1).value & "','" & Ws.Cells(i, 4).value & "','" & Ws.Cells(i, 5).value & "','" & _
"20" & Ws.Cells(i, 6).value.ToString.Replace(".", "") & "','" & "20" & Ws.Cells(i, 7).value.ToString.Replace(".", "") & "','" & "20" & Ws.Cells(i, 8).value.ToString.Replace(".", "") & "','" & Ws.Cells(i, 9).value & "','" & Ws.Cells(i, 10).value & "','" & _
Ws.Cells(i, 11).value & "','" & Ws.Cells(i, 12).value & "')"
executeSql(strSql)
ProgressBar1.Value = iCount - i
Next
MessageBox.Show("获取记录成功")
'Wk.Close()
'wk=nothing
'App = Nothing
Next
End If
fillGrid()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
App.ActiveWorkbook.Close()
App.Workbooks.Close()
App.Quit()
Wk = Nothing
App = Nothing
GC.Collect()
End Try
End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: