Edraw Office Viewer component教程(二):将Excel嵌入VB 6并使之自动化
来源:互联网 发布:淘宝出货单表格 编辑:程序博客网 时间:2024/05/17 03:35
Edraw office viewer component对于开发人员来说是一个将Microsoft Excel电子表格嵌入到vb.net应用程序中的简单可靠的解决方案。
使用Edraw office viewer component,vb开发人员可以轻松地将一个现有的Excel电子表格嵌入到表单中,然后进行自动化。在加载表单时,开发人员可以使用Open方法从硬盘驱动器或服务器中加载* .xls、* .xlsx、* .xslm或* .cvs文件,并在窗口的某个区域中打开。Excel文件可以是只读、完全编辑或禁止保存等状态。开发人员还可以使用excel组件轻松实现excel自动化。
本文将演示如何逐步将Excel嵌入到VB中。如果你没有officeviewer.ocx文件,请先安装。
Office viewer component - 支持Word、Excel、PowerPoint、Visio和Project的vb6、vb.net项目
在VB 6中嵌入Excel组件
启动Visual Basic并创建一个新的标准项目。默认情况下创建Form1。
在“工具箱”面板中,右键单击空白区域。然后单击Component...
在“工具箱”面板中,右键单击空白区域。然后单击Component...
在弹出的对话框中,查看Edraw Office Viewer Component。
单击确定按钮。
Office Viewer Component便已添加到“工具栏”窗口中。
Office Viewer Component便已添加到“工具栏”窗口中。
切换到VB 6窗口,然后添加组件。
该组件包括许多自定义Excel窗口的方法、事件和属性。
开发人员可以使用以下代码在Form_Load事件中打开一个excel文件。
Private Sub Form_Load()
EDOffice1.OpenFileDialog'EDOffice1.OpenWord
“d:\ test.xlsx”
'EDOffice1.Open“d:\ test.xls”,“Excel.Application”
End Sub
EDOffice1.OpenFileDialog'EDOffice1.OpenWord
“d:\ test.xlsx”
'EDOffice1.Open“d:\ test.xls”,“Excel.Application”
End Sub
为了保护Excel工作表不被最终用户修改,开发人员需要在DocumentOpened事件中添加以下代码。
Private Sub EDOffice_DocumentOpened()
EDOffice1.ProtectDoc 1'XlProtectTypeNormal
End Sub
EDOffice1.ProtectDoc 1'XlProtectTypeNormal
End Sub
一些包含excel自动化的方法可用于创建或修改Excel数据。
bool ExcelAddWorkSheet(long Index);
bool ExcelDeleteWorkSheet(long Index);
bool ExcelActivateWorkSheet(long Index);
长ExcelGetWorkSheetCount();
bool ExcelSetCellValue(long Column,long Row,BSTR Value);
BSTR ExcelGetCellValue(长列,长行);
bool ExcelSetRowHeight(long Row,double Height);
bool ExcelSetColumnWidth(long Column,double Width);
afx_msg bool ExcelDeleteRow(long Row);
bool ExcelDeleteColumn(long Column);
bool ExcelInsertRow(long Row);
bool ExcelInsertColumn(long Column);
bool ExcelInsertPageBreakInRow(long Row);
bool ExcelInsertPageBreakInColumn(long Column);
bool ExcelCopyToClipboard();
bool ExcelPasteStringToWorksheet(BSTR bstText);
bool ExcelDeleteWorkSheet(long Index);
bool ExcelActivateWorkSheet(long Index);
长ExcelGetWorkSheetCount();
bool ExcelSetCellValue(long Column,long Row,BSTR Value);
BSTR ExcelGetCellValue(长列,长行);
bool ExcelSetRowHeight(long Row,double Height);
bool ExcelSetColumnWidth(long Column,double Width);
afx_msg bool ExcelDeleteRow(long Row);
bool ExcelDeleteColumn(long Column);
bool ExcelInsertRow(long Row);
bool ExcelInsertColumn(long Column);
bool ExcelInsertPageBreakInRow(long Row);
bool ExcelInsertPageBreakInColumn(long Column);
bool ExcelCopyToClipboard();
bool ExcelPasteStringToWorksheet(BSTR bstText);
使用Visual Basic自动化操作Excel
单击项目,然后单击引用。将显示“引用”对话框。向下滚动列表,找到Microsoft Excel object库(Microsoft Excel xx.x Object库),然后选择引用Excel。如果列表中没有正确的Excel Object库,请确认你是否安装了合适的Excel版本。
单击“确定”关闭“引用”对话框。
将CommandButton添加到Form1。
在Form1的代码窗口中,插入以下代码:
Private Sub Command1_Click()
Dim oXL As Excel.Application
Set oXL = EDOffice1.GetApplication()
Dim oWB As Excel.Workbook
Set oWB = EDOffice1.ActiveDocument()
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Set oSheet = oWB.ActiveSheet
oSheet.Cells(1, 1).Value = "First Name"
oSheet.Cells(1, 2).Value = "Last Name"
oSheet.Cells(1, 3).Value = "Full Name"
oSheet.Cells(1, 4).Value = "Salary"
' Format A1:D1 as bold, vertical alignment = center.
With oSheet.Range("A1", "D1")
.Font.Bold = True
.VerticalAlignment = xlVAlignCenter
End With
' Create an array to set multiple values at once.
Dim saNames(5, 2) As String
saNames(0, 0) = "John"
saNames(0, 1) = "Smith"
saNames(1, 0) = "Tom"
saNames(1, 1) = "Brown"
saNames(2, 0) = "Sue"
saNames(2, 1) = "Thomas"
saNames(3, 0) = "Jane"
saNames(3, 1) = "Jones"
saNames(4, 0) = "Adam"
saNames(4, 1) = "Johnson"
' Fill A2:B6 with an array of values (First and Last Names).
oSheet.Range("A2", "B6").Value = saNames
' Fill C2:C6 with a relative formula (=A2 & " " & B2).
Set oRng = oSheet.Range("C2", "C6")
oRng.Formula = "=A2 & "" "" & B2"
' Fill D2:D6 with a formula(=RAND()*100000) and apply format.
Set oRng = oSheet.Range("D2", "D6")
oRng.Formula = "=RAND()*100000"
oRng.NumberFormat = "$0.00"
' AutoFit columns A:D.
Set oRng = oSheet.Range("A1", "D1")
oRng.EntireColumn.AutoFit
oXL.UserControl = True
End Sub
Set oXL = EDOffice1.GetApplication()
Dim oWB As Excel.Workbook
Set oWB = EDOffice1.ActiveDocument()
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Set oSheet = oWB.ActiveSheet
oSheet.Cells(1, 1).Value = "First Name"
oSheet.Cells(1, 2).Value = "Last Name"
oSheet.Cells(1, 3).Value = "Full Name"
oSheet.Cells(1, 4).Value = "Salary"
' Format A1:D1 as bold, vertical alignment = center.
With oSheet.Range("A1", "D1")
.Font.Bold = True
.VerticalAlignment = xlVAlignCenter
End With
' Create an array to set multiple values at once.
Dim saNames(5, 2) As String
saNames(0, 0) = "John"
saNames(0, 1) = "Smith"
saNames(1, 0) = "Tom"
saNames(1, 1) = "Brown"
saNames(2, 0) = "Sue"
saNames(2, 1) = "Thomas"
saNames(3, 0) = "Jane"
saNames(3, 1) = "Jones"
saNames(4, 0) = "Adam"
saNames(4, 1) = "Johnson"
' Fill A2:B6 with an array of values (First and Last Names).
oSheet.Range("A2", "B6").Value = saNames
' Fill C2:C6 with a relative formula (=A2 & " " & B2).
Set oRng = oSheet.Range("C2", "C6")
oRng.Formula = "=A2 & "" "" & B2"
' Fill D2:D6 with a formula(=RAND()*100000) and apply format.
Set oRng = oSheet.Range("D2", "D6")
oRng.Formula = "=RAND()*100000"
oRng.NumberFormat = "$0.00"
' AutoFit columns A:D.
Set oRng = oSheet.Range("A1", "D1")
oRng.EntireColumn.AutoFit
oXL.UserControl = True
End Sub
按F5运行项目。Excel将自动填充excel单元格。
自动化Excel时要注意的一点是Excel Macro Recorder。它仍然在Word 2007中,但隐藏在开发者功能区栏。
以上就是本次教程的全部内容,接下来会有更多相关教程,敬请关注!您也可以在评论者留下你的经验和建议。
0 0
- Edraw Office Viewer component教程(二):将Excel嵌入VB 6并使之自动化
- Edraw Office Viewer component教程(一):将Microsoft Word嵌入VB.NET并使之自动化
- Edraw Office Viewer component教程(三):将MS Word、Excel、PowerPoint嵌入到WPF应用程序中
- Edraw Office Viewer Component
- 将Edraw Office Viewer Component添加到基于应用程序的对话框中
- vb教程:将Edraw Office ActiveX控件添加到Visual Basic 6.0项目
- VC之Office自动化开发(二)
- VC之Office自动化开发(二)
- VC之Office自动化开发(二)
- VC++中通过自动化将Excel嵌入程序中
- 如何使用 MFC 嵌入并自动化 Microsoft Excel 工作表
- 如何使用 MFC 嵌入并自动化 Microsoft Excel 工作表
- 如何使用 MFC 嵌入并自动化 Microsoft Excel 工作表
- 如何使用 MFC 嵌入并自动化 Microsoft Excel 工作表
- vb与office的数据交互之vb导出Excel
- Excel嵌入网页(二)
- 运用Edraw为WPF应用程序嵌入Office文档的方法总结
- 运用Edraw为WPF应用程序嵌入Office文档的方法总结
- linux下学习正则的一些小结
- 大都会系统MetLife小记
- java实现重建二叉树
- Python进阶之装饰器
- Ubuntu下安装MySQL及简单操作
- Edraw Office Viewer component教程(二):将Excel嵌入VB 6并使之自动化
- win10+anaconda3-4.3.1+tesorflow1.0安装过程(配置Spyder)
- 港中大教授James招收机器学习理论方向全奖博士生
- 获取当年年份 月份判断星期几 如何写个程序计算出每个月的第一个星期一对应的日期
- 【MATLAB图像处理2】 直方图的均衡化(附源码)
- java两个栈实现队列
- vue 搭建
- 使用flex box(弹性盒子模型)进行页面布局的注意事项
- 远程linux数据库时无法正常连接的解决方法