简体   繁体   English

有没有人有使用 NuGet 的 DocumentFormat.OpenXML.DotNet.Core 包构建控制台应用程序 (C#) 的经验?

[英]Does anyone have experience building a Console App (C#) using the package DocumentFormat.OpenXML.DotNet.Core from NuGet?

I have successfully started a console app solution in Visual Studio 2019, in C#, and have downloaded and installed the package DocumentFormat.OpenXML.DotNet.Core from NuGet.我已经在 Visual Studio 2019 中成功启动了一个控制台应用程序解决方案,在 C# 中,并从 NuGet 下载并安装了包 DocumentFormat.OpenXML.DotNet.Core。 Since DocumentFormat.OpenXML does not play with .NET Core, I cannot use that.由于 DocumentFormat.OpenXML 不能与 .NET Core 一起使用,我不能使用它。 I have also successfully attached the package to the solution, and it appears in the Solution Explorer with no alerts.我也成功地将包附加到解决方案,它出现在解决方案资源管理器中,没有任何警报。 (Same method I have used in the past with other packages.) (我过去在其他软件包中使用的方法相同。)

This package is supposed to give my solution access to the OpenXML functionality, but I cannot get my program to recognize it.这个包应该让我的解决方案访问 OpenXML 功能,但我无法让我的程序识别它。

I have tried inserting a using statement for DocumentFormat, but I get an error that the using statement is not necessary - and then it asks if I am missing a using statement or assembler reference.我曾尝试为 DocumentFormat 插入 using 语句,但我收到一个错误,提示 using 语句不是必需的 - 然后它询问我是否缺少 using 语句或汇编程序引用。

I have tried changing the namespace to DocumentFormat, but then I have to add all the classes and all the functions, which it seems to me is the entire purpose of the package.我曾尝试将命名空间更改为 DocumentFormat,但随后我必须添加所有类和所有函数,在我看来,这就是包的全部目的。

Here is the code (it's sample code just to get this working):这是代码(它的示例代码只是为了让它工作):

using System;
using DocumentFormat;
using S = DocumentFormat.OpenXml.Spreadsheet.Sheets;
using E = DocumentFormat.OpenXml.OpenXmlElement;
using A = DocumentFormat.OpenXml.OpenXmlAttribute;

namespace ConsoleApp1
{
    class Program
    {
        static void ReadExcelFile()
        {
            try
            {
                //Lets open the existing excel file and read through its content . Open the excel using openxml sdk
                using (SpreadsheetDocument doc = SpreadsheetDocument.Open("testdata.xlsx", false))
                {
                    //create the object for workbook part  
                    WorkbookPart workbookPart = doc.WorkbookPart;
                    Sheets thesheetcollection = workbookPart.Workbook.GetFirstChild<Sheets>();
                    StringBuilder excelResult = new StringBuilder();

                    //using for each loop to get the sheet from the sheetcollection  
                    foreach (Sheet thesheet in thesheetcollection)
                    {
                        excelResult.AppendLine("Excel Sheet Name : " + thesheet.Name);
                        excelResult.AppendLine("----------------------------------------------- ");
                        //statement to get the worksheet object by using the sheet id  
                        Worksheet theWorksheet = ((WorksheetPart)workbookPart.GetPartById(thesheet.Id)).Worksheet;

                        SheetData thesheetdata = (SheetData)theWorksheet.GetFirstChild<SheetData>();
                        foreach (Row thecurrentrow in thesheetdata)
                        {
                            foreach (Cell thecurrentcell in thecurrentrow)
                            {
                                //statement to take the integer value  
                                string currentcellvalue = string.Empty;
                                if (thecurrentcell.DataType != null)
                                {
                                    if (thecurrentcell.DataType == CellValues.SharedString)
                                    {
                                        int id;
                                        if (Int32.TryParse(thecurrentcell.InnerText, out id))
                                        {
                                            SharedStringItem item = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(id);
                                            if (item.Text != null)
                                            {
                                                //code to take the string value  
                                                excelResult.Append(item.Text.Text + " ");
                                            }
                                            else if (item.InnerText != null)
                                            {
                                                currentcellvalue = item.InnerText;
                                            }
                                            else if (item.InnerXml != null)
                                            {
                                                currentcellvalue = item.InnerXml;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    excelResult.Append(Convert.ToInt16(thecurrentcell.InnerText) + " ");
                                }
                            }
                            excelResult.AppendLine();
                        }
                        excelResult.Append("");
                        Console.WriteLine(excelResult.ToString());
                        Console.ReadLine();
                    }
                }
            }
            catch (Exception)
            {

            }
        }
    }
}

I have refreshed the package, I have started a new solution and added the package to it before typing anything in the code window, I have tried updating and reinstalling both in the NuGet Package Manager Console and in the Manage NuGet Packages for Solution window.我已经刷新了包,我已经启动了一个新的解决方案并在代码窗口中输入任何内容之前将包添加到其中,我尝试在 NuGet 包管理器控制台和管理 NuGet 包解决方案窗口中更新和重新安装。

Can someone please tell me what I'm missing?有人可以告诉我我错过了什么吗?

Thanks in advance,提前致谢,

DJ DJ

Not sure how you determined that the Open XML SDK as provided in the DocumentFormat.OpenXml NuGet package "does not play with .NET Core".不确定您如何确定DocumentFormat.OpenXml NuGet 包中提供的 Open XML SDK“不支持 .NET Core”。 As a contributor to the Open XML SDK, I can confirm that it definitely does play with .NET Core and you can clone my CodeSnippets GitHub repository to test this yourself.作为 Open XML SDK 的贡献者,我可以确认它确实可以与 .NET Core 一起使用,您可以克隆我的CodeSnippets GitHub 存储库来自己测试。 That repository contains a solution with multiple projects, a few of which use .NET Core (eg, the CodeSnippets library, which targets netstandard2.0 , and the CodeSnippets.Tests library, which targets netcoreapp3.0 ).该资源库包含多个项目,其中的一些使用.NET的核心(例如,CodeSnippets库,它的目标的解决方案netstandard2.0和CodeSnippets.Tests库,它的目标netcoreapp3.0 )。

The DocumentFormat.OpenXml.DotNet.Core NuGet package that you use was last updated on July 30, 2016, ie, almost four years ago.您使用的DocumentFormat.OpenXml.DotNet.Core NuGet 包上次更新是在 2016 年 7 月 30 日,即大约四年前。 You should really replace this with the official DocumentFormat.OpenXml NuGet package.你真的应该用官方的 DocumentFormat.OpenXml NuGet 包替换它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 和 DocumentFormat.OpenXml nuget ZEFE90A8E604A7C84A70E8 格式化 Excel 中的数字 - How to format number in Excel by using C# and DocumentFormat.OpenXml nuget package? DocumentFormat.OpenXml Nuget程序包具有许多依赖项,例如Microsoft.NetCore.Platforms - DocumentFormat.OpenXml Nuget package have many dependencies like Microsoft.NetCore.Platforms 使用DocumentFormat.OpenXml(C#)更新.docx文档中的目录 - Update TOC in .docx document using DocumentFormat.OpenXml (C#) 使用DocumentFormat.OpenXml(c#)合并Word文档 - Merging word-documents using DocumentFormat.OpenXml(c#) 使用Documentformat.OpenXML在C#中验证Excel文件 - Validate Excel file in c# using Documentformat.OpenXML 如何使用 DocumentFormat.OpenXML 或 ClosedXML C# 从 Excel 获取合并单元格 - How to get a merged cell from Excel using DocumentFormat.OpenXML or ClosedXML C# 在c#dotnet核心控制台应用程序中保护密码输入 - Securing a password input in c# dotnet core console app 如何使用DocumentFormat.OpenXml C#获取excel工作表中的特定列? - How can I get the specific column in excel worksheet using DocumentFormat.OpenXml C#? 从 excel 单元格中检索数据,即 C# documentformat.openxml 中的 integer.xslx 文档 - Retrieving data from excel cell that is integer .xslx document in C# documentformat.openxml 使用Microsoft DocumentFormat.OpenXml SDK在C#中读取Excel文件 - Reading excel file in c# using Microsoft DocumentFormat.OpenXml SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM