2

我的表单中有一个 SaveAs 对话框来保存 Excel 工作表。当我单击对话框中的取消时,会出现另一个消息框,询问“是否要保存对 Book1 所做的更改?”。我希望摆脱这个消息框,因为用户已经决定不通过单击取消来保存 excel...请帮助..谢谢...

         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    Dim myArray(DataGridView1.Rows.Count - 1, DataGridView1.Columns.Count - 1) As Object

    Dim r As Integer
    Dim c As Integer

    xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = CType(xlWorkBook.Worksheets.Item("sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
                                                                                                                 For r = 0 To DataGridView1.Rows.Count - 1
        For c = 0 To DataGridView1.Columns.Count - 1
            myArray(r, c) = DataGridView1.Rows(r).Cells(c).Value
        Next cc 
       xlWorkSheet.Range("A1").Resize(DataGridView1.Rows.Count, DataGridView1.Columns.Count).Value = myArray
      Dim saveFileDialog As New SaveFileDialog()
    saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
    saveFileDialog.FilterIndex = 2
    saveFileDialog.RestoreDirectory = True

    If saveFileDialog.ShowDialog() = DialogResult.OK Then
        xlWorkSheet.SaveAs(saveFileDialog.FileName)
        MsgBox(" Your file has been saved")

    End If


    xlWorkBook.Close()
    xlApp.Quit()
    xlApp = Nothing
    xlWorkBook = Nothing
    xlWorkSheet = Nothing                                                                                            
 End Sub
4

2 回答 2

1

使用 false 调用 close 方法以不显示对话框。

xlWorkBook.Close(False)

更多关于 MSDN

于 2013-07-31T12:27:12.967 回答
0

退出前试试这个:

xlApp.DisplayAlerts = False

或者将False值传递给 close 方法:

xlWorkBook.Close(False)
于 2013-07-31T12:24:00.350 回答