简体   繁体   中英

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. Since DocumentFormat.OpenXML does not play with .NET Core, I cannot use that. 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.

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.

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.

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.

Can someone please tell me what I'm missing?

Thanks in advance,

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". 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. 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 ).

The DocumentFormat.OpenXml.DotNet.Core NuGet package that you use was last updated on July 30, 2016, ie, almost four years ago. You should really replace this with the official DocumentFormat.OpenXml NuGet package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM