簡體   English   中英

從多個datagridviews格式化Excel電子表格導出vb.net

[英]Formating excel spread sheet export from multiple datagridviews vb.net

我目前正在使用帶有SQL后端的Microsoft Visual Studio 2013。 我正在加載多個DGV,然后嘗試將其導出到Excel電子表格。 但是,我不確定如何格式化此數據。 當我嘗試導出多個DGV時,它們會彼此覆蓋在excel文件的左上角。 有誰知道如何格式化和選擇要在excel電子表格上加載DGV的位置。 我也不希望它們都放在不同的紙上。 這是我的代碼:

    Private Sub BtnExcelExport_Click(sender As Object, e As EventArgs) Handles BtnExcelExport.Click

    Try

        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 = xlworkbook.Sheets("sheet1")

        For I = 0 To DGVJobs.RowCount - 1
            For J = 0 To DGVJobs.ColumnCount - 1
                For K As Integer = 1 To DGVJobs.Columns.Count
                    xlworksheet.Cells(1, K) = DGVJobs.Columns(K - 1).HeaderText
                    xlworksheet.Cells(I + 2, J + 1) = DGVJobs(J, I).Value.ToString()
                Next
            Next
        Next

        'Here is my second DGV upload out of a few, this one will write over top of the other
        For I = 0 To DGVAssociates.RowCount - 1
            For J = 0 To DGVAssociates.ColumnCount - 1
                For K As Integer = 1 To DGVAssociates.Columns.Count
                    xlworksheet.Cells(1, K) = DGVAssociates.Columns(K - 1).HeaderText
                    xlworksheet.Cells(I + 2, J + 1) = DGVAssociates(J, I).Value.ToString()
                Next
            Next
        Next

        xlworksheet.SaveAs("C:\example.xlsx")
        xlworkbook.Close()
        xlapp.Quit()

        releaseobject(xlapp)
        releaseobject(xlworkbook)
        releaseobject(xlworksheet)


        MsgBox("You can find the file C:\example.xlsx")
        Dim res As MsgBoxResult
        res = MsgBox("Saving completed, would you like to open the file?", MsgBoxStyle.YesNo)
        If (res = MsgBoxResult.Yes) Then
            Process.Start("C:\example.xlsx")
        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub releaseobject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

第二個for循環是我遇到覆蓋問題的地方。 如果任何人都有可以將我發送到良好網站的鏈接,請也將其發布。

為什么您要遍歷兩次列? 最內層的循環是不必要的。

現在,用於指​​定要在excel中寫入的列的變量在For的兩組中都從0變為ColumnCount 這就是為什么他們互相覆蓋的原因。

嘗試這個:

For I = 0 To DGVJobs.RowCount - 1
    For J = 0 To DGVJobs.ColumnCount - 1
        xlworksheet.Cells(1, J + 1) = DGVJobs.Columns(J).HeaderText
        xlworksheet.Cells(I + 2, J + 1) = DGVJobs(J, I).Value.ToString()
    Next
Next

For I = 0 To DGVAssociates.RowCount - 1
    For J = 0 To DGVAssociates.ColumnCount - 1
        xlworksheet.Cells(1, J + 1 + DGVJobs.ColumnCount) = DGVAssociates.Columns(K - 1).HeaderText
        xlworksheet.Cells(I + 2, J + 1 + DGVJobs.ColumnCount) = DGVAssociates(J, I).Value.ToString()
    Next
Next

(順便說一句,離題;將數據導出到excel的更快方法是先創建一個包含所有數據的數組,然后將其分配到工作表中的相應Range

嘗試使用此代碼,那么您肯定會給我發一條感謝消息:)

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO

 Private Sub ExportToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToExcel.Click

    Try
        Dim xlapp As Microsoft.Office.Interop.Excel.Application
        Dim xlworkbook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlworksheet, xlworksheet1 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 = xlworkbook.Sheets("Sheet1")


        For I = 0 To DataGridView_Report.RowCount - 1
            For J = 0 To DataGridView_Report.ColumnCount - 1
                For K As Integer = 1 To DataGridView_Report.Columns.Count
                    xlworksheet.Cells(1, K) = DataGridView_Report.Columns(K - 1).HeaderText
                    xlworksheet.Cells(I + 2, J + 1) = DataGridView_Report(J, I).Value.ToString()
                Next
            Next
        Next

        xlworksheet1 = CType(xlworkbook.Worksheets.Add(), Excel.Worksheet)

        For I = 0 To DataGridView_Calculation.RowCount - 1
            For J = 0 To DataGridView_Calculation.ColumnCount - 1
                For K As Integer = 1 To DataGridView_Calculation.Columns.Count
                    xlworksheet1.Cells(1, K) = DataGridView_Calculation.Columns(K - 1).HeaderText
                    xlworksheet1.Cells(I + 2, J + 1) = DataGridView_Calculation(J, I).Value.ToString()
                Next
            Next
        Next

        xlworksheet.Columns.AutoFit()
        xlworksheet1.Columns.AutoFit()

        Dim xlRange, xlRange1 As Excel.Range
        xlRange = xlworksheet.Range("A1", "N1")
        xlRange1 = xlworksheet1.Range("A1", "L1")
        xlRange.Columns.Font.Bold = True
        xlRange1.Columns.Font.Bold = True

        xlworksheet.Name = "My Sheet1"
        xlworksheet1.Name = "My Sheet2"

        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "Excel Workbook|*.xls|Excel Workbook 2011|*.xlsx"
        saveFileDialog1.Title = "Save Excel File"
        saveFileDialog1.FileName = "Export " & Now.ToShortDateString & ".xlsx"
        saveFileDialog1.ShowDialog()

        saveFileDialog1.InitialDirectory = "C:/"
        If saveFileDialog1.FileName <> "" Then

            Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
            fs.Close()
        End If


        Dim strFileName As String = saveFileDialog1.FileName
        Dim blnFileOpen As Boolean = False


        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
            Exit Sub
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        xlworkbook.SaveAs(strFileName)
        xlapp.Workbooks.Open(strFileName)
        xlapp.Visible = True
        Exit Sub
errorhandler:
            MsgBox(Err.Description)

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub releaseobject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM