簡體   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?

我已經在 Visual Studio 2019 中成功啟動了一個控制台應用程序解決方案,在 C# 中,並從 NuGet 下載並安裝了包 DocumentFormat.OpenXML.DotNet.Core。 由於 DocumentFormat.OpenXML 不能與 .NET Core 一起使用,我不能使用它。 我也成功地將包附加到解決方案,它出現在解決方案資源管理器中,沒有任何警報。 (我過去在其他軟件包中使用的方法相同。)

這個包應該讓我的解決方案訪問 OpenXML 功能,但我無法讓我的程序識別它。

我曾嘗試為 DocumentFormat 插入 using 語句,但我收到一個錯誤,提示 using 語句不是必需的 - 然后它詢問我是否缺少 using 語句或匯編程序引用。

我曾嘗試將命名空間更改為 DocumentFormat,但隨后我必須添加所有類和所有函數,在我看來,這就是包的全部目的。

這是代碼(它的示例代碼只是為了讓它工作):

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)
            {

            }
        }
    }
}

我已經刷新了包,我已經啟動了一個新的解決方案並在代碼窗口中輸入任何內容之前將包添加到其中,我嘗試在 NuGet 包管理器控制台和管理 NuGet 包解決方案窗口中更新和重新安裝。

有人可以告訴我我錯過了什么嗎?

提前致謝,

DJ

不確定您如何確定DocumentFormat.OpenXml NuGet 包中提供的 Open XML SDK“不支持 .NET Core”。 作為 Open XML SDK 的貢獻者,我可以確認它確實可以與 .NET Core 一起使用,您可以克隆我的CodeSnippets GitHub 存儲庫來自己測試。 該資源庫包含多個項目,其中的一些使用.NET的核心(例如,CodeSnippets庫,它的目標的解決方案netstandard2.0和CodeSnippets.Tests庫,它的目標netcoreapp3.0 )。

您使用的DocumentFormat.OpenXml.DotNet.Core NuGet 包上次更新是在 2016 年 7 月 30 日,即大約四年前。 你真的應該用官方的 DocumentFormat.OpenXml NuGet 包替換它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM