Workbook.SaveAsXMLData 方法 (2007 system)

更新:2007 年 11 月

将映射到指定 XML 架构映射的数据导出到 XML 数据文件。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel.v9.0(在 Microsoft.Office.Tools.Excel.v9.0.dll 中)

语法

声明
Public Sub SaveAsXMLData ( _
    Filename As String, _
    Map As XmlMap _
)
用法
Dim instance As Workbook
Dim Filename As String
Dim Map As XmlMap

instance.SaveAsXMLData(Filename, Map)
public void SaveAsXMLData(
    string Filename,
    XmlMap Map
)

参数

  • Filename
    类型:System.String

    要保存的文件的名称。可以包含完整路径;否则,Microsoft Office Excel 将文件保存在当前文件夹中。

备注

如果 Excel 无法使用指定架构映射导出数据,则此方法会导致运行时错误。若要检查 Excel 是否可以使用指定架构映射导出数据,请使用 IsExportable 属性。

示例

下面的代码示例演示如何从工作簿中导出 XML 数据。该示例将 XML 数据从 DataSet 导入到当前工作簿的 XmlMap 中。然后,该示例调用 SaveAsXMLData 方法将数据从 XmlMap 导出到 XML 文件中。调用 SaveAsXMLData 方法时,BeforeXmlExport 事件处理程序报告正在导出 XML,并且 AfterXmlExport 事件处理程序报告是否已成功导出 XML。

此示例针对的是文档级自定义项。

Private Sub WorkbookXmlExportEvents()

    ' Create a new DataTable.
    Dim ds As New DataSet()
    Dim dt As DataTable = ds.Tables.Add("Customers")
    dt.Columns.Add(New DataColumn("LastName"))
    dt.Columns.Add(New DataColumn("FirstName"))

    ' Add a new row to the DataTable.
    Dim dr As DataRow = dt.NewRow()
    dr("LastName") = "Chan"
    dr("FirstName") = "Gareth"
    dt.Rows.Add(dr)

    ' Add a new XML map.
    Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add( _
        ds.GetXmlSchema())

    ' Import the data into Sheet1. 
    Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
    Me.XmlImportXml(ds.GetXml(), xmlMap1, True, _
        range1)

    If xmlMap1.IsExportable Then
        Me.SaveAsXMLData(Me.Name & ".xml", xmlMap1)
    End If
End Sub

Sub ThisWorkbook_BeforeXmlExport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByRef Cancel As Boolean) _
    Handles Me.BeforeXmlExport

    MsgBox("Microsoft Excel is exporting XML from " & _
        "the XmlMap.")
End Sub

Sub ThisWorkbook_AfterXmlExport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByVal Result As Excel.XlXmlExportResult) _
    Handles Me.AfterXmlExport

    If Result = Excel.XlXmlExportResult.xlXmlExportSuccess Then
        MsgBox("XML export succeeded.")
    Else
        MsgBox("XML export failed.")
    End If
End Sub
private void WorkbookXmlExportEvents()
{
    this.BeforeXmlExport +=
        new Excel.WorkbookEvents_BeforeXmlExportEventHandler(
        ThisWorkbook_BeforeXmlExport);

    this.AfterXmlExport +=
        new Excel.WorkbookEvents_AfterXmlExportEventHandler(
        ThisWorkbook_AfterXmlExport);

    // Create a new DataTable.
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("Customers");
    dt.Columns.Add(new DataColumn("LastName"));
    dt.Columns.Add(new DataColumn("FirstName"));

    // Add a new row to the DataTable.
    DataRow dr = dt.NewRow();
    dr["LastName"] = "Chan";
    dr["FirstName"] = "Gareth";
    dt.Rows.Add(dr);

    // Add a new XML map.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(
        ds.GetXmlSchema(), missing);

    // Import the data into Sheet1. 
    Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
    this.XmlImportXml(ds.GetXml(), out xmlMap1, true,
       range1);

    // Export the data.
    if (xmlMap1.IsExportable)
    {
        this.SaveAsXMLData(this.Name + ".xml", xmlMap1);
    }
}

void ThisWorkbook_BeforeXmlExport(Excel.XmlMap Map, string Url,
    ref bool Cancel)
{
    MessageBox.Show("Microsoft Excel is exporting XML from " +
        "the XmlMap.");
}

void ThisWorkbook_AfterXmlExport(Excel.XmlMap Map, string Url,
    Excel.XlXmlExportResult Result)
{
    if (Result == Excel.XlXmlExportResult.xlXmlExportSuccess)
    {
        MessageBox.Show("XML export succeeded.");
    }
    else
    {
        MessageBox.Show("XML export failed.");
    }
}

权限

另请参见

参考

Workbook 类

Workbook 成员

Microsoft.Office.Tools.Excel 命名空间