Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am exporting values from Gridview to excel and I want to save the excel by displaying save as dialog box. But I am getting an error saying

'customer_details.xls' cannot be accessed. The file may be corrupted, location on a server that is not responsing, or read-only. <blockquote class="FQ"><div class="FQA">Quote:</div>


Below is my code:

C#
protected void btnExort_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Visible = false;
            Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add(1);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Workbook objSheet = null;
            Microsoft.Office.Interop.Excel._Workbook objBook = null;
            for (int gridRow = 0; gridRow < gridView.Rows.Count; gridRow++)
            {
                GridViewRow row = gridView.Rows[gridRow];
                string name = ((Label)row.FindControl("Label1")).ToString();
                worksheet.Cells[gridRow + 3, 3] = ((Label)row.FindControl("Label1")).Text;
                worksheet.Cells[gridRow + 3, 2] = ((Label)row.FindControl("Label2")).Text;
                worksheet.Cells[gridRow + 3, 4] = ((Label)row.FindControl("Label3")).Text;
                worksheet.Cells[gridRow + 3, 5] = ((Label)row.FindControl("Label4")).Text;
                worksheet.Cells[gridRow + 3, 6] = ((Label)row.FindControl("Label7")).Text;
                worksheet.Cells[gridRow + 3, 7] = ((Label)row.FindControl("Label8")).Text;
            }

            worksheet.Cells[2, 2] = "Customer Name";
            worksheet.Cells[2, 3] = "Customer ID";
            worksheet.Cells[2, 4] = "Customer Display Name";
            worksheet.Cells[2, 5] = "Customer Email";
            worksheet.Cells[2, 6] = "Service ID";
            worksheet.Cells[2, 7] = "Service Name";
            //excel.Range cells = worksheet.Cells["B2", "D2"]; 
            DateTime currentTime;
            currentTime = DateTime.Now;

            string fileName = "customer_details.xls";
            //+currentTime;
            try
            {
                app.SaveWorkspace("D:\\ExcelWork\\ExportToExcel\\" + fileName);
                app.Workbooks.Close();

            }
            catch (Exception ex) { }
             
        }
Posted
Comments
ZurdoDev 1-Oct-13 7:46am    
SaveWorkspace is not what you want is it? You just want to save the Workbook.

1 solution

May be the file "'customer_details.xls' you are accessing is open. Please confirm that excel file is not open and than try.
 
Share this answer
 

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