抗DDOS抗皱产品推荐主要厂家和抗皱产品推荐有哪些?

使用 Windows 窗体的多条相关记录填写 Excel 2003 工作表
MSDN Library
使用 Windows 窗体的多条相关记录填写 Excel 2003 工作表
发布日期 : 11/10/2005 |
更新日期 : 11/10/2005 Frank RiceMicrosoft Corporation 适用于:Microsoft Office Excel 2003Microsoft Office Access 2003Microsoft Visual Studio 2005 Tools for Microsoft Office System
摘要:学习如何在 Microsoft Office Excel 2003 工作簿中显示 Microsoft Windows 窗体和相关记录,以及如何将这些信息写入格式化工作表。本文还介绍一些方法,用于在一个数据集内导航这些相关数据表,根据一条选定记录返回相关记录。 下载 。 本页内容
简介 一些开发人员希望在 Microsoft Office Word 2003 和 Microsoft Office Excel 2003 后端编写代码来解决任意数目的业务方案,Microsoft Visual Studio 2005 Tools for Microsoft Office System (Visual Studio 2005 Tools for Office) 为这类开发人员提供了大量工具。例如,您也许想编写一种业务逻辑来管理 Excel 工作表中的预算数据,或者使用通过 Web 服务获得的销售数据填写 Word 文档中的表。如果使用 Microsoft Visual Studio .NET,那么您就能利用 Visual Studio 2005 Tools for Office 解决这样甚至更多的方案。 本文,将构建一个面向 Excel 的解决方案。构建本文概述的应用程序时,将使用 Visual Studio .NET 和 Microsoft Office System 中多个领域的技术,包括 Visual Studio Server Explorer、Microsoft Windows 窗体、OLE DB 数据访问和 Microsoft Office 自动化。 特别地,本文在演练方案时还描述以下几个步骤:
在 Visual Studio .NET 中创建一个项目
将一个 Windows 窗体添加到项目中
在 Windows 窗体中显示客户数据
根据用户的选择显示相关记录
按定单明细填写一个工作表
此外,本文还描述一些相关任务:
根据相关数据添加数据表表达式列
编辑相关数据的合计信息
创建一个格式化工作表
测试应用程序
听起来有趣吗?那就让我们开始吧。
在 Visual Studio .NET 中创建 Excel 工作簿项目 在本步骤中,将创建一个 Excel 工作簿项目。
在 Visual Studio .NET 中创建一个项目
在 Visual Studio .NET 中,单击 File 菜单,移动到 New,然后单击 Project,显示 New Project 对话框。
在 Project Types 窗格中,展开 Microsoft Office System Projects,然后选择 Visual Basic Projects。
在 Templates 窗格中,选择 Excel.Workbook。
命名项目 OfficeExcelRelatedRecordInsert,然后单击 OK。
将出现 Microsoft Office Project Wizard。
确保选中 Create New Document,然后单击 Finish。
Visual Studio .NET 在特定位置创建一个 Excel 工作簿,并在解决方案资源管理器中添加 OfficeExcelRelatedRecordInsert 项目。
在项目中添加 Windows 窗体 在本步骤中,将创建一个 Windows 窗体来选择和显示相关记录。
创建一个 Windows 窗体来选择和显示相关记录
在解决方案资源管理器中,选择项目 OfficeExcelRelatedRecordInsert。
在 Project 菜单上,单击 Add Windows Form。
命名窗体 SelectRecords.vb,然后单击 Open。
新窗体在设计器中打开,SelectRecords.vb 文件添加到解决方案资源管理器中。
在 File 菜单上,选择 Save All,保存项目。
连接数据库 该数据库连接允许您在集成开发环境内与数据源进行通讯。
在 Server Explorer 中连接 Northwind 示例数据库
在 Visual Studio .NET 中,选择 View 菜单,单击 Server Explorer,建立一个与 Northwind 示例数据库的数据连接。
在 Tools 菜单上,选择 Connect to Database。
在 Data Link Properties 对话框中,单击 Provider 选项卡,选择 Microsoft Jet 4.0 OLE DB Provider,然后单击 Next。
在 Data Link Properties 对话框中,单击 Connection 选项卡,提供一个指向 Northwind 示例数据库的路径。数据库通常安装在 C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb。
输入登录到数据库所需的用户名和密码。
单击 OK,建立数据连接。
Data Link Properties 对话框关闭,新数据连接出现在数据连接节点下方,以其访问的服务器和数据库命名。
注 存储连接字符串的详细信息(例如服务器名、用户名和密码)能暗示应用程序的安全性。控制访问数据库的更安全的方法是使用 Windows 集成安全性。请搜索 Visual Studio 帮助文件,查找更多有关 Database Security 的信息。
不断展开 Server Explorer 中的 Northwind 数据连接,直到看见所有表为止。
创建数据适配器和连接 本步骤将创建连接和数据适配器,用于连接应用程序和数据源并在它们之间交换数据。
创建数据适配器和连接
将 Customers 表从 Server Explorer 拖放到窗体上。组件栏中将显示连接和数据适配器。
注 如果提示在连接字符串中包含密码,那么您应该意识到其中涉及的安全隐患,因为密码是以纯文本形式保存的。建议不要包含密码。在连接数据库时会提示您输入一个用户名和密码。
右键单击 OleDbConnection1,然后单击 Properties。
在属性窗口顶部的下拉列表中,选择 OleDbConnection1,将 Name 属性设置为 dcNorthwind。
重复前面的步骤设置 OleDbDataAdapter1,将 Name 属性设置为 daCustomers。
将 Orders 表从 Server Explorer 拖放到窗体。组件栏中将显示第二个数据适配器。
选择新的数据适配器,并将 Name 属性设置为 daOrders。
生成数据集 接下来,通过刚添加到窗体的数据适配器,生成一个包含 Customers 表和 Orders 表的数据集。
生成包含相关数据表的数据集
在 Data 菜单中,选择 Generate Dataset。出现 Generate Dataset 对话框。
单击 New,将数据集命名为 dsNorthwind。
选定表 Customers 和表 Orders。
选中 Add this dataset to the designer 复选框,然后单击 OK。
解决方案资源管理器的项目中将添加一个名为 dsNorthwind.xsd 的文件,并且组件栏中将显示该数据集的一个实例。
创建表关系 生成数据集并不能自动创建数据集中各表之间的关系。可使用 XML 设计器以编程方式或可视方式创建关系。本示例使用了 XML 设计器。
创建 Customers 表和 Orders 表之间的关系
在解决方案资源管理器中双击 dsNorthwind.xsd 文件。文件将在XML 设计器中打开。
在 View 菜单上,单击 Toolbox,然后单击 XML Schema 选项卡。将一个 Relation 拖放到 Orders 表中。
在 Edit Relation 对话框中设置下列值:
表 1. 关系值
CustomersOrders
Key Fields
CustomerID
Foreign Key Fields
CustomerID
单击 OK 创建关系并关闭对话框。
保存项目。
生成窗体 此应用程序使用组合框、列表框和 RTF 文本框来选择和显示数据。此外,还使用一个按钮将记录和相关记录添加到工作表。
图 1. SelectRecords 窗体
添加选择和显示数据的控件
在解决方案资源管理器中,右键单击 SelectRecords.vb,然后选择 View Designer。
在窗体的左半部分,添加一个 ListBox 控件,并将其 Name 属性设置为 lbOrders。
在窗体的右半部,添加一个 RichTextBox 控件,并将其 Name 属性设置为 rtbDetails。
在列表框上方,添加一个 ComboBox 控件,并将其 Name 属性设置为 cbCustomers。然后,可能要调整控件的长度以使其适合公司名称的显示。
在列表框和 RTF 文本框下方,添加一个 Button 控件,将其 Name 属性设置为 btnDisplay,并将其 Text 属性设置为 Display Records。
本文后面,将为该按钮添加代码。
单击以返回 Windows 窗体设计器,并在 Properties 窗口中对组合框应用下列设置。
表 2. 组合框设置
DataSource
DsNorthwind1
DisplayMember
ValueMember
Customers.CustomerID
注 本文后面,将为组合框和其他控件添加代码。
生成工作簿 现在,请将注意力转移到工作簿。
在应用程序启动时显示窗体
在解决方案资源管理器中,双击 ThisWorkbook.vb,打开代码编辑器中的工作簿。
在 ThisWorkbook_Open 事件处理程序中添加代码,声明一个窗体变量,然后显示该窗体。使用
关键字作为 SelectRecords 过程的一个参数,创建一个对当前 OfficeCodeBehind 对象的引用。
Dim inputForm As New SelectRecords(Me)
inputForm.Show()
创建一个名为 echoStringToCell 的方法,它将文本写入工作表。该方法从窗体调用,并将记录数据传递到工作表中的适当单元格。
Public Sub EchoStringToCell(ByVal str As String, _
ByVal row As Integer, ByVal column As Integer)
Dim sheet1 As Excel.Worksheet
' Start Excel to the worksheet and display the records.
sheet1 = CType(Me.thisWorkbook.Worksheets(1), Excel.Worksheet)
CType(sheet1.Cells.Item(row, column), Excel.Range).Value2 = str
保存项目并返回到窗体的代码窗口。
初始化 Windows 窗体
在代码窗口的 Windows 窗体代码文件(公共类 SelectRecords 语句)前,添加下列语句。这些语句允许在类的内部添加对对象的引用,而不必使用完全限定名:
Imports System
Imports System.IO
在 Inherits System.Windows.Forms.Form 语句后,为 OfficeCodeBehind 声明一个类级别的变量,它使 Windows 窗体的构造函数 Sub New() 在初识化窗体时包含一个对当前工作簿的引用:
Private excelCode As OfficeCodeBehind
打开 Windows Form Designer generated code 区域,并将 Sub New() 构造函数的默认代码替换为下列代码:
Public Sub New(ByVal targetExcelCode As OfficeCodeBehind)
MyBase.New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Adds any initialization after the InitializeComponent() call.
Me.excelCode = targetExcelCode
关闭 Windows Form Designer generated code 区域。
用数据填写表格 下一步建立组合框,并将代码添加到应用程序以实现用数据填写表格。
用数据填写数据集 (dsNorthwind1) 中的 Customers 表和 Orders 表
双击窗体中的一块空白区域,为 SelectRecords_Load 事件创建事件处理程序。
将默认代码替换为以下代码:
Private Sub SelectRecords_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
' Create an expression column named Total in the Order_Details
'Dim dcTotal As DataColumn = New DataColumn("Total")
'dcTotal.DataType = System.Type.GetType("System.Decimal")
'dcTotal.Expression = "UnitPrice * Quantity"
'DsNorthwind1.Order_Details.Columns.Add(dcTotal)
' Create an expression column named OrderTotal in the Orders
'Dim dcOrderTotal As DataColumn = New DataColumn("OrderTotal")
'dcOrderTotal.DataType = System.Type.GetType("System.Decimal")
'dcOrderTotal.Expression = "Sum(Child.Total)"
'DsNorthwind1.Orders.Columns.Add(dcOrderTotal)
' Turn off constraints in the dataset.
DsNorthwind1.EnforceConstraints = False
' Fill the tables with data.
'daOrderDetails.Fill(DsNorthwind1)
'daProducts.Fill(DsNorthwind1)
daOrders.Fill(DsNorthwind1)
daCustomers.Fill(DsNorthwind1)
' Turn constraints back on.
DsNorthwind1.EnforceConstraints = True
保存项目。
按下 F5 键以运行应用程序。启动 Excel,这时窗体的组合框将包含一个公司名称的列表。
关闭窗体和工作簿。
导航相关记录 在 Microsoft Visual Basic .NET 中使用关系数据库时,比较常见的任务之一是在参与建立一对多关系的数据集中的两个表之间访问数据。完成这项任务的一种方法是使用 DataRelation 对象逐一浏览 DataSet 中的 DataTable。在给定一个相关 DataTable 的 DataRow 时,这种方法允许检索 DataTable 中的所有相关 DataRow 对象。另一种做法是使用 GetChildRows 或 GetParentRow 方法。在选择一个数据行后,可通过调用 GetChildRows 或 GetParentRow 方法并向该数据行传递适当的数据关系来返回它的相关记录。例如,在 Customers 表和 Orders 表之间建立 DataRelation 后,可通过 DataRow.GetChildRows 检索某一位客户的所有定单行。GetChildRows 方法返回一个 DataRow 对象数组中的数据,而 GetParentRow 方法只返回单个数据行。本文采用了后一种方法。
创建获取选定客户的定单的事件处理程序
单击 SelectRecords.vb [Design] 选项卡以查看 View Designer。
双击组合框为 SelectedIndexChanged 事件创建事件处理程序。
将默认代码替换为以下代码:
Private drSelectedCustomer As DataRow
Private Sub cbCustomers_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles cbCustomers.SelectedIndexChanged
' Declare a string to hold the selected customer's CustomerID.
Dim SelectedCustomerID As String
SelectedCustomerID = cbCustomers.SelectedValue.ToString()
' Declare a data row to hold the selected customer's record.
drSelectedCustomer = DsNorthwind1.Customers.FindByCustomerID _
(SelectedCustomerID)
' Declare an array of data rows to hold the related records.
Dim draOrders As DataRow()
draOrders = drSelectedCustomer.GetChildRows("CustomersOrders")
' Display the length of the array (number of orders)
' and the CustomerID in the form caption.
Me.Text = draOrders.Length.ToString() & " orders for " & _
SelectedCustomerID
保存项目。
按下 F5 键运行此应用程序。
选择另一个客户,并检查窗体标题。
将显示选定客户的定单总数及其 Customer ID。
关闭窗体和工作簿。
显示相关记录 执行了前一小节中的步骤后,现在您得到了选定客户的相关记录(存储在数据行数组中)。在本节中,您将显示这些记录以便与用户进行交互。应用程序将逐一访问 GetChildRows 方法返回的数据行数组中的数据,并将每个相关记录的 OrderID 字段作为单独的项添加到列表框中。
在列表框中显示相关记录
在解决方案资源管理器中,右键单击 SelectRecords,然后单击 View Code。
在前面步骤中创建的组合框的 SelectedIndexChanged 事件处理程序中,添加以下代码(在处理程序已有代码的后面):
' When the selected customer changes, clear the list of orders.
lbOrders.Items.Clear()
rtbDetails.Text = ""
' Add the OrderID of each related order to the list box.
Dim drOrder As DataRow
For Each drOrder In draOrders
lbOrders.Items.Add(drOrder("OrderID"))
保存项目。
运行此应用程序。
列表框中将显示定单列表。在组合框中选择另一个客户,定单列表将更新。
关闭窗体和工作簿。
导航多个表中的相关记录 导航三个或更多的表与导航两个表差别不大。为了说明在此应用程序中如何导航两个以上的表,请在 dsNorthwind 数据集中添加 OrderDetails 表和 Products 表。在列表框中选定一个定单后,该定单的详细信息即显示在 RTF 文本框中。 在下列步骤中,将在现有数据集中添加另外两个表,并创建其他的 DataRelation 对象,从而将所有表格联系到一起。
在 dsNorthwind 数据集中添加 OrderDetails 表和 Products 表
在解决方案资源管理器中,右键单击 SelectRecords.vb,然后选择 View Designer。
将 Order Details 表从 Server Explorer 拖放到窗体。组件栏中将显示一个新的数据适配器。
选定这个新的数据适配器,并将其 Name 属性设置为 daOrderDetails。
将 Products 表从 Server Explorer 拖放到窗体上。组件栏中将显示一个新的数据适配器。
选定这个新的数据适配器,并将其 Name 属性设置为 daProducts。
关闭 Server Explorer。
新表只是添加到了窗体,因此每次添加额外的表时,都必须重新生成数据集。
重新生成 dsNorthwind 数据集
在 Data 菜单上,选择 Generate Dataset。
单击 Existing,然后选择 dsNorthwind 数据集。
选择所有四个表(Customers、Orders、OrderDetails 和 Products)。
清除 Add this dataset to the designer 复选框,然后单击 OK。将重新生成带有附加表的数据集。
注 如果出现一个对话框,其中说明“The file has been modified outside of the source editor.Do you want to reload it?”(文件已在源编辑器之外进行了修改。是否要重新加载?),请单击 Yes。生成数据集并不能自动创建数据集中各表之间的关系。
在解决方案资源管理器中,双击 dsNorthwind.xsd 文件。文件将在 XML 设计器中打开。
在 View 菜单上,单击 Toolbox,然后单击 XML Schema 选项卡。将一个 Relation 对象拖放到 OrderDetails 表上。
在 Edit Relation 对话框中设置下列值:
表 3. 关系值
OrdersOrderDetails
OrderDetails
Key Fields
Foreign Key Fields
单击 OK 以创建关系并关闭对话框。
在 View 菜单上,单击 Toolbox,然后单击 XML Schema 选项卡。将一个 Relation 对象拖放到 OrderDetails 表上。
在 Edit Relation 对话框中设置下列值:
表 4. 关系值
ProductsOrderDetails
OrderDetails
Key Fields
Foreign Key Fields
单击 OK 以创建关系并关闭对话框。
保存项目。
OrderDetails 表和 Products 表被添加到数据集中,但是您仍然需要添加代码以便在运行时用数据填写这些表。
用数据填写表
在解决方案资源管理器中,右键单击 SelectRecords.vb,然后单击 View Code。
在 SelectRecords_Load 事件处理程序中,取消注释下列语句,这些语句位于用数据填写表 的注释与 daOrders.Fill(dsNorthwind1) 行之间:
daOrderDetails.Fill(dsNorthwind1)
daProducts.Fill(dsNorthwind1)
填写 RTF 文本框 在本小节内,要为项目添加代码,以便在列表框中选定某个定单时,可在 RTF 文本框中显示所有定单明细。 以下代码将基于列表框中选定的定单调用 GetChildRows 方法。OrderDetails 表中所有的相关记录都将分配给名为 draOrderDetails 的数据行数组。每个数据行的内容均显示在 RTF 文本框中。
设置 RTF 文本框以显示所有定单明细
在解决方案资源管理器中,右键单击 SelectRecords.vb,然后单击 View Designer。
双击列表框为列表框 lbOrders 的 SelectedIndexChanged 事件创建事件处理程序。
将默认代码替换为以下代码:
Private details As String
Private OrderTotals As String = ""
Private Sub lbOrders_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles lbOrders.SelectedIndexChanged
' Clear the rich text box when a new order is selected.
rtbDetails.Clear()
' Declare an integer to hold the selected OrderID.
Dim SelectedOrderID As Integer
' Cast the selected item as an integer.
SelectedOrderID = CType(lbOrders.SelectedItem, Integer)
' Declare a data row to hold the selected Order record.
Dim drSelectedOrder As DataRow
drSelectedOrder = _
DsNorthwind1.Orders.FindByOrderID(SelectedOrderID)
' Declare an array of data rows to hold the related records.
Dim draOrderDetails() As DataRow
draOrderDetails = _
drSelectedOrder.GetChildRows("OrdersOrderDetails")
' Create the OrdersTotal entry.
OrderTotals = "Order Total: " & String.Format("{0:c}", _
DsNorthwind1.Orders.FindByOrderID(CType(lbOrders.SelectedItem, _
Integer))("OrderTotal")) & ControlChars.CrLf
details = ""
Dim drDetails As DataRow
Dim dcDetails As DataColumn
' Cycle through the order detail entries by row
' and then by column.
For Each drDetails In draOrderDetails
For Each dcDetails In drDetails.Table.Columns
details &= dcDetails.ColumnName & ": "
If dcDetails.ColumnName.ToString = "Total" Then
details &= String.Format("{0:c}", drDetails(dcDetails))
details &= drDetails(dcDetails).ToString()
details &= ControlChars.CrLf
details &= ControlChars.CrLf
' Display the results in the rich text box.
rtbDetails.Text = details
在 SelectRecords_Load 事件处理程序中,取消注释下列语句:
Dim dcTotal As DataColumn = New DataColumn("Total")
dcTotal.DataType = System.Type.GetType("System.Decimal")
dcTotal.Expression = "UnitPrice * Quantity"
DsNorthwind1.Order_Details.Columns.Add(dcTotal)
这段代码说明如何创建包含数学运算结果,经过计算的列。如上所示,可计算从现有列获得的值。代码创建了一个名为 Total 的列,将它添加到 OrderDetails 表,它包含表达式 UnitPrice * Quantity 返回的值,该值是定单总计美元值。
取消注释下列语句:
Dim dcOrderTotal As DataColumn = New DataColumn("OrderTotal")
dcOrderTotal.DataType = System.Type.GetType("System.Decimal")
dcOrderTotal.Expression = "Sum(Child.Total)"
DsNorthwind1.Orders.Columns.Add(dcOrderTotal)
这段代码说明如何使用表达式列中的合计信息。代码在 Orders 表中添加了一个名为 OrderTotal 的新列。此列使用 Sum 功能,根据在列表框 (lbOrders) 中选定的定单返回所有子定单明细记录的总计美元值。本文后面的工作表将使用该值。
保存项目。
运行此应用程序。
在列表框中选择一个定单,其定单明细将显示在 RTF 文本框中。请注意每个定单下方的 Totals(总计)项。
在列表框中选择另一个定单。RTF 文本框中的定单明细将被更新。
导航多对多关系 在多对多关系中,一个表中的多条记录在另一个表中可以有多条匹配记录。多对多关系通过使用一个链接表建立,该表只包含要链接的两个表的关键字字段。这种多对多关系的建立允许表 A 的一条记录与表 B 的多条记录链接,反之亦然,也就是将链接表作为一个桥梁使用。 在 Northwind 数据库中,Orders 表和 Products 表就是以这种方式相联系的。因为有些定单可能包含很多产品,而有些产品又在很多定单中销售。所以这两个表通过 OrderDetails 表连接,该表通过源自每个表的相同列使数据联系起来。 为了导航 Order Details 表和 Products 表之间的多对多关系,您可以根据 Order Details 表中的单条记录来访问产品表,产品表将返回产品名称并将其显示在定单明细表中。 可使用 GetParentRow 方法从 Products 表访问产品名称。调用 GetParentRow 方法返回单个数据行,而调用 GetChildRows 方法则返回一个行数组。
从定单明细记录中获取产品名称
在解决方案资源管理器中,右键单击 SelectRecords,然后单击 View Code。
在列表框 (lbOrders) 的 SelectedIndexChanged 事件处理程序中,将下列代码添加到 For Each 语句之间:
details &= "Product name: " & _
CType(drDetails.GetParentRow("ProductsOrderDetails") _
("ProductName"), String) & ControlChars.CrLf
保存项目。
运行此应用程序。
在列表框中选择一个定单,RTF 文本框中将显示产品名称和详细信息。现在窗体中显示来自所有四个表的相关数据。
在列表框中选择另一个定单。RTF 文本框中的定单明细将被更新。
关闭窗体和工作簿。
格式化工作表 下一步,格式化工作表以正确显示客户信息和相关记录。本节提供的按钮代码包含硬编码值,这样做是为了使按钮适合预先格式化的工作表。可能需要改变这些值以使其适合自定义的工作表。
格式化与本项目相关的工作表。
定位到保存 OfficeExcelRelatedRecordInsert 项目的位置。
打开 OfficeExcelRelatedRecordInsert.xls 工作簿。
如果显示 Windows 窗体,则将其关闭。
高亮显示列 A 到列 E 之间的区域。
在 Format 菜单上,单击 Cells。
在 Alignment 选项卡上,单击 Vertical 下拉列表,并选择 Top。单击 OK。
高亮显示单元格 A1 至单元格 E2 之间的区域。
在 Format 菜单上,单击 Cells。
在 Patterns 选项卡上,为高亮区域选择一种颜色。
添加要进行的其他格式化形式。关闭并保存工作簿。
显示工作表中的数据 现在,将之前添加到窗体的代码添加到按钮中,以显示工作表中的记录和相关数据。
双击按钮,打开添加了按钮的 Click 事件处理程序的代码文件。
将默认代码替换为以下代码:
Private Sub btnDisplay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDisplay.Click
' Some of these items are hard-coded for display in a preformatted
' form in specific columns/rows. You can change these settings
' to fit your own preformatted forms.
Dim row As Integer = 1
Dim column As Integer = 1
' Add the contact name to the worksheet.
Me.excelCode.EchoStringToCell("Contact Name: " & vbCrLf & _
drSelectedCustomer.ItemArray(3), row, column)
column += 1
' Add the customerID to the worksheet.
Me.excelCode.EchoStringToCell("Customer ID: " & vbCrLf & _
drSelectedCustomer.ItemArray(6), row, column)
column += 1
' Add the company name to the worksheet.
Me.excelCode.EchoStringToCell("Company Name: " & vbCrLf & _
drSelectedCustomer.ItemArray(2), row, column)
column += 1
' Add the current date to the worksheet.
Me.excelCode.EchoStringToCell("Date: " & Date.Today, 1, column)
Dim sr As New StringReader(details)
Dim tempStr As String = ""
Dim results As String = ""
tempStr = sr.ReadLine
If tempStr = "" Then 'end of record.
'Display the record.
Me.excelCode.EchoStringToCell(results, row, column)
' Clear the results variable to get ready for the next read.
results = ""
' Check to see if we are at the end of the records.
tempStr = sr.ReadLine
If tempStr = "" Then 'end of orders.
results = ""
' If we are at the end of records, this will add a line feed,
' carriage return to the blank line which we can then test for.
results &= tempStr & vbCrLf
tempStr = ""
Loop Until results = vbCrLf
column += 1
' Display the order totals.
Me.excelCode.EchoStringToCell(OrderTotals, row, column)
' Close the form and release its resources.
Me.Dispose()
保存项目。
运行此应用程序。
按步骤显示 RTF 文本框中的相关记录。
单击 Display Record 按钮。
格式化工作表中将显示的记录信息(例如,联系人姓名和公司名称)以及相关定单和总计定单量。
图 2. 格式化工作表显示的客户 AROUT 的记录
在产品环境中,如果您满意自己的工作表且项目编译没有出错,那么就可以开始部署应用程序了。除此之外,仔细考虑为项目添加错误处理代码也很重要。有关部署的更多信息,请参阅 。有关错误处理的更多信息,请参阅 Visual Studio 主题 。
小结 Visual Studio 2005 Tools for Office 提供了很多工具和抽象,使您能够利用 Microsoft .NET Framework 中高效的增强功能,通过 Visual Basic .NET 来扩展 Word 和 Excel。正如本文所呈现的那样,利用好这些工具便可摆脱不必要的工作,以周密、循序渐进的方式全情投入应用程序的逻辑开发。
其他资源 有关本文介绍的工具和技术的更多信息,请参阅下列资源:
Programming with Office
Build Office Solutions That Use Managed Code Extensions
Getting Started
Automating Office Applications
Deploying Office Solutions That Use Managed Code Extensions
此页面有帮助吗?
更多反馈?
1500 个剩余字符
我们非常感谢您的反馈。

我要回帖

更多关于 抗菌产品 的文章

 

随机推荐