【问题标题】:steps to create excel XML spreadsheet using c#使用 c# 创建 excel XML 电子表格的步骤
【发布时间】:2013-09-21 06:53:16
【问题描述】:

我们有一个要求,比如将数据导出到 Xml 格式的 excel 工作表中,比如创建一个新的 XML 电子表格我已经按照这个链接创建 excel xml Spreadsheet。在此链接中,他提到了示例

< ?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>Author</author>
<lastauthor>LastAuthor</lastauthor>
<created>11-09-2007</created>
<version>12.00</version>
</documentproperties>
<excelworkbook xmlns="urn:schemas-microsoft-com:office:excel">
<protectstructure>False</protectstructure>
<protectwindows>False</protectwindows>
</excelworkbook>
</workbook>

我需要在c#项目中定义这种格式,在上面的代码中我需要从数据库中获取关于作者和最后作者的信息......

在那个链接中他没有完全提到创建文档...

如果我想创建一个ExcelXml spread sheet 我需要遵循哪些步骤,我是否需要创建一个预定义的格式来存储在项目中...

我们能够访问开放的 XML sdk,但我找不到任何用于在 excel 电子表格中创建 xml 格式的示例解决方案,是否可以使用 open XML SDK 做同样的事情,如果可能的话,请指点我在正确的方向...

有没有人有任何想法和解决方案非常感谢我......

提前致谢

【问题讨论】:

  • 你可以有一个模板电子表格,然后复制它,当然如果模板是理想的。
  • 大多数时候我们只是创建一个基本电子表格并使用 XSLT 更改它。
  • 我强烈推荐使用像 ClosedXML 这样的东西。尝试使用 OpenXML API 很糟糕。
  • @aquinas 最后我使用了相同的 API(ClosedXML)....
  • 如果想使用 OpenXml API,这里有一个很好的指南:dispatchertimer.com/tutorial/…

标签: c# .net export-to-excel openxml-sdk


【解决方案1】:

您可以使用 OpenXml SDK 来完成此任务。

直接使用 OpenXml SDK 并不容易,对于简单的应用程序最好使用包装器。

看看JumboExcel 项目(披露:我是作者)。

创建电子表格非常简单:

var tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".xlsx");
using (var file = new FileStream(tempFileName, FileMode.CreateNew))
{
    OpenXmlBuilder.Write(
        file, 
        new[] {
            new Worksheet(
                "Parameters",
                null,
                new Row(new InlineString("Name"), new InlineString("Value")),
                new Row(new InlineString("Height"), new DecimalCell(123m))
            )
        }
    );
}
Process.Start(tempFileName);

此外,您还可以探索源代码,参见 Github page 的源代码,并查看 DemoTests 以获取更多示例。

【讨论】:

    【解决方案2】:

    如果模板失败,请尝试从 here 获取的以下内容

    using System.IO;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    
    public static void CreateSpreadsheetWorkbook(string filepath)
        {
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
    
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
    
            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();
    
            // Add a WorksheetPart to the WorkbookPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());
    
            // Add Sheets to the Workbook.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
    
            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
            sheets.Append(sheet);
    
            workbookpart.Workbook.Save();
    
            // Close the document.
            spreadsheetDocument.Close();
        }
    
    // Called using
    CreateSpreadsheetWorkbook("C:\\Test\\Test.xlsx");
    

    编辑:您可以使用以下代码将 xml 转换为 excel:

    Workbook workbook = new Workbook(); 
    workbook.LoadFromFile(@"../../Data/test.xml"); 
    workbook.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2010);
    

    如果您想实际创建 Office XML 文档,我不确定如何从 xml 文件自动执行该过程。看看this for some pointers

    【讨论】:

    • 谢谢你,一个疑问是我需要把所有这些放在哪里,是在 c# 类文件中还是在 xml 文件中...&lt;?xml version="1.0"?&gt; &lt;ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"&gt; &lt;ss:Worksheet ss: Name="Sheet1"&gt; &lt;ss:Table&gt; &lt;/ss:Table&gt; &lt;/ss:Worksheet&gt; &lt;/ss:Workbook&gt;
    • 对于导出,我需要先创建xml格式然后将其导出到excel表中..这样正确吗...
    • 我误会了您有一个 xml 文档并希望将其放入 excel 中。
    • 您想真正将 xml 写入 excel 文件吗?只进入一个单元格?有一点你说将 xml DATA 导入电子表格,现在看来你想要电子表格中的实际 XML?两种截然不同的东西。
    • 抱歉让我感到困惑,我想要电子表格中的实际 xml .. 我有来自数据库的数据 .. 像 emp name 和 emp id ... 我该怎么做...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多