I honestly don't see what I'm missing here.
The code selects an excel file, the excel file is opened with EPPlus and I attempt to edit one of the cells. However, when I try to save, it errors out.
I'm getting the following error on my p.Saveas()
: NullReferenceException: Object reference not set to an instance of an object. Error saving file C:\2.xlsx' I can clearly see my path is valid.
OpenFileDialog odf = new OpenFileDialog();
odf.ShowDialog();
FileInfo file = new FileInfo(odf.FileName);
Console.WriteLine(file);
foreach (DataGridViewRow row in DGVmain.SelectedRows)
{
using (ExcelPackage p = new ExcelPackage(file))
{
using (ExcelWorkbook WBook = p.Workbook)
{
using (OfficeOpenXml.ExcelWorksheet WS = WBook.Worksheets.First())
{
if (WBook != null)
{
if (WBook.Worksheets.Count > 0)
{
string k = Convert.ToString(row.Cells[1].Value);
string ks = Convert.ToString(row.Cells[2].Value);
string kss = Convert.ToString(row.Cells[3].Value);
WS.Cells[14, 2].Value = k + ", " + ks + " " + kss;
}
}
}
}
p.SaveAs(file);
}
}
The file is meant to save as the original file, and replace it if it already exists, which is why I use saveas versus save, which will throw an error at that point,