c# - 使用 Linq to XML、XMLNS、XDeclaration、单元格格式创建 Office Excel 文档(有效)

标签 c# .net excel linq-to-xml ms-office

我需要在 C# 和 Linq 中将此 XML 复制到 XML。除了普通的 .NET 库之外,我不希望依赖于其他库。 XML 如下所示。

问题:我不知道如何打印这两行:

<?mso-application progid="Excel.Sheet"?>
<Data ss:Type="String">name</Data>

完整的 XML 文档:

<?xml version="1.0" encoding="utf-8" ?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:html="http://www.w3.org/TR/REC-html40"
  xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">
    <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"></OfficeDocumentSettings>
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"></ExcelWorkbook>
    <Worksheet ss:Name="Sheet 1">
        <Table>
            <Row>
                <Cell>
                    <Data ss:Type="String">name</Data>
                    </Cell>
                                <Cell>
                    <Data ss:Type="String">status</Data>
                </Cell>
                           </Row>
            <Row>
                <Cell>
                    <Data ss:Type="String">Niike2</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">Enabled</Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

代码:

XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
XDocument doc = new XDocument(
    new XDeclaration("1.0", "UTF-8", string.Empty),
        new XComment(String.Format("Exported: {0}", DateTime.Now)),
        new XElement(ns + "Workbook",
            new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
            new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
            new XAttribute(XNamespace.Xmlns + "x2", "http://schemas.microsoft.com/office/excel/2003/xml"),
            new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
            new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
            new XAttribute(XNamespace.Xmlns + "c", "urn:schemas-microsoft-com:office:component:spreadsheet"),
            new XElement(ns + "Worksheet",
            new XElement(ns + "Table",
                new XElement(ns + "Row",
                    new XElement(ns + "Cell", "name")
                    )
                )
            )
        );

最佳答案

第二行有 称为处理指令。剩下的只是操纵命名空间。

XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace x = "urn:schemas-microsoft-com:office:excel";
XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
XNamespace o = "urn:schemas-microsoft-com:office:office";
XNamespace html = "http://www.w3.org/TR/REC-html40";
XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";

XDocument doc = new XDocument(
        new XDeclaration("1.0", "UTF-8", string.Empty),
        new XComment(String.Format("Exported: {0}", DateTime.Now)),
        new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),        
        new XElement("Workbook",            
            new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
            new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
            new XAttribute(XNamespace.Xmlns + "x2", "http://schemas.microsoft.com/office/excel/2003/xml"),
            new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"),
            new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
            new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
            new XAttribute(XNamespace.Xmlns + "c", "urn:schemas-microsoft-com:office:component:spreadsheet"),
            new XElement("Worksheet", new XAttribute(ss + "Name", "Sheet 1"),               
                new XElement("Table",
                    new XElement("Row",
                        new XElement("Cell",
                            new XElement("Data", new XAttribute(ss + "Type", "String"),"status"))
                    )
                )
            )
        )
    );

关于c# - 使用 Linq to XML、XMLNS、XDeclaration、单元格格式创建 Office Excel 文档(有效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4513714/

相关文章:

c# - 如何建立Nest SearchRequest对象并在查询中查看原始JSON?

c# - Updatepanel 中的 ImageButton 仅在第一页加载时触发

excel - 发现要使用的 SAP 事务字段的组件标识符

excel - 如何使用VBA设置动态单元格范围?

c# - const Enum 避免循环定义的最佳实践命名约定

c# - Visual Studio 项目文件指定多次导入

c# - 在 C# 中使用 'out' 关键字返回多个值

.net - "FontWeight"的 TypeConverter 不支持从字符串转换

c# - 如何在文本框的 TextChanged 事件中获取文本框的旧文本和更改文本?

excel - 在 Excel 中对标准曲线进行插值