Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created an asp.net website using c#,when the button is clicked,the values from the webpage are stored into an excel sheet in the system,this works fine when done locally,but,when accessing from the client machine,within the intranet,it shows an error saying:
"file path is invalid or the file is already open",
how can make the excel sheet open and the data be saved in the client machine? pls help.
this is my code:

Excel._Application myExcelApp;

Excel.Workbooks myExcelWorkbooks;

Excel.Workbook myExcelWorkbook;

// Excel ._Worksheet myExccelWorksheetToChange;

object misValue = System.Reflection.Missing.Value;

myExcelApp = new Excel.Application();

myExcelApp.Visible = true;

myExcelWorkbooks = myExcelApp.Workbooks;



String fileName = "D:\\marks.xlsx";


myExcelWorkbook = myExcelWorkbooks.Open(fileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);

// string name = System.IO.Path.GetTempPath()+Guid.NewGuid().ToString()+".xlsx";
Excel.Worksheet myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
// myExcelWorksheet.SaveAs("marks");

String cellFormulaAsString = myExcelWorksheet.get_Range("A2", misValue).Formula.ToString();

myExcelWorksheet.get_Range("A1", misValue).Formula = TextBox1.Text;
Posted

1 solution

I'm not sure you clearly understand Server/Client architecture. What you are asking is not directly possible. There are indirect ways to achieve this.

When the user clicks the button, the server is processing the action. Which means that the Excel file operations are done on the server as well. Every web request in IIS is treated as an isolated process or session (unless you do some configuration changes). Which means multiple sessions are trying to access the Excel file and hence the error you are getting. Excel opens files for exclusive access preventing anyone else from accessing it.

To get around this issue, you would need to copy the Excel file to temporary storage with a unique file name. Open it, save the changes to it, close it and prompt the user to download it.
 
Share this answer
 
Comments
sundaram meenakshi 11-Aug-13 23:53pm    
thank you @virusstorm,i will try and let you know :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900