2016-11-21 80 views
0
的CANCLE後

我在Windows應用程序的工作使用C#,當我嘗試導出從我的DataGridView,SaveFileDialog開放,如果點擊保存它的工作好,越來越Excelsheet但是當點擊取消脫穎而出按鈕不關閉SaveFileDialogue。 我知道有很多類似的問題,但沒有幫助我。SaveFileDialog無法正常關閉點擊SaveFileDialog

代碼片段在這裏..

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; 

    } 

輸出:

enter image description here

enter image description here

enter image description here

當我確認,我想在是退出,並按,我明白了帽子背面另一個彈出窗口是可見的,即圖像3,並在克拉克的那個按鈕後,我的項目工作。這個彈出窗口是怎麼來的?

任何人都可以指向正確的方向嗎?

在此先感謝。

回答

1

問題是,Excel試圖自己照顧未保存的文件,因此當您嘗試退出/關閉未保存的文件時,它會彈出消息。如果您不需要它並且不希望保存,則應該使用

workbook.Close(false, misValue, misValue) 

命令關閉工作簿。見這些問題的答案:
How to close-without-save an Excel /xlsm workbook, w/ a custom function, from C#
Closing Excel application with Excel Interop without save message

+0

標誌着我的答案是正確的。提供的答案是非常有用的。謝謝@SashaDu –