none
想请帮忙看看我的代码missing了什么 RRS feed

  • 问题

  • 以下是我的代码?

    我的问题是当user save file dialog时user 按cancel时整个代码无法动弹。

     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 i As Integer
            Dim j As Integer

            xlapp = New Microsoft.Office.Interop.Excel.Application
            xlWorkBook = xlapp.Workbooks.Add(misValue)
            xlWorkSheet = CType(xlWorkBook.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)

            For i = 0 To DataGridView2.RowCount - 2
                For j = 0 To DataGridView2.ColumnCount - 1
                    xlWorkSheet.Cells(i + 1, j + 1) = _
                        DataGridView2(j, i).Value.ToString()
                Next
            Next

            Using sfd As New SaveFileDialog
                sfd.Filter = ".xls Files (*.xls)|*.xls"
                If sfd.ShowDialog() = DialogResult.OK Then
                    xlWorkSheet.SaveAs(sfd.FileName)
                    MessageBox.Show(sfd.FileName)
                End If
            End Using

            xlWorkBook.Close()
            xlapp.Quit()

            releaseObject(xlapp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)


            MsgBox("Save file success")

    end sub

    2020年5月6日 1:56

答案

  • Hi christing,

    >>目前遇到的问题是当user save file dialog时,user选择cancel。整个代码会hang 无法动弹

    使用 Return 关键字退出方法:

            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "Execl files (*.xlsx)|*.xlsx"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                xlWorkSheet.SaveAs(saveFileDialog1.FileName)
                MsgBox("Save file success")
            Else
                Return
            End If

    希望可以帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年5月8日 0:51
    2020年5月7日 6:25
    版主

全部回复

  • Hi christing,

    我根据你的代码进行了测试,但是并没有出现乱码的情况。

    为了将文件保存在其他地方,你可以使用 SaveFileDialog 来保存Excel.

    例如:

            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "Execl files (*.xlsx)|*.xlsx"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                xlWorkSheet.SaveAs(saveFileDialog1.FileName)
            End If

    如果你保存到不同路径以后仍然出现乱码的情况,我建议你提供更多关于 DataGridView2 的信息,这样有助于帮助我们重现你的问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2020年5月6日 8:46
    版主
  •  Xingyu Zhao感谢你的回复

    Datagridview 2 呈现的数据是从datagridview1 transfer 过来的。

    我有两个datagridview,datagridview1 是读excel file user 选择要的column并且将数据transfer去datagridview2.

    user convert datagridview2 的数据变成excel file。

    目前遇到的问题是当user save file dialog时,user选择cancel。整个代码会hang 无法动弹

    2020年5月6日 9:12
  • Hi christing,

    >>目前遇到的问题是当user save file dialog时,user选择cancel。整个代码会hang 无法动弹

    使用 Return 关键字退出方法:

            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "Execl files (*.xlsx)|*.xlsx"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                xlWorkSheet.SaveAs(saveFileDialog1.FileName)
                MsgBox("Save file success")
            Else
                Return
            End If

    希望可以帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年5月8日 0:51
    2020年5月7日 6:25
    版主