此软件包应该使我的解决方案可以访问OpenXML功能,但是我无法让我的程序识别它。
[我尝试为DocumentFormat插入using语句,但是收到一条错误消息,指出using语句不是必需的-然后它询问我是否缺少using语句或汇编程序引用。
我曾尝试将名称空间更改为DocumentFormat,但随后我必须添加所有类和所有函数,在我看来,这是软件包的[[entire
目的。这是代码(仅是示例代码,它可以正常工作):
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
我已经在Visual Studio 2019中以C#成功启动了控制台应用程序解决方案,并从NuGet下载并安装了包DocumentFormat.OpenXML.DotNet.Core。由于DocumentFormat ....