0

I am working on windows application using c#, when i try to export to excel from my DataGridView, SaveFileDialog open and if click Save it's working good and getting Excelsheet but when click on Cancel button it's not close SaveFileDialogue. I know there is many similar question available but didn't help me.

Snippet Code Here..

 Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

    try
    {

        worksheet = workbook.ActiveSheet;

        worksheet.Name = "ExportedFromDatGrid";

        int cellRowIndex = 1;
        int cellColumnIndex = 1;

        int count = 1;
        progressBar1.Minimum = 1;


        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)

        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                if (cellRowIndex == 1)
                {
                    worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Columns[j].HeaderText;

                    cellRowIndex++;
                    worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    cellRowIndex = cellRowIndex - 1;
                }
                else
                {
                    cellRowIndex++;

                    worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                    cellRowIndex = cellRowIndex - 1;
                }
                cellColumnIndex++;
            }
            cellColumnIndex = 1;
            cellRowIndex++;

            count++;
            progressBar1.Maximum = count;
            progressBar1.PerformStep();

        }


        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";

        saveDialog.FilterIndex = 2;

        saveDialog.RestoreDirectory = true;
        saveDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

        DialogResult dialogResult = saveDialog.ShowDialog();

        if (dialogResult == DialogResult.OK)
        {
            workbook.SaveAs(saveDialog.FileName);
            MessageBox.Show("Export Successful");
        }
        else if (dialogResult == DialogResult.Cancel)
        {
           if (MessageBox.Show("Are you sure you want to quit without saving?", "Quitting",
                 MessageBoxButtons.YesNo) == DialogResult.No)
            {
                this.DialogResult = DialogResult.None;

            }
        }
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        excel.Quit();
        workbook = null;
        excel = null;

    }

Output :

enter image description here

enter image description here

enter image description here

When i confirm , i want to quit and press on yes, i see that back side another popup is visible i.e image 3. and after clik on cancle button of that, my project work. how this popup is come?

Can anyone point me in the right direction?

Thanks in advance.

Mohit Solanki
  • 261
  • 3
  • 17

1 Answers1

1

The issue is that Excel tries to take care of unsaved files by itself, so when you try to Quit/Close with unsaved files it pop-ups the message. If you do not need it and do not wish to save, you should close the workbook with

workbook.Close(false, misValue, misValue) 

command. See these answers:
How to close-without-save an Excel /xlsm workbook, w/ a custom function, from C# ,
Closing Excel application with Excel Interop without save message

Community
  • 1
  • 1
SashaDu
  • 376
  • 1
  • 5