Error while saving XLS file. [Resolved]

Posted by Sateeshkumar.Singupuram under C# on 6/21/2013 | Points: 10 | Views : 3222 | Status : [Member] | Replies : 3
hi,
i am trying to create an .xls file from a data table and saving it in application local folder. all this i am doing for a web application. everything is fine until i end with an error. so please look into below error and help me out with a solution

Error :

Microsoft Excel cannot access the file 'E:\Syed Project\E-Comply_Project\E-Comply Source Code\COC_Export\'. 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.


Code :

public void DTToExcel(System.Data.DataTable dt, string filePath)
{
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;


xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


string data = string.Empty;
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
data = dt.Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}
xlWorkBook.SaveAs(filePath + "\\" + "BOM_Matched_ExcelReport.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}catch(Exception Ex)
{ throw Ex; }
}


Thanks & Regards,
Sateesh kumar Singupuram

sateesh kumar singupuram


Responses

Posted by: Justshekhar123 on: 6/24/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Sateesh kumar Singupuram ,

i have reviewed your function in which you are facing the error and tried to implement on my machine and i have got the solution over this.
Just modified your code which is like:

xlWorkBook.SaveAs(filePath + "\\" + "BOM_Matched_ExcelReport.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

Replace this line with :
xlWorkBook.Saved = true;
xlWorkBook.SaveCopyAs(filePath + "\\" + "BOM_Matched_ExcelReport.xls");

If your problem solved with this solution.Please mark this as answer.

Thanks & Regards,
Shekhar Jadhav

Sateeshkumar.Singupuram, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Justshekhar123 on: 6/21/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Sateesh kumar Singupuram ,

As U mentioned the Path ie."E:\Syed Project\E-Comply_Project\E-Comply Source Code\COC_Export\".

Have you Checked that the forlder ie.COC_Export is having the Full access permission ie(for Read,Write,Execute).

Thanks & Regards,
Shekhar Jadhav

Sateeshkumar.Singupuram, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sateeshkumar.Singupuram on: 6/24/2013 [Member] Starter | Points: 25

Up
0
Down
hi Shekhar Jadhav,
thanks for you reply with the clear analysis on this issue. i understood what you are asking for, but unable to point it as i changed the permissions to allow full control over the folder. manually i changed the folder permissions. is anything needs to be done extra to give permissions. my folder is attached to my aasembly. please help me with a solution. thank you.

Regards,
SateeshKumar Singupuram


sateesh kumar singupuram

Sateeshkumar.Singupuram, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response