vb.net 遍历第一个excel表格,并把其中数据填到另一个excel表格的特定位置
vb.net 遍历第一个excel表格,并把其中数据填到另一个excel表格的特定位置 --------------------编程问答-------------------- 表格比较大的话,直接将第一个表格读入数组,然后遍历,再把新数组一次填入第二个表格。。。that's all 。。 --------------------编程问答--------------------
Dim xlApp As Excel.Application--------------------编程问答-------------------- Imports Microsoft.Office.Interop
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
'新建一个excel程序
xlApp = New Excel.Application
xlBook = xlApp.Workbooks.Add
xlSheet = xlBook.Worksheets.Item(1)
'读取excel内容 rowNum循环。
msgbox(xlSheet.Cells(RowNum, 1))
'....
'打开一个excel
xlBook = xlApp.Workbooks.Open("文件.xls")
xlSheet = xlBook.Worksheets.Item(1)
For i as Integer = 1 to xlSheet.UsedRange.Rows
''循环操作...UsedRange是Excel当前有数据的区域,包括 行数,列数。
next
Public Class importExcel
'取得excel数据,存储至数据库
Public Sub create(ByVal strFile As String)
Dim excelapp As Excel.Application '声明一个application对象
Dim excelworkbook As Excel.Workbook '声明一个工作簿对象
Dim excelsheet As Excel.Worksheet '声明一个工作表对象
Dim flag As Boolean = False
Try
sql = "select count(*) from plctagsm"
rs = Member.executeQuery(sql)
rs.Read()
If rs(0) > 0 Then
flag = False
Else
flag = True
End If
rs.Close()
Member.close()
Catch ex As Exception
End Try
If Not flag Then
message("数据库非空,请检查数据库!")
Else
excelapp = New Excel.Application
excelworkbook = excelapp.Workbooks.Open(strFile)
'访问到工作表:item使用索引值来得到sheet对象的引用
excelsheet = excelworkbook.Sheets.Item(1)
excelsheet.Activate()
sql = ""
'忽略第一行
For i = 2 To excelsheet.UsedRange.Rows.Count
'后两条是Bool型变量,不能加单引号
sql += "insert into plctagsm values(NULL," _
& "'" & excelsheet.Cells(i, 1).value & "'" _
& ",'" & excelsheet.Cells(i, 2).value & "'" _
& ",'" & excelsheet.Cells(i, 3).value & "'" _
& ",'" & excelsheet.Cells(i, 4).value & "'" _
& ",'" & excelsheet.Cells(i, 5).value & "'" _
& "," & excelsheet.Cells(i, 6).value _
& "," & excelsheet.Cells(i, 7).value _
& ");"
Next
Try
Member.executeUpdate(sql)
Member.close()
Catch ex As Exception
End Try
excelworkbook.Close()
End If
End Sub
End Class
这是我写的将打开的excel内容写入mysql数据库的程序,原理与你的要求一样,会对你有帮助的。 --------------------编程问答--------------------
Imports Microsoft.Office.Interop--------------------编程问答-------------------- 推荐使用NPIO
Public Class importExcel
'取得excel数据,存储至数据库
Public Sub create(ByVal strFile As String)
Dim excelapp As Excel.Application '声明一个application对象
Dim excelworkbook As Excel.Workbook '声明一个工作簿对象
Dim excelsheet As Excel.Worksheet '声明一个工作表对象
Dim flag As Boolean = False
Try
sql = "select count(*) from plctagsm"
rs = Member.executeQuery(sql)
rs.Read()
If rs(0) > 0 Then
flag = False
Else
flag = True
End If
rs.Close()
Member.close()
Catch ex As Exception
End Try
If Not flag Then
message("数据库非空,请检查数据库!")
Else
excelapp = New Excel.Application
excelworkbook = excelapp.Workbooks.Open(strFile)
'访问到工作表:item使用索引值来得到sheet对象的引用
excelsheet = excelworkbook.Sheets.Item(1)
excelsheet.Activate()
sql = ""
'忽略第一行
For i = 2 To excelsheet.UsedRange.Rows.Count
'后两条是Bool型变量,不能加单引号
sql += "insert into plctagsm values(NULL," _
& "'" & excelsheet.Cells(i, 1).value & "'" _
& ",'" & excelsheet.Cells(i, 2).value & "'" _
& ",'" & excelsheet.Cells(i, 3).value & "'" _
& ",'" & excelsheet.Cells(i, 4).value & "'" _
& ",'" & excelsheet.Cells(i, 5).value & "'" _
& "," & excelsheet.Cells(i, 6).value _
& "," & excelsheet.Cells(i, 7).value _
& ");"
Next
Try
Member.executeUpdate(sql)
Member.close()
Catch ex As Exception
End Try
excelworkbook.Close()
End If
End Sub
End Class
补充:.NET技术 , VB.NET