|
I am getting this while import and exporting data to Excel file
Quote: Microsoft Office Excel cannot access the file 'C:\usr\SVN\ePROM\EffOnePMA\Files\QuestionStructure.xls'.
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.
Here I am Attacheing my code
private void DownLoadStrcture()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Workbook xlTempWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
range = null;
int rCnt = 2;
int cCnt = 2;
string str;
string fname = "QuestionStructure.xls";
string Filepath = Server.MapPath("~/Files/" + fname);
string TempFilename = "QuestionStructure_ " + Convert.ToInt32(Session[PageBase.SESSION_USER_ID]) + ".xls";
string TempFilepath = Server.MapPath("~/Files/Temp/" + TempFilename);
string dep = ddlDepartment.SelectedItem.Text.ToString();
string area = ddlArea.SelectedItem.Text.ToString();
string category = ddlCategory.SelectedValue != "-1" ? ddlCategory.SelectedItem.Text : "";
string subCategory = ddlSubCategory.SelectedValue != "-1" ? ddlSubCategory.SelectedItem.Text : "";
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(Filepath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
File.Copy(Filepath, TempFilepath);
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
xlApp = new Excel.Application();
xlTempWorkBook = xlApp.Workbooks.Open(TempFilepath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlTempWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
string[] stringArray = new string[] { dep, area, category, subCategory };
for (int i = 0; i < stringArray.Length; i++)
{
(range.Cells[rCnt, cCnt] as Excel.Range).Value2 = stringArray[i].ToString();
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
rCnt++;
}
xlApp.DisplayAlerts = false;
xlApp.ScreenUpdating = false;
xlApp.Visible = false;
xlApp.UserControl = false;
xlApp.Interactive = false;
string Deletefilepath = Server.MapPath("~/Files/Temp/");
if (File.Exists(Deletefilepath + TempFilename))
File.Delete(Deletefilepath + TempFilename);
xlTempWorkBook.Close(SaveChanges: true);
Marshal.ReleaseComObject(xlTempWorkBook);
xlApp.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(xlTempWorkBook);
Marshal.FinalReleaseComObject(range);
Marshal.FinalReleaseComObject(xlApp);
}
|
|
|
|