vb6.0添加excel的问题 一下代码提示编译错误 “用户定义类型未定义”
PrivateSubCommand1_Click()DimExcelAppAsExcel.ApplicationDimExcelSheetAsObjectDimExcel...
Private Sub Command1_Click()
Dim ExcelApp As Excel.Application
Dim ExcelSheet As Object
Dim ExcelWorkbook As Object
Set ExcelApp = New Excel.Application
Set ExcelWorkbook = ExcelApp.Workbooks.Add
Set ExcelSheet1 = ExcelApp.Worksheets("sheet1")
Dim Sh As Object, rngStart As Range
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
Set Sh1 = ExcelSheet1
Set rngStart = Sh1.Range("A1")
With rngStart.Rows(1)
.Font.Bold = True
Sh1.Range("A1", "C1").Font.Color = RGB(112, 220, 21)
Sh1.Range("A1", "C1").Borders.LineStyle = xlContinuous
.Cells(, 1) = "桩 号"
.Cells(, 2) = "X(N)坐标"
.Cells(, 3) = "Y(E)坐标"
End With
End Sub 展开
Dim ExcelApp As Excel.Application
Dim ExcelSheet As Object
Dim ExcelWorkbook As Object
Set ExcelApp = New Excel.Application
Set ExcelWorkbook = ExcelApp.Workbooks.Add
Set ExcelSheet1 = ExcelApp.Worksheets("sheet1")
Dim Sh As Object, rngStart As Range
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
Set Sh1 = ExcelSheet1
Set rngStart = Sh1.Range("A1")
With rngStart.Rows(1)
.Font.Bold = True
Sh1.Range("A1", "C1").Font.Color = RGB(112, 220, 21)
Sh1.Range("A1", "C1").Borders.LineStyle = xlContinuous
.Cells(, 1) = "桩 号"
.Cells(, 2) = "X(N)坐标"
.Cells(, 3) = "Y(E)坐标"
End With
End Sub 展开
1个回答
展开全部
1:读取。
1、在VB中,建立一个FORM,在其上放置两个命令按钮,将Command1的Caption属性改为EXCEL,Command2的Caption属性改为End。然后在其中输入如下程序:
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Private Sub Command1_Click() '打开EXCEL过程
If Dir("D:\temp\excel.bz") = "" Then '判断EXCEL是否打开
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("D:\temp\bb.xls") '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = "abc" '给单元格1行驶列赋值
xlBook.RunAutoMacros (xlAutoOpen) 运行EXCEL中的启动宏
Else
MsgBox ("EXCEL已打开")
End If
End Sub
Private Sub Command2_Click()
If Dir("D:\temp\excel.bz") <> "" Then '由VB关闭EXCEL
xlBook.RunAutoMacros (xlAutoClose) '执行EXCEL关闭宏
xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
End If
Set xlApp = Nothing '释放EXCEL对象
End
End Sub
2、在D盘根目录上建立一个名为Temp的子目录,在Temp目录下建立一个名为"bb.xls"的EXCEL文件。
3、在"bb.xls"中打开Visual Basic编辑器,在工程窗口中点鼠标键选择插入模块,在模块中输入入下程序存盘:
Sub auto_open()
Open "d:\temp\excel.bz" For Output As #1 '写标志文件
Close #1
End Sub
Sub auto_close()
Kill "d:\temp\excel.bz" '删除标志文件
End Sub
4、运行VB程序,点击EXCEL按钮可以打开EXCEL系统,打开EXCEL系统后,VB程序和EXCEL分别属两个不同的应用系统,均可同时进行操作,由于系统加了判断,因此在VB程序中重复点击EXCEL按钮时会提示EXCEL已打开。如果在EXCEL中关闭EXCEL后再点EXCEL按钮,则会重新打开EXCEL。而无论EXCEL打开与否,通过VB程序均可关闭EXCEL。
2:写入。
Excel导入sql:
'引用ADO(Microsoft ActiveX Data Objects 2.X Library)
Private Sub Command2_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=dataBase001;Data Source=mySERVICE"
cn.CursorLocation = adUseClient
cn.Open
cn.Execute "select * into table4 from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=Yes;database=d:\temp\bb.xls;','select * from [Sheet1$]')"
cn.Close
Set cn=Nothing
End Sub
1、在VB中,建立一个FORM,在其上放置两个命令按钮,将Command1的Caption属性改为EXCEL,Command2的Caption属性改为End。然后在其中输入如下程序:
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Private Sub Command1_Click() '打开EXCEL过程
If Dir("D:\temp\excel.bz") = "" Then '判断EXCEL是否打开
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("D:\temp\bb.xls") '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = "abc" '给单元格1行驶列赋值
xlBook.RunAutoMacros (xlAutoOpen) 运行EXCEL中的启动宏
Else
MsgBox ("EXCEL已打开")
End If
End Sub
Private Sub Command2_Click()
If Dir("D:\temp\excel.bz") <> "" Then '由VB关闭EXCEL
xlBook.RunAutoMacros (xlAutoClose) '执行EXCEL关闭宏
xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
End If
Set xlApp = Nothing '释放EXCEL对象
End
End Sub
2、在D盘根目录上建立一个名为Temp的子目录,在Temp目录下建立一个名为"bb.xls"的EXCEL文件。
3、在"bb.xls"中打开Visual Basic编辑器,在工程窗口中点鼠标键选择插入模块,在模块中输入入下程序存盘:
Sub auto_open()
Open "d:\temp\excel.bz" For Output As #1 '写标志文件
Close #1
End Sub
Sub auto_close()
Kill "d:\temp\excel.bz" '删除标志文件
End Sub
4、运行VB程序,点击EXCEL按钮可以打开EXCEL系统,打开EXCEL系统后,VB程序和EXCEL分别属两个不同的应用系统,均可同时进行操作,由于系统加了判断,因此在VB程序中重复点击EXCEL按钮时会提示EXCEL已打开。如果在EXCEL中关闭EXCEL后再点EXCEL按钮,则会重新打开EXCEL。而无论EXCEL打开与否,通过VB程序均可关闭EXCEL。
2:写入。
Excel导入sql:
'引用ADO(Microsoft ActiveX Data Objects 2.X Library)
Private Sub Command2_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=dataBase001;Data Source=mySERVICE"
cn.CursorLocation = adUseClient
cn.Open
cn.Execute "select * into table4 from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=Yes;database=d:\temp\bb.xls;','select * from [Sheet1$]')"
cn.Close
Set cn=Nothing
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询