WPF VB将datagrid(不是datagridview)导出到Excel

时间:2017-02-20 04:57:39

标签: wpf excel vb.net

我在WPF中使用VB.NET。我只知道WPF数据网格似乎与Datagridview不同,下面是通常的VB导出例程

Private Sub BtnExport_Click(sender As Object, e As RoutedEventArgs)
    Dim app As Object
    Dim xlbook As Object
    Dim xlsheet As Object
    app = CreateObject("Excel.Application")
    xlbook = app.Workbooks.Add()
    xlsheet = xlbook.ActiveSheet
    app.Visible = True
    Try
        Dim iX As Integer
        Dim iY As Integer
        Dim iC As Integer
        Dim iz As Integer
        For iC = 0 To DGJrn.Columns.Count - 1
            xlsheet.Cells(1, iC + 1).Value = DGJrn.Columns(iC).Header
        Next

        iz = 1
        For iX = 0 To DGJrn.Rows.Count - 1
            For iY = 0 To DGJrn.Columns.Count - 1
                Dim a As String = DGJrn(iY, iX).Value
                If a <> Nothing Then xlsheet.Cells(iz + 1, iY + 1) = (DGJrn(iY, iX).Value & "").ToString()
            Next
            iz = iz + 1
        Next
        app.Visible = True
        app.UserControl = True
        releaseobject(app)
        releaseobject(xlbook)
        releaseobject(xlsheet)
    Catch ex As Exception
        'MessageBox.Show(ex.ToString())
    End Try
    AppActivate(System.Diagnostics.Process.GetCurrentProcess.Id)
    MsgBox("Data has been successfuly generated!")

End Sub

我总是失败,“datagrid无法编入索引”你能帮帮忙吗? 编辑:添加xml和绑定

<DataGrid x:Name="DGJrn" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,10,39" AutoGenerateColumns="False" ItemsSource="{Binding}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="id" Binding="{Binding Path=ID}" />
                            <DataGridTextColumn Header="JTYPE" Binding="{Binding Path=JType}" />
                            <DataGridTextColumn Header="JSRC" Binding="{Binding Path=JSrc}" />
                            <DataGridTextColumn Header="TRNDT" Binding="{Binding Path=TrnDt, StringFormat=\{0:dd/MM/yyyy\}}" />
                            <DataGridTextColumn Header="Period" Binding="{Binding Path=Period}" />
                            <DataGridTextColumn Header="Desc" Binding="{Binding Path=Desc}" />
                            <DataGridTextColumn Header="TrnRef" Binding="{Binding Path=TrnRef}" />
                            <DataGridTextColumn Header="AccCode" Binding="{Binding Path=AccCode}" />
                            <DataGridTextColumn Header="BASum" Binding="{Binding Path=BASum}" />
                            <DataGridTextColumn Header="DbtCrd" Binding="{Binding Path=DbtCrd}" />
                            <DataGridTextColumn Header="OtAmt" Binding="{Binding Path=OtAmt}" />
                            <DataGridTextColumn Header="T1" Binding="{Binding Path=T1}" />
                            <DataGridTextColumn Header="T2" Binding="{Binding Path=T2}" />
                            <DataGridTextColumn Header="T3" Binding="{Binding Path=T3}" />
                            <DataGridTextColumn Header="T4" Binding="{Binding Path=T4}" />
                            <DataGridTextColumn Header="T5" Binding="{Binding Path=T5}" />
                            <DataGridTextColumn Header="T6" Binding="{Binding Path=T6}" />
                            <DataGridTextColumn Header="T7" Binding="{Binding Path=T7}" />
                            <DataGridTextColumn Header="T8" Binding="{Binding Path=T8}" />
                            <DataGridTextColumn Header="Remrk" Binding="{Binding Path=Remrk}" />
                        </DataGrid.Columns>
                    </DataGrid>

连接:

Private Sub LoadGrid()
    Dim DA As New OleDbDataAdapter
    Dim DT As New System.Data.DataTable
    Dim Cmd As New OleDbCommand

    With ConAccess
        Try
            If .State = ConnectionState.Open Then .Close()
            .Open()
        Catch ex As Exception
            MsgBox("Error Connection : " & ex.Message)
        End Try
    End With
    Dim Sql As String = ""
    Sql = "SELECT * FROM APJI;"

    Cmd = New OleDbCommand(Sql, ConAccess)
    DA.SelectCommand = Cmd
    DA.Fill(DT)

    DGJrn.ItemsSource = DT.DefaultView

End Sub

1 个答案:

答案 0 :(得分:0)

需要关闭我自己的问题,所以我需要将源从DataGrid更改为DataTable。所以现在这是我的代码:

Private Sub DatatableToExcel()
    Dim _excel As New Microsoft.Office.Interop.Excel.Application
    Dim wBook As Microsoft.Office.Interop.Excel.Workbook
    Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

    wBook = _excel.Workbooks.Add()
    wSheet = wBook.ActiveSheet()

    Dim dt As System.Data.DataTable = Dtab
    Dim dc As System.Data.DataColumn
    Dim dr As System.Data.DataRow
    Dim colIndex As Integer = 0
    Dim rowIndex As Integer = 0

    For Each dc In dt.Columns
        colIndex = colIndex + 1
        _excel.Cells(1, colIndex) = dc.ColumnName
    Next

    For Each dr In dt.Rows
        rowIndex = rowIndex + 1
        colIndex = 0
        For Each dc In dt.Columns
            colIndex = colIndex + 1
            _excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
        Next
    Next

    wSheet.Columns.AutoFit()
    Dim strFileName As String = "C:\datatable.xlsx"
    If System.IO.File.Exists(strFileName) Then
        System.IO.File.Delete(strFileName)
    End If

    wBook.SaveAs(strFileName)
    wBook.Close()
    _excel.Quit()
End Sub