我的表单中有一个 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