希望导出datagridview

时间:2013-12-30 22:13:19

标签: vb.net excel datagridview export

我有一个滚动的datagridview,它可以动态地改变它的列数和行数。我正在寻找一种方法来导出datagridview,就像用户单击按钮时一样。我希望它导出为无法编辑的内容(因此不是excel文件)。我已经尝试使用iTextSharp导出为pdf,似乎无法想出一个适合它的动态变化循环。此外,我还没有找到任何使用iTextSharp的示例代码,其中还包括导出行标题和列标题。我还尝试了一个解决方案(我现在似乎无法找到)在微软论坛上利用Excel的附加功能在datagridview内容导出后写入PDF。我遇到的问题是它创建了许多页面的方式,并且仍然显示已从datagridveiw中隐藏或删除的行。知道如何才能完成这项壮举吗?

我将包含两张图片,以显示动态填充datagridview的方式和更改

最多可包含66行和12列(不包括行标题或列标题) datagridview example 1 http://i44.tinypic.com/4i0zgo.png datagridview example 2 http://i41.tinypic.com/9pols3.png

2 个答案:

答案 0 :(得分:1)

您可以使用EPPlus创建受密码保护(已锁定)的 Excel文件,该文件可以阅读但不能编辑。我可以告诉你如何从这样的情况开始:

enter image description here

并获取这样的受保护文件:

enter image description here


首先,您必须下载并在项目中包含EPPlus Library。别忘了查看精彩的EPPlusSamples Project

然后你需要一种方法来提取你的DataGridView VISIBLE 数据(你不希望导出不可见的列)。我是用二维数组做的。此函数接受DataGridView参数并返回2d数组:

Function GetDataGridView2DArray(ByVal dataGridView As DataGridView) As String(,)
    'Save list of visible columns
    Dim nrVisibleColumns = (From c As DataGridViewColumn In DataGridView1.Columns
               Where c.Visible
               Select c).ToList()

    'create 2d-array to store values, dimensions given by number of rows and visible columns
    Dim dgvArray(nrVisibleColumns.Count, DataGridView1.Rows.Count) As String

    'create the first row with Column Headers text
    For Each col As DataGridViewColumn In nrVisibleColumns
        dgvArray(col.Index + 1, 0) = col.HeaderText
    Next

    'create Rows, including Row Header text
    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim rowNumber = row.Index + 1
        dgvArray(0, rowNumber) = DataGridView1.Rows(row.Index).HeaderCell.Value 'save rowheader cell value 

        For Each col As DataGridViewColumn In nrVisibleColumns
            dgvArray(col.Index + 1, rowNumber) = DataGridView1(col.Index, row.Index).Value
        Next
    Next

    Return dgvArray
End Function

现在您已拥有阵列,您可以创建 Excel文件并填充数据。另外,在保存之前,我们会用密码锁定它以防止用户编辑。

Private Sub CreateXls()
    Dim fi = New FileInfo("C:\temp\output.xlsx")
    Dim package As New ExcelPackage()

    Dim ws = package.Workbook.Worksheets.Add("Dgv Output") 'create sheet

    'get array of datagridview data
    Dim dataArray = GetDataGridView2DArray(DataGridView1)

    'loop my 2d array and fill my excel file
    For iColumn As Integer = dataArray.GetLowerBound(0) To dataArray.GetUpperBound(0)
        For iRow As Integer = dataArray.GetLowerBound(1) To dataArray.GetUpperBound(1)
            ws.Cells(iRow + 1, iColumn + 1).Value = dataArray(iColumn, iRow)
            ws.Cells(iRow + 1, iColumn + 1).Style.Locked = True 'lock the cell
        Next 
    Next

    ws.Cells.AutoFitColumns() 'resize columns to fit
    ws.Protection.AllowFormatColumns = True 'let user resize columns if he wants
    ws.Protection.SetPassword("1") 'protect the sheet from editing

    package.SaveAs(fi) 'save file
End Sub

现在,您只需点击一下即可轻松导出动态 DataGridView

答案 1 :(得分:0)

这是我的编码测试和输出是完美的:

    Dim ExcelApp As Object, ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer
    Dim j As Integer

    'create object of excel
    ExcelApp = CreateObject("Excel.Application")
    ExcelBook = ExcelApp.WorkBooks.Add
    ExcelSheet = ExcelBook.WorkSheets(1)

    With ExcelSheet
        For Each col As DataGridViewColumn In Me.DataGridView1.Columns
            ExcelSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
            For i = 1 To Me.DataGridView1.RowCount
                ExcelSheet.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("First Column Name").Value
                For j = 1 To DataGridView1.Columns.Count - 1
                    ExcelSheet.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
                Next
            Next
        Next
    End With
    ExcelApp.Visible = True
    ExcelSheet = Nothing
    ExcelBook = Nothing
    ExcelApp = Nothing