I installed DocumentFormat.OpenXML
via Nuget so that I can manipulate Excel files.
I am testing with this code:
in Program.cs
:
static void Main(string[] args)
{
ExcelWatcher.ExcelDataExtractor xtr = new ExcelWatcher.ExcelDataExtractor();
string data=xtr.getData(@"C:\Users\usrname\Documents\test.xlsx");
Console.WriteLine(data);
}
in ExcelDataExtractor.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace ExcelWatcher
{
public class ExcelDataExtractor
{
public string getData(string workbookPath)
{
try
{
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(workbookPath,false);
return string.Empty;
}
catch(Exception ex)
{
return ex.ToString();
}
}
}
But when, in Program.cs
, the xtr.getData()
call occurs, I get this error:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.IO.Packaging, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'
as an unhandled exception.
I have a reference for the WindowsBase
assembly enabled.
I looked at some other StackOverflow questions like this one. But when I tried running this in the Package Manager CLI:
update-package -reinstall System.IO.Packaging
I got this error:
update-package : 'System.IO.Packaging' was not installed in any project. Update failed.
But System.IO.Packaging
is showing as one of the references I have enabled.
What's wrong with how I'm using OpenXML?