Click here to Skip to main content
14,836,641 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

In my application I am exporting values from Grid to Excel and saving the excel file using SaveWorkSpace() method.

I have set a message box when the file is saved successfully.
But if I press cancel, even then it's showing the alert message.

I don't want to show the message when it's cancelled.

Below is my code.

C#
<blockquote class="FQ"><div class="FQA">Quote:</div>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 book = Microsoft.Office.Interop.Excel.Workbooks.Add("") as Excel.Workbook;
            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";

            DateTime currentTime;
            currentTime = DateTime.Now;

            try
            {
                app.SaveWorkspace();
                app.Workbooks.Close();
            }
            catch (Exception ex) { }
            ClientScript.RegisterStartupScript(this.GetType(), "fnCall", "<script language='javascript'>alert('Saved Successfully');</script>");

        }


Please help.
Posted

1 solution

Add this in your code,

string filePath=string.Empty;

....
....
....

try
{
app.SaveWorkspace();
filePath = workbook.Path;
app.Workbooks.Close();
}

catch (Exception ex) { }

check();
if (filePath == "")
{
ClientScript.RegisterStartupScript(this.GetType(), "fnCall", "<script language='javascript'>alert('Saving Cancelled');</script>");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "fnCall", "<script language='javascript'>alert('Saved Successfully');</script>");
}
   

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