When the user clicks a button in the webpage, i want to open an Excel file. But it is only working when i debug my code in VS2010. When i just open the webpage and click the button it returns with an error:
Microsoft Excel cannot access the file 'K:\report_SYY_per_month.xlsx'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code...
The code i am currently using is this (not final yet of course..):
protected void ReportButton_Click(object sender, EventArgs e)
{
string path = @"K:\report_SYY_per_month.xlsx";
var excel = new Excel.Application();
excel.Visible = true;
//excel.UserControl = true;
System.Globalization.CultureInfo CI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Excel.Workbooks wbooks = excel.Workbooks;
Excel.Workbook wBook = wbooks.Open(path,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Excel.Sheets sheets = wBook.Worksheets;
string currentSheet = "per_month";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)sheets.get_Item(currentSheet);
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
wbooks.Close();
excel.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.CurrentThread.CurrentCulture = CI;
}