如何将列添加到存储为 VB.NET XML 文字的 MS Office Excel 电子表格
How to add columns to MS Office Excel Spreadsheet stored as VB.NET XML Literal
背景
我有一个 Excel 传播sheet,我已将其保存为 2003 XML 传播 sheet,我已将其粘贴到控制台模式中 VB.NET 使用 VS 2022 创建的程序。
目标
我想使用 VB.NET 自动添加一些列。
观察结果
我看到 MSExcel 广泛使用了 XML 命名空间:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="197 Industry Groups">
<Table ss:ExpandedColumnCount="10" ss:ExpandedRowCount="198" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="42" ss:DefaultRowHeight="11.25">
<Row>
<Cell><Data ss:Type="String">Order</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
<Cell><Data ss:Type="String">Symbol</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">2</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
<Cell ss:StyleID="s62" ss:HRef="https://marketsmith.investors.com/mstool?Symbol=G1000"><Data
ss:Type="String">G1000</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
计划
使用 XPath 获取对第一行的引用并使用 this post as a guide
添加一个单元格
问题
- 我需要将哪些名称空间添加到名称空间管理器? AddNameSpace 函数的第二个 (uri) 参数使用什么?我需要添加一个空的命名空间吗?
- 为什么值 g2 和 g1000 得到值 Nothing?
Dim g2 = industryGroups.XPathSelectElement("//ss:Workbook/ss:Worksheet/ss:Table/ss:Row[0]", namespaceManager)
Dim g1000 = (From p In industryGroups.Descendants("Table") Select p).FirstOrDefault()
- 如何使用其他单元格作为模式添加新的 XElement(单元格)(即每个单元格包含一个具有属性的数据,或者在某些情况下,该单元格将具有 href 属性)。我可以使用 XML 文字功能并嵌入我计算的 href、字符串和数字吗?
谢谢!
齐格弗里德
以下内容对问题 #1 和 #2 应该有所帮助:
添加以下导入语句:
Imports System.Xml
Imports System.Xml.XPath
代码:
Dim xDoc As XDocument = XDocument.Load(filename)
Debug.WriteLine($"xDoc: {xDoc.ToString()}")
'add namespaces that exist in XML file
Dim nsSS As XNamespace = "urn:schemas-microsoft-com:office:spreadsheet"
'Dim dataItems = From x In xDoc.Descendants(nsSS + "Workbook").Descendants(nsSS + "Worksheet").Descendants(nsSS + "Table").Descendants(nsSS + "Row").Descendants(nsSS + "Cell").Descendants(nsSS + "Data") Select x
'Dim dataItems = From x In xDoc.Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Workbook").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Worksheet").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Table").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Row").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Cell").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Data") Select x
Dim dataItems = From x In xDoc.Descendants(nsSS + "Table").Descendants(nsSS + "Row").Descendants(nsSS + "Cell").Descendants(nsSS + "Data") Select x
Debug.WriteLine($"dataItems: {dataItems.Count.ToString()} Type: {dataItems.GetType.ToString()}")
If dataItems IsNot Nothing Then
For Each item In dataItems
Debug.WriteLine($"{item.ToString()}")
Next
End If
代码:
Dim xDoc As XDocument = XDocument.Load(filename)
'Debug.WriteLine($"xDoc: {xDoc.ToString()}")
'add namespaces that exist in XML file
Dim nsMgr = New XmlNamespaceManager(New NameTable())
nsMgr.AddNamespace("", "urn:schemas-microsoft-com:office:spreadsheet")
nsMgr.AddNamespace("o", "urn:schemas-microsoft-com:office:office")
nsMgr.AddNamespace("x", "urn:schemas-microsoft-com:office:excel")
nsMgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet")
nsMgr.AddNamespace("html", "http://www.w3.org/TR/REC-html40")
Dim g2 = xDoc.XPathSelectElement("ss:Workbook/ss:Worksheet/ss:Table", nsMgr)
Debug.WriteLine($"g2: {g2.ToString()}")
Test.xml:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>TestUser</Author>
<LastAuthor>TestUser</LastAuthor>
<Created>2022-04-02T14:35:55Z</Created>
<LastSaved>2022-04-02T14:37:37Z</LastSaved>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>5955</WindowHeight>
<WindowWidth>17970</WindowWidth>
<WindowTopX>32767</WindowTopX>
<WindowTopY>32767</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<NumberFormat ss:Format="0"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Column ss:StyleID="s62"/>
<Row>
<Cell><Data ss:Type="String">Id</Data></Cell>
<Cell><Data ss:Type="String">FirstName</Data></Cell>
<Cell><Data ss:Type="String">LastName</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">1</Data></Cell>
<Cell><Data ss:Type="String">John</Data></Cell>
<Cell><Data ss:Type="String">Smith</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">2</Data></Cell>
<Cell><Data ss:Type="String">Bob</Data></Cell>
<Cell><Data ss:Type="String">Seagul</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>2</ActiveRow>
<ActiveCol>2</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
资源:
- XDocument containing namespaces
- how to use XPath with XDocument?
- XDocument Class
背景
我有一个 Excel 传播sheet,我已将其保存为 2003 XML 传播 sheet,我已将其粘贴到控制台模式中 VB.NET 使用 VS 2022 创建的程序。
目标
我想使用 VB.NET 自动添加一些列。
观察结果
我看到 MSExcel 广泛使用了 XML 命名空间:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="197 Industry Groups">
<Table ss:ExpandedColumnCount="10" ss:ExpandedRowCount="198" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="42" ss:DefaultRowHeight="11.25">
<Row>
<Cell><Data ss:Type="String">Order</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
<Cell><Data ss:Type="String">Symbol</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">2</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
<Cell ss:StyleID="s62" ss:HRef="https://marketsmith.investors.com/mstool?Symbol=G1000"><Data
ss:Type="String">G1000</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
计划
使用 XPath 获取对第一行的引用并使用 this post as a guide
添加一个单元格问题
- 我需要将哪些名称空间添加到名称空间管理器? AddNameSpace 函数的第二个 (uri) 参数使用什么?我需要添加一个空的命名空间吗?
- 为什么值 g2 和 g1000 得到值 Nothing?
Dim g2 = industryGroups.XPathSelectElement("//ss:Workbook/ss:Worksheet/ss:Table/ss:Row[0]", namespaceManager)
Dim g1000 = (From p In industryGroups.Descendants("Table") Select p).FirstOrDefault()
- 如何使用其他单元格作为模式添加新的 XElement(单元格)(即每个单元格包含一个具有属性的数据,或者在某些情况下,该单元格将具有 href 属性)。我可以使用 XML 文字功能并嵌入我计算的 href、字符串和数字吗?
谢谢!
齐格弗里德
以下内容对问题 #1 和 #2 应该有所帮助:
添加以下导入语句:
Imports System.Xml
Imports System.Xml.XPath
代码:
Dim xDoc As XDocument = XDocument.Load(filename)
Debug.WriteLine($"xDoc: {xDoc.ToString()}")
'add namespaces that exist in XML file
Dim nsSS As XNamespace = "urn:schemas-microsoft-com:office:spreadsheet"
'Dim dataItems = From x In xDoc.Descendants(nsSS + "Workbook").Descendants(nsSS + "Worksheet").Descendants(nsSS + "Table").Descendants(nsSS + "Row").Descendants(nsSS + "Cell").Descendants(nsSS + "Data") Select x
'Dim dataItems = From x In xDoc.Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Workbook").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Worksheet").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Table").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Row").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Cell").Descendants("{urn:schemas-microsoft-com:office:spreadsheet}Data") Select x
Dim dataItems = From x In xDoc.Descendants(nsSS + "Table").Descendants(nsSS + "Row").Descendants(nsSS + "Cell").Descendants(nsSS + "Data") Select x
Debug.WriteLine($"dataItems: {dataItems.Count.ToString()} Type: {dataItems.GetType.ToString()}")
If dataItems IsNot Nothing Then
For Each item In dataItems
Debug.WriteLine($"{item.ToString()}")
Next
End If
代码:
Dim xDoc As XDocument = XDocument.Load(filename)
'Debug.WriteLine($"xDoc: {xDoc.ToString()}")
'add namespaces that exist in XML file
Dim nsMgr = New XmlNamespaceManager(New NameTable())
nsMgr.AddNamespace("", "urn:schemas-microsoft-com:office:spreadsheet")
nsMgr.AddNamespace("o", "urn:schemas-microsoft-com:office:office")
nsMgr.AddNamespace("x", "urn:schemas-microsoft-com:office:excel")
nsMgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet")
nsMgr.AddNamespace("html", "http://www.w3.org/TR/REC-html40")
Dim g2 = xDoc.XPathSelectElement("ss:Workbook/ss:Worksheet/ss:Table", nsMgr)
Debug.WriteLine($"g2: {g2.ToString()}")
Test.xml:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>TestUser</Author>
<LastAuthor>TestUser</LastAuthor>
<Created>2022-04-02T14:35:55Z</Created>
<LastSaved>2022-04-02T14:37:37Z</LastSaved>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>5955</WindowHeight>
<WindowWidth>17970</WindowWidth>
<WindowTopX>32767</WindowTopX>
<WindowTopY>32767</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<NumberFormat ss:Format="0"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Column ss:StyleID="s62"/>
<Row>
<Cell><Data ss:Type="String">Id</Data></Cell>
<Cell><Data ss:Type="String">FirstName</Data></Cell>
<Cell><Data ss:Type="String">LastName</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">1</Data></Cell>
<Cell><Data ss:Type="String">John</Data></Cell>
<Cell><Data ss:Type="String">Smith</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">2</Data></Cell>
<Cell><Data ss:Type="String">Bob</Data></Cell>
<Cell><Data ss:Type="String">Seagul</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>2</ActiveRow>
<ActiveCol>2</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
资源:
- XDocument containing namespaces
- how to use XPath with XDocument?
- XDocument Class