D J*_*lom 1 c# openxml .net-core visual-studio-2019
我已在 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)
{
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我刷新了包,启动了一个新的解决方案,并在代码窗口中键入任何内容之前将包添加到其中,我尝试在 NuGet 包管理器控制台和“管理解决方案的 NuGet 包”窗口中更新和重新安装。
有人可以告诉我我错过了什么吗?
提前致谢,
DJ
不确定您如何确定DocumentFormat.OpenXml NuGet 包中提供的 Open XML SDK“不能与 .NET Core 一起使用”。作为 Open XML SDK 的贡献者,我可以确认它确实可以与 .NET Core 一起使用,并且您可以克隆我的CodeSnippets GitHub 存储库来自行测试。该存储库包含一个具有多个项目的解决方案,其中一些项目使用 .NET Core(例如,面向 的 CodeSnippets 库netstandard2.0
和面向 的 CodeSnippets.Tests 库netcoreapp3.0
)。
您使用的DocumentFormat.OpenXml.DotNet.Core NuGet 包的上次更新时间是 2016 年 7 月 30日,即大约四年前。您确实应该将其替换为官方 DocumentFormat.OpenXml NuGet 包。
归档时间: |
|
查看次数: |
1531 次 |
最近记录: |