在Microsoft Office System中使用ADO.NET数据集(五)

** 将 ** ** DataSet ** ** 作为 ** ** XML ** ** 导入到 ** ** Excel ** ** 工作表 ** ** **

Microsoft Office Excel 2003 为 XML 提供了非常强大的支持 , 它允许你将工作簿保存为 XML 或者将 XML 数据源导入到工作簿。 由于DataSet天生就序列化为XML,所以你可以非常容易的将它的数据导入到Excel中。导入步骤需要XML架构,这样Excel才能够将数据映射到工作簿的适当单元格,而DataSet自动提供了这个功能。事实上,这就是本文前面描述的 ** GetDataSet ** 方法的结尾处所添加的代码的功能,这些代码将 DataSet 对象的数据和架构写到磁盘上。

If bSaveSchema Then

'Places the file in this app's bin directory

ds.WriteXmlSchema("Customers.xsd")

ds.WriteXml("Customers.xml")

End If

这段代码在应用程序的 bin 文件夹创建 XML 和架构文件。图 5 显示 XML 文件的结果。

** 图 ** ** 5 ** ** : ** ** 使用 ** ** DataSet ** ** 的 ** ** WriteXml ** ** 方法生成的 ** ** Customers.xml ** ** 文件 **

图 6 显示 XML 架构 , 也是在 Internet Explorer 显示。

** 图 ** ** 6 ** ** : ** ** 使用 ** ** DataSet ** ** 的 ** ** WriteXmlSchema ** ** 方法生成的 ** ** Customers. xsd ** ** 文件 **

这项技术也可以用在 Excel 中的一个新特性上 , 这个对象叫做 ** ListObject ** , 它是一个列表结构的新类型 , 本质上它提供数据的完整视图。 结果列表显示在图7中。你可以单击标题行的下拉列表对整个列表数据进行筛选和排序。图中的XML源面板显示了该数据的架构,你可以通过修改该架构来决定列表中可以包含那些数据。

** 图 ** ** 7 ** ** 使用 ** ** XML ** ** 架构映射以编程方式导入 ** ** XML **

** BuildXMLMap ** 子过程执行与 ** BuildWorksheet ** 相似的功能 , 但是它使用 XML 导入和映射来将数据从 DataSet 移动到 Excel 中。 该过程接收一个 ** DataSet ** 作为其唯一参数,然后实例化 Excel 、添加一个工作簿,同事执行其他 UI (用户界面)任务。 例子中的工作簿名为 Northwind Customers 。 当然,你可以将它替换为你自己的工作簿的名字。

Private Sub BuildXMLMap(ByVal ds As DataSet)

'Create an instance of Excel 2003, add a workbook,

'and let the user know what's happening

Dim xl As New Excel.Application

xl.Workbooks.Add()

xl.ActiveSheet.Name = "Northwind Customers"

xl.Visible = True

xl.Range("A1").Value = "Loading the DataSet...."

该过程直接从 DataSet 对象加载 XML 数据 , 但是没有任何方法可以直接从内存中读取其架构。因此这段代码使用 .NET Framework Path **** 对象的 ** GetFullPath ** 方法为 .XSD 架构文件 获取了一个完整路径和文件名。

Try

Dim sMap As String = System.IO.Path.GetFullPath("Customers.xsd")

然后 , 代码添加 XML 映射架构到当前活动工作簿的 ** XmlMaps ** 集合。 Add 方法的 第一个参数是将要被映射的文件的位置,第二个参数是存储于集合中的这个映射的名字。代码设置了该映射的名字,以便在后面引用这个映射。

'Add the map to the active workbook

'You can only add a map from a disk file.

xl.ActiveWorkbook.XmlMaps.Add(sMap, _

"NorthwindCustomerOrders").Name _

= "NorthwindCustomerOrders_Map"

代码得到这个新建的 XML 映射的引用 , 将它保存为映射变量 , 然后添加一个 ** ListObject ** 到当前活动工作表。 你所选择的列表面板列,本示例中是列A到J,包括标题行下面的许多行都是用来容纳数据的。

'Specify the cells where the mapped data should go upon import

Dim map As Excel.XmlMap = _

xl.ActiveWorkbook.XmlMaps("NorthwindCustomerOrders_Map")

xl.Range("A1", "J1").Select()

Dim list As Excel.ListObject = _

CType(xl.ActiveSheet, Excel.Worksheet).ListObjects.Add

下一步是映射 XML 数据的特定元素到列表的每个列。 SetValue 方法持有所使用的映射的引用,并使用一个XPath表达式来指示哪些元素保存在哪一列。下面的代码设置列 A 容纳 CustomerID 元素 , 然后设置该列标题为 “ Customer ID ” 。你可以设置以相同方式设置其他列的内容和标题。

list.ListColumns(1).XPath.SetValue(map, _

"/NorthwindCustomerOrders/Customers/CustomerID")

xl.Range("A1").Value = "Customer ID"

现在要设置 ** ListObject ** 的结构 , 因为下一步将导入 XML 数据。 Import 方法读取磁盘文件,并且这里使用 ** ImportXml ** 方法直接从 ** DataSet ** 的 ** GetXml ** 方法中读取 XML ,并未将 XML 数据保存到磁盘上。 该过程在最后打开“XML源”任务面板,方便你查看。

'Import the XML data

xl.ActiveWorkbook.XmlMaps("NorthwindCustomerOrders_Map"). _

ImportXml(ds.GetXml)

'Open the XML Source Task Pane

xl.DisplayXMLSourcePane(map)

Catch ex As Exception

End Try

End Sub

Published At
Categories with Web编程
Tagged with
comments powered by Disqus