I have an ASP.Net Web Forms site where a user can write values to an Excel Spreadsheet. I was able to allow users to open a copy of the spreadsheet saved on the server and open it on Excel (all users have access to Excel). I was able to have the user write to the file right as they open it with a button. The problem is that the user can't write to the file after it is open. I added some code below to illustrate my problem.
Working code:
protected void openExcel(object sender, EventArgs e)
{
string xlsxpath = Server.MapPath("~/new_takeoff.xlsx");
fi = new FileInfo(Server.MapPath("~/new_takeoff.xlsx"));
excelPackage = new ExcelPackage(fi);
first = excelPackage.Workbook.Worksheets[1];
ExcelRange chosen = first.SelectedRange;
first.Cells["I54"].Value = 51;
excelPackage.Save();
System.Diagnostics.Process.Start(xlsxpath);
}
As you can see I took care of writing and opening in the same method (handled by an OnClick instance of a button).
These separate methods (handled by separate buttons) do not work:
protected void openExcel(object sender, EventArgs e)
{
string xlsxpath = Server.MapPath("~/new_takeoff.xlsx");
System.Diagnostics.Process.Start(xlsxpath);
}
protected void updateIT(object sender, EventArgs e)
{
fi = new FileInfo(Server.MapPath("~/new_takeoff.xlsx"));
excelPackage = new ExcelPackage(fi);
first = excelPackage.Workbook.Worksheets[1];
ExcelRange chosen = first.SelectedRange;
first.Cells["I54"].Value = 51;
excelPackage.Save();
}
The debugger tells me that the file cannot be accessed because it is being used by another process.
By the way, I need the file open because the user has to pick what cell to fill. The C# code behind picks up the chosen ExcelRange using SelectedRange in the EPPlus library.
What I have tried:
I tried using the Microsoft.Office.Interop.Excel dll, but of course that doesn't work for a website.