怎么用VB与EXCEL连接,实现对数据的录入、修改、删除
3个回答
展开全部
用以下代码:
Set ObjExcel = New Excel.Application
ObjExcel.SheetsInNewWorkbook = 1
Set ObjWorkbook = ObjExcel.Workbooks.Open("e:\111.xls") ‘xls的文件名路径可以修改
Set ExcelSheet = ObjWorkbook.Worksheets(1) ’指定为sheet1
ObjExcel.Visible = True
ExcelSheet.Cells(1, 1) = “123456” ‘给第一行第一列赋值
ExcelSheet.Cells(2, 1) = “45679” ’给第二行第一列赋值
’类推,括号中第1个数表示行,第2个数表示列,比如A1就是1,1,B1就是2,1
‘同理
ExcelSheet.Cells(1, 2) = “1111” ’A2单元格
ExcelSheet.Cells(2, 2) = “2222” ‘B2单元
With ObjExcel.ActiveSheet.Range("A1:B4").Borders '边框设置
.LineStyle = xlBorderLineStyleContinuous
.Weight = xlThin
.ColorIndex = 1
End With
ObjWorkbook.Save
ObjWorkbook.Close
Set ObjExcel = Nothing
Set ObjExcel = New Excel.Application
ObjExcel.SheetsInNewWorkbook = 1
Set ObjWorkbook = ObjExcel.Workbooks.Open("e:\111.xls") ‘xls的文件名路径可以修改
Set ExcelSheet = ObjWorkbook.Worksheets(1) ’指定为sheet1
ObjExcel.Visible = True
ExcelSheet.Cells(1, 1) = “123456” ‘给第一行第一列赋值
ExcelSheet.Cells(2, 1) = “45679” ’给第二行第一列赋值
’类推,括号中第1个数表示行,第2个数表示列,比如A1就是1,1,B1就是2,1
‘同理
ExcelSheet.Cells(1, 2) = “1111” ’A2单元格
ExcelSheet.Cells(2, 2) = “2222” ‘B2单元
With ObjExcel.ActiveSheet.Range("A1:B4").Borders '边框设置
.LineStyle = xlBorderLineStyleContinuous
.Weight = xlThin
.ColorIndex = 1
End With
ObjWorkbook.Save
ObjWorkbook.Close
Set ObjExcel = Nothing
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询