![](/img/trans.png)
[英]How to format number in Excel by using C# and DocumentFormat.OpenXml nuget package?
[英]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.