VB.net将datagridview导出到Excel

时间:2017-03-14 11:43:36

标签: excel vb.net datagridview export-to-excel

我有一个DataGridView和一个按钮,可以将DataGridView的值导出为ex​​cel。问题是如何将值设置为不可编辑或仅在将其发送到Excel时才读取?什么是设置值的默认单元格大小的代码?

这是我的Button代码:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim App_xls As Object
    Dim Lig_cpt, Col_cpt As Integer
    App_xls = CreateObject("Excel.Application")
    App_xls.workbooks.add()

    App_xls.visible = True

    Try

        For Col_cpt = 0 To DataGridView1.ColumnCount - 1
            App_xls.ActiveSheet.cells(1, Col_cpt + 1).value = DataGridView1.Columns(Col_cpt).HeaderText
        Next
        For Lig_cpt = 0 To DataGridView1.Rows.Count - 1
            For Col_cpt = 0 To DataGridView1.ColumnCount - 1
                If IsNumeric(DataGridView1.Item(Col_cpt, Lig_cpt).Value) Then
                    App_xls.ActiveSheet.cells(Lig_cpt + 2, Col_cpt + 1).value = CDbl(DataGridView1.Item(Col_cpt, Lig_cpt).Value)
                Else
                    App_xls.ActiveSheet.cells(Lig_cpt + 2, Col_cpt + 1).value = DataGridView1.Item(Col_cpt, Lig_cpt).Value

                End If
            Next
        Next

    Catch ex As Exception

    End Try


End Sub

`

3 个答案:

答案 0 :(得分:0)

默认情况下,所有单元格都设置为工作表不可编辑(已锁定)。但只有在工作表受到保护时,该功能才会生效。您也可以选择设置保护密码。

可用于此目的的函数是Excel.Worksheet.Protect

如果您需要任何单元格可编辑,则必须解锁这些单元格。

答案 1 :(得分:0)

Imports System.Data Imports System.Data.SqlClient Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click



Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")

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

xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox("You can find the file C:\vbexcel.xlsx")

结束子

私有子版本releaseObject(ByVal obj作为对象)     尝试       System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)       obj =无     异常捕获       obj =无     最后       GC.Collect()     结束尝试   结束子 结束班

答案 2 :(得分:-1)

要从DGV导出到Excel,请尝试以下脚本。

.text
ori $t1, $0, 0xffffffff #load 32-bit pattern into $t1

您也可以试试这个。

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data.OleDb

'~~> Define your Excel Objects

Public Class Form1

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For Each col As DataGridViewColumn In Me.DataGridView1.Columns
            xlWorkSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
        Next
        Try
            For CurrentRowIndex = 0 To DataGridView1.RowCount - 1 'current row index
                'For j = 0 To Me.DataGridView1.ColumnCount

                For CurrentColumnIndex = 0 To DataGridView1.ColumnCount - 1 'current column index within row index
                    xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                    xlWorkSheet.Cells(CurrentRowIndex + 3, CurrentColumnIndex + 1) = DataGridView1(CurrentColumnIndex, CurrentRowIndex).Value.ToString()
                Next

                'xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                'xlWorkSheet.Cells(i + 2, j + 1) = Me.DataGridView1(j, i).Value.ToString()
                'xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                'Next
            Next

        Catch ex As Exception
            MsgBox("Unable to extract data" & ex.Message, MsgBoxStyle.Critical)
            Exit Sub
        End Try

        xlWorkBook.Activate()

        '//get path
        Me.FolderBrowserDialog1.ShowDialog()
        Dim path As String = Me.FolderBrowserDialog1.SelectedPath

        xlWorkBook.SaveAs(path & "\Excel_With_Headers.xls")
        'xlWorkSheet.SaveAs("burn permit export.xls")

        xlWorkBook.Close()
        xlApp.Quit()

        'releaseObject(xlApp)
        'releaseObject(xlWorkBook)
        'releaseObject(xlWorkSheet)

        MsgBox("You can find your report at " & path & "\burn permit export.xls")
    End Sub
End Class