我的表单中有一个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
答案 0 :(得分:1)
答案 1 :(得分:0)
在退出前尝试此操作:
xlApp.DisplayAlerts = False
或者将False
值传递给close方法:
xlWorkBook.Close(False)