Я успешно запустил решение консольного приложения в Visual Studio 2019, в C#, и загрузил и установил пакет DocumentFormat.Open XML .Do tNet .Core из NuGet. Поскольку DocumentFormat.Open XML не работает с. NET Core, я не могу это использовать. Я также успешно прикрепил пакет к решению, и он появляется в обозревателе решений без предупреждений. (Тот же метод, который я использовал в прошлом с другими пакетами.)
Предполагается, что этот пакет предоставит моему решению доступ к функциональности Open XML, но я не могу заставить свою программу его распознать.
Я попытался вставить оператор using для DocumentFormat, но я получаю сообщение об ошибке, что оператор 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