[datagridview数据导出到带有保存对话框的vb net中的excel

问题描述 投票:0回答:1

我使用了下面的代码,它可以100%工作,但是我需要知道如何保存在保存对话框中。

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim sheetIndex As Integer
        Dim Ex As Object
        Dim Wb As Object
        Dim Ws As Object
        Ex = CreateObject("Excel.Application")
        Wb = Ex.workbooks.add

        ' Copy each DataTable as a new Sheet

        'On Error Resume Next
        Dim col, row As Integer
        ' Copy the DataTable to an object array
        Dim rawData(DataGridView1.Rows.Count, DataGridView1.Columns.Count - 1) As Object

        ' Copy the column names to the first row of the object array

        For col = 0 To DataGridView1.Columns.Count - 1
            rawData(0, col) = DataGridView1.Columns(col).HeaderText.ToUpper

        Next

        For col = 0 To DataGridView1.Columns.Count - 1
            For row = 0 To DataGridView1.Rows.Count - 1
                rawData(row + 1, col) = DataGridView1.Rows(row).Cells(col).Value

            Next
        Next
        ' Calculate the final column letter
        Dim finalColLetter As String = String.Empty
        finalColLetter = ExcelColName(DataGridView1.Columns.Count) 'Generate Excel Column Name (Column ID)


        sheetIndex += 1
        Ws = Wb.Worksheets(sheetIndex)
        'Ws.name = "Test10"
        Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, DataGridView1.Rows.Count + 1)

        Ws.Range(excelRange, Type.Missing).Value2 = rawData
        Ws = Nothing


        Wb.SaveAs("D:\5.xlsx", Type.Missing, Type.Missing,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        Wb.Close(True, Type.Missing, Type.Missing)
        Wb = Nothing
        ' Release the Application object
        Ex.Quit()
        Ex = Nothing
        ' Collect the unreferenced objects
        GC.Collect()
        MsgBox("Exported Successfully.", MsgBoxStyle.Information)
    End Sub
    Public Function ExcelColName(ByVal Col As Integer) As String
        If Col < 0 And Col > 256 Then
            MsgBox("Invalid Argument", MsgBoxStyle.Critical)
            Return Nothing
            Exit Function
        End If
        Dim i As Int16
        Dim r As Int16
        Dim S As String
        If Col <= 26 Then
            S = Chr(Col + 64)
        Else
            r = Col Mod 26
            i = System.Math.Floor(Col / 26)
            If r = 0 Then
                r = 26
                i = i - 1
            End If
            S = Chr(i + 64) & Chr(r + 64)
        End If
        ExcelColName = S
    End Function
End Class
vb.net railstutorial.org
1个回答
0
投票

如果我有问题,我想你可以这样处理:

Dim sfd As New Windows.Forms.SaveFileDialog
sfd.AddExtension = True
sfd.DefaultExt = ".xlsx"   
sfd.FileName = "D:\5.xlsx"
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
   ... your code here ...
End If

因此,您的代码结尾将如下所示:

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim sfd As New Windows.Forms.SaveFileDialog
    sfd.AddExtension = True
    sfd.DefaultExt = ".xlsx"   
    sfd.FileName = "D:\5.xlsx"
    If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
       Export(sfd.FileName)
    End If
End Sub

Private Sub Export(FileName as string)
    Dim sheetIndex As Integer
    Dim Ex As Object
    Dim Wb As Object
    Dim Ws As Object
    Ex = CreateObject("Excel.Application")
    Wb = Ex.workbooks.add

    ' Copy each DataTable as a new Sheet

    'On Error Resume Next
    Dim col, row As Integer
    ' Copy the DataTable to an object array
    Dim rawData(DataGridView1.Rows.Count, DataGridView1.Columns.Count - 1) As Object

    ' Copy the column names to the first row of the object array

    For col = 0 To DataGridView1.Columns.Count - 1
        rawData(0, col) = DataGridView1.Columns(col).HeaderText.ToUpper

    Next

    For col = 0 To DataGridView1.Columns.Count - 1
        For row = 0 To DataGridView1.Rows.Count - 1
            rawData(row + 1, col) = DataGridView1.Rows(row).Cells(col).Value
        Next
    Next
    ' Calculate the final column letter
    Dim finalColLetter As String = String.Empty
    finalColLetter = ExcelColName(DataGridView1.Columns.Count) 'Generate Excel Column Name (Column ID)


    sheetIndex += 1
    Ws = Wb.Worksheets(sheetIndex)
    'Ws.name = "Test10"
    Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, DataGridView1.Rows.Count + 1)

    Ws.Range(excelRange, Type.Missing).Value2 = rawData
    Ws = Nothing


    Wb.SaveAs(FileName, Type.Missing, Type.Missing,
 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
    Wb.Close(True, Type.Missing, Type.Missing)
    Wb = Nothing
    ' Release the Application object
    Ex.Quit()
    Ex = Nothing
    ' Collect the unreferenced objects
    GC.Collect()
    MsgBox("Exported Successfully.", MsgBoxStyle.Information)
End Sub

Public Function ExcelColName(ByVal Col As Integer) As String
    If Col < 0 And Col > 256 Then
        MsgBox("Invalid Argument", MsgBoxStyle.Critical)
        Return Nothing
        Exit Function
    End If
    Dim i As Int16
    Dim r As Int16
    Dim S As String
    If Col <= 26 Then
        S = Chr(Col + 64)
    Else
        r = Col Mod 26
        i = System.Math.Floor(Col / 26)
        If r = 0 Then
            r = 26
            i = i - 1
        End If
        S = Chr(i + 64) & Chr(r + 64)
    End If
    ExcelColName = S
End Function
End Class
© www.soinside.com 2019 - 2024. All rights reserved.