Last post Mar 04, 2013 01:51 PM by markfitzme
Mar 04, 2013 01:37 PM|vijaykumar_chebolu|LINK
I have a requriement to upload a file and modify and then return the same. I dont need to save the file to any location but manipulate it and return. However I am not knowing how to return the file.
The below code allows the file to save, I even wrote code with out saving file. only problem is I am getting an error that this file is already open by someother user.
The process cannot access the file 'D:\Places\places\App_Data\877d36d3-ce29-48d1-995a-ea6652a528a7C2.xlsx' because it is being used by another process.
Can you please help me
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
if (uploadFile.ContentLength > 0)
string path = string.Empty;
var fileName = Path.GetFileName(uploadFile.FileName);
path = Path.Combine(Server.MapPath("~/App_Data/"), Guid.NewGuid() + fileName);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(path);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets;
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count;
(xlRange.Cells[4, 5] as Excel.Range).Value2 = "asdF";
(xlRange.Cells[4, 6] as Excel.Range).Value2 = "asdF";
(xlRange.Cells[4, 7] as Excel.Range).Value2 = "asdF";
(xlRange.Cells[4, 8] as Excel.Range).Value2 = "asdF";
return File(path, "application/text");
private void releaseObject(object obj)
obj = null;
catch (Exception ex)
obj = null;
//MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
Mar 04, 2013 01:51 PM|markfitzme|LINK
You may have better luck not using the Excel object. First, Microsoft has recommended against using Office apps in web applications since the late 90's. The threading models are all wrong for eachother and they can lead to server instability. It's happened
to me on a legacy project where the developer ignored the MS recommendations.
Second, did you omit the code that shows saving and closing the workbook? If you don't close it specifically the file may remained locked even though the interop objects are disposed of.