簡體   English   中英

從datagridview vb.net導出到Excel時的顏色單元格

[英]Color cells when exporting to Excel from datagridview vb.net

我有基於標准為datagridview中的單元格着色的代碼。 這可以正常工作。

應用程序內部的着色程序

下面顯示的代碼檢查標題“ As(Arsen)”下的值,並將其與為ULxAS定義的定義限制進行比較,並相應地上色。 結果是正確的,並且如上所述。

 Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

    'If Not IsNothing(DataGridView2.Rows("As (Arsen)")) Then

    'Kategorier i kolonner (vanlig)
    'As
    Dim UL1As As Double = 8
    Dim UL2As As Double = 20
    Dim UL3As As Double = 50
    Dim Ul4As As Double = 600
    Dim Ul5As As Double = 1000

    If Me.DataGridView2.CurrentRow.Cells(0).Value Is DBNull.Value Then
        MessageBox.Show("Cellen er tom.")
    Else

        For i As Integer = 0 To Me.DataGridView2.Rows.Count - 1
            If Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL1As Then
                Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.DodgerBlue
            ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL1As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL2As Then
                Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.LawnGreen
            ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL2As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL3As Then
                Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.Yellow
            ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL3As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul4As Then
                Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.Orange
            ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul4As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul5As Then
                Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.Red
            ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul5As Then
                Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.BlueViolet
            End If
        Next

但是,當我嘗試將相同的邏輯應用於導出例程時,我無法使其正常工作。 我設法導出了顏色跳過第一行但其他顏色正確的輸出。 我可以使它起作用的唯一方法是對列號進行硬編碼。

Private Sub ExportToExcel()
    'Annen fungerende eksportrutine. Formaterer!
    ' Creating a Excel object.
    Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
    Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
    Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing

    Try

        worksheet = workbook.ActiveSheet

        worksheet.Name = "ExportedFromDataGrid"

        Dim cellRowIndex As Integer = 1
        Dim cellColumnIndex As Integer = 1

        'For i = 0 To DataGridView1.RowCount - 2
        '    For j = 0 To DataGridView1.ColumnCount - 1
        '            worksheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
        '            worksheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
        '        Next
        '    Next
        'Next

        'gets header rows.
        For Each column In DataGridView2.Columns
            worksheet.Cells(1, column.Index + 1).Value = column.Name
        Next


        'gets all other rows
        Dim rowIndex = 2
        For Each row As DataGridViewRow In DataGridView2.Rows
            If Not row.IsNewRow Then
                For colIndex As Integer = 0 To DataGridView2.Columns.Count - 1
                    worksheet.Cells(rowIndex, colIndex + 1).Value = row.Cells(colIndex).Value.ToString
                Next
            End If
            rowIndex += 1
        Next


        'As
        Dim UL1As As Double = 8
        Dim UL2As As Double = 20
        Dim UL3As As Double = 50
        Dim Ul4As As Double = 600
        Dim Ul5As As Double = 1000

        Dim Ul1Cd As Double = 1.5
        Dim Ul2Cd As Double = 10
        Dim Ul3Cd As Double = 15
        Dim Ul4Cd As Double = 30
        Dim Ul5Cd As Double = 1000

        Dim Ul1Cr As Double = 50
        Dim Ul2Cr As Double = 200
        Dim Ul3Cr As Double = 500
        Dim Ul4Cr As Double = 2800
        Dim Ul5Cr As Double = 25000

        Dim Ul1Cu As Double = 100
        Dim Ul2Cu As Double = 200
        Dim Ul3Cu As Double = 1000
        Dim Ul4Cu As Double = 8500
        Dim Ul5Cu As Double = 25000

        Dim Ul1Hg As Double = 1
        Dim Ul2Hg As Double = 2
        Dim Ul3Hg As Double = 4
        Dim Ul4Hg As Double = 10
        Dim Ul5Hg As Double = 1000

        Dim Ul1Ni As Double = 60
        Dim Ul2Ni As Double = 135
        Dim Ul3Ni As Double = 200
        Dim Ul4Ni As Double = 1200
        Dim Ul5Ni As Double = 2500

        Dim Ul1Pb As Double = 60
        Dim Ul2Pb As Double = 100
        Dim Ul3Pb As Double = 300
        Dim Ul4Pb As Double = 700
        Dim Ul5Pb As Double = 2500

        Dim Ul1Zn As Double = 200
        Dim Ul2Zn As Double = 500
        Dim Ul3Zn As Double = 1000
        Dim Ul4Zn As Double = 5000
        Dim Ul5Zn As Double = 25000

        'Virker! Men forskyver fargene opp ett hakk.

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.Columns(i).HeaderText = ("As (Arsen)") And DataGridView2.Rows(i).Cells(i).Value < UL1As Then
                    worksheet.Cells(i, j).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.Columns(i).HeaderText = ("As (Arsen)") And DataGridView2.Rows(i).Cells(i).Value >= UL1As And worksheet.Rows(i).Cells(i).Value < UL2As Then
                    worksheet.Cells(i, j).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.Columns(i).HeaderText = ("As (Arsen)") And DataGridView2.Rows(i).Cells(i).Value >= UL2As And worksheet.Rows(i).Cells(i).Value < UL3As Then
                    worksheet.Cells(i, j).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.Columns(i).HeaderText = ("As (Arsen)") And DataGridView2.Rows(i).Cells(i).Value >= UL3As And worksheet.Rows(i).Cells(i).Value < Ul4As Then
                    worksheet.Cells(i, j).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.Columns(i).HeaderText = ("As (Arsen)") And DataGridView2.Rows(i).Cells(i).Value >= Ul4As And worksheet.Rows(i).Cells(i).Value < Ul5As Then
                    worksheet.Cells(i, j).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.Columns(i).HeaderText = ("As (Arsen)") And DataGridView2.Rows(i).Cells(i).Value >= Ul5As Then
                    worksheet.Cells(i, j).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cd (Kadmium)" Then

        'Cd


        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = ("Cd (Kadmium)") And DataGridView2.Rows(i).Cells(j).Value < Ul1Cd Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = ("Cd (Kadmium)") And DataGridView2.Rows(i).Cells(j).Value >= Ul1Cd And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Cd Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = ("Cd (Kadmium)") And DataGridView2.Rows(i).Cells(j).Value >= Ul2Cd And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Cd Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = ("Cd (Kadmium)") And DataGridView2.Rows(i).Cells(j).Value >= Ul3Cd And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Cd Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = ("Cd (Kadmium)") And DataGridView2.Rows(i).Cells(j).Value >= Ul4Cd And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Cd Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Kadmium" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Cd Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" Then

        'Cr

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" And DataGridView2.Rows(i).Cells(j).Value < Ul1Cr Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" And DataGridView2.Rows(i).Cells(j).Value >= Ul1Cr And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Cr Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" And DataGridView2.Rows(i).Cells(j).Value >= Ul2Cr And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Cr Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" And DataGridView2.Rows(i).Cells(j).Value >= Ul3Cr And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Cr Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" And DataGridView2.Rows(i).Cells(j).Value >= Ul4Cr And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Cr Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cr (Krom)" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Cr Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next


        'ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" Then

        'Cu


        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" And DataGridView2.Rows(i).Cells(j).Value < Ul1Cu Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" And DataGridView2.Rows(i).Cells(j).Value >= Ul1Cu And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Cu Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" And DataGridView2.Rows(i).Cells(j).Value >= Ul2Cu And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Cu Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" And DataGridView2.Rows(i).Cells(j).Value >= Ul3Cu And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Cu Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" And DataGridView2.Rows(i).Cells(j).Value >= Ul4Cu And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Cu Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Cu (Kopper)" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Cu Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'Hg

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = "Hg (Kvikksølv)" And DataGridView2.Rows(i).Cells(j).Value < Ul1Hg Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Hg (Kvikksølv)" And DataGridView2.Rows(i).Cells(j).Value >= Ul1Hg And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Hg Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Hg (Kvikksølv)" And DataGridView2.Rows(i).Cells(j).Value >= Ul2Hg And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Hg Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Hg (Kvikksølv)" And DataGridView2.Rows(i).Cells(j).Value >= Ul3Hg And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Hg Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Hg (Kvikksølv)" And DataGridView2.Rows(i).Cells(j).Value >= Ul4Hg And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Hg Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Hg (Kvikksølv)" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Hg Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'Ni

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = "Ni (Nikkel)" And DataGridView2.Rows(i).Cells(j).Value < Ul1Ni Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Ni (Nikkel)" And DataGridView2.Rows(i).Cells(j).Value >= Ul1Ni And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Ni Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Ni (Nikkel)" And DataGridView2.Rows(i).Cells(j).Value >= Ul2Ni And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Ni Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Ni (Nikkel)" And DataGridView2.Rows(i).Cells(j).Value >= Ul3Ni And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Ni Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Ni (Nikkel)" And DataGridView2.Rows(i).Cells(j).Value >= Ul4Ni And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Ni Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Ni (Nikkel)" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Ni Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'Pb

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = "Pb (Bly)" And DataGridView2.Rows(i).Cells(j).Value < Ul1Pb Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Pb (Bly)" And DataGridView2.Rows(i).Cells(j).Value >= Ul1Pb And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Pb Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Pb (Bly)" And DataGridView2.Rows(i).Cells(j).Value >= Ul2Pb And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Pb Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Pb (Bly)" And DataGridView2.Rows(i).Cells(j).Value >= Ul3Pb And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Pb Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Pb (Bly)" And DataGridView2.Rows(i).Cells(j).Value >= Ul4Pb And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Pb Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Pb (Bly)" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Pb Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'Zn
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            For j As Integer = 0 To -1
                If DataGridView2.CurrentCell.OwningColumn.Name = "Zn (Sink)" And DataGridView2.Rows(i).Cells(j).Value < Ul1Zn Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Zn (Sink)" And DataGridView2.Rows(i).Cells(j).Value >= Ul1Zn And Me.DataGridView2.Rows(i).Cells(j).Value < Ul2Zn Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Zn (Sink)" And DataGridView2.Rows(i).Cells(j).Value >= Ul2Zn And Me.DataGridView2.Rows(i).Cells(j).Value < Ul3Zn Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Zn (Sink)" And DataGridView2.Rows(i).Cells(j).Value >= Ul3Zn And Me.DataGridView2.Rows(i).Cells(j).Value < Ul4Zn Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Zn (Sink)" And DataGridView2.Rows(i).Cells(j).Value >= Ul4Zn And Me.DataGridView2.Rows(i).Cells(j).Value < Ul5Zn Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                ElseIf DataGridView2.CurrentCell.OwningColumn.Name = "Zn (Sink)" And DataGridView2.Rows(i).Cells(j).Value >= Ul5Zn Then
                    worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
                End If
                cellRowIndex += 1
            Next
            'cellColumnIndex = 1
            'cellRowIndex += 1
        Next

        'End If

        'Else
        'MessageBox.Show("Cellen er tom")
        'Exit Sub
        'End If
        'Getting the location And file name of the excel to save from user.
        Dim saveDialog As New SaveFileDialog()
        saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
        saveDialog.FilterIndex = 1

        If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            workbook.SaveAs(saveDialog.FileName)
            MessageBox.Show("Export Successful")
        End If
    Catch ex As System.Exception
        MessageBox.Show(ex.Message)
    Finally
        excel.Quit()
        workbook = Nothing
        excel = Nothing
    End Try

End Sub

我希望能夠引用列標題名稱,而不必引用列號,因為它們可能會有所不同,但名稱是不變的。 另外,如果所有行的顏色正確,我也會喜歡的。

任何幫助是極大的贊賞。

在不知道顏色編碼代表什么的情況下,我將這一部分留給您決定。 從發布的圖片看來,顏色不一定表示簡單值,因為相同的值是不同的顏色,具體取決於列。

首先發表你的評論

我設法導出了顏色跳過第一行但其他顏色正確的輸出。

同樣,這是一個簡單的索引問題。 下面的行遍歷DataGridView的行。

For i As Integer = 1 To DataGridView2.Rows.Count - 1

此循環中的變量i用作DataGridView行的索引。 它從零(0)而不是1開始。這將跳過DataGridView的第一行。 從零開始,它似乎可以正常工作。

其次,我無法使下面的行正常工作。 “ As(Arsen)”部分似乎被忽略了。 我將文本更改為“ Arsen”,它似乎可以正常工作。

DataGridView2.Rows(i).Cells("As (Arsen)").Value

將上面的行更改為:

DataGridView2.Rows(i).Cells("Arsen").Value

進行了這些更改之后,代碼似乎按預期運行了單列“ Arsen”。 由於似乎每列的相同值可能具有不同的顏色,所以我想我缺少了一些東西。 深層的if-then-else語句看起來過於復雜。 下面的代碼改為使用Select / Switch語句。 為了幫助您,將創建一個簡單的Sub來更改給定列的顏色。 這樣,您可以識別列並根據需要更改特定列的顏色。 希望這可以幫助。

在注釋中標記了對原始代碼的更改(更改了一些變量名)

Private Sub ExportToExcel()
  Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
  Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
  Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
  excel.Visible = True
  Try
    worksheet = workbook.ActiveSheet
    worksheet.Name = "ExportedFromDataGrid"
    Dim cellRowIndex As Integer = 1
    Dim cellColumnIndex As Integer = 1
    'gets header rows.
    For Each column In dgvElements.Columns
      worksheet.Cells(1, column.Index + 1).Value = column.Name
    Next
    'gets all other rows
    Dim rowIndex = 2
    For Each row As DataGridViewRow In dgvElements.Rows
      If Not row.IsNewRow Then
        For colIndex As Integer = 0 To dgvElements.Columns.Count - 1
          worksheet.Cells(rowIndex, colIndex + 1).Value = row.Cells(colIndex).Value.ToString
        Next
      End If
      rowIndex += 1
    Next

    ' Substituted code below that loops through each column with data
    ' then sets the color for each of those columns by calling the SetColColor method
    For index As Integer = 2 To dgvElements.Columns.Count - 1
      Dim colName = dgvElements.Columns(index).Name
      SetColColor(worksheet, colName, index + 1)
    Next
    MessageBox.Show("Closing excel: save if needed!")
    'workbook.SaveAs("YourFileName..",)
    workbook.Close()
    excel.Quit()
    Marshal.ReleaseComObject(worksheet)
    Marshal.ReleaseComObject(workbook)
    Marshal.ReleaseComObject(excel)
  Catch
    MessageBox.Show("Error")
  End Try
End Sub

設置列顏色方法。 您可能需要對此進行調整。

Private Sub SetColColor(worksheet As Microsoft.Office.Interop.Excel._Worksheet, colName As String, colIndex As Integer)

  Dim UL1As As Double = 8
  Dim UL2As As Double = 20
  Dim UL3As As Double = 50
  Dim Ul4As As Double = 600
  Dim Ul5As As Double = 1000

  Dim rIndex = 2
  Dim cIndex = colIndex

  For Each row As DataGridViewRow In dgvElements.Rows
    Dim curValue = row.Cells(colName).Value
    Select Case curValue
      Case >= Ul5As
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
      Case >= Ul4As
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
      Case >= UL3As
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
      Case >= UL2As
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
      Case >= UL1As
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
      Case Else
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
    End Select
    rIndex += 1
  Next
End Sub

更新編輯以對每個元素使用不同的着色值

Private Sub SetColColor(worksheet As Microsoft.Office.Interop.Excel._Worksheet, colName As String, colIndex As Integer)
  Dim rIndex = 2
  Dim cIndex = colIndex
  Dim ULArray = GetElementColorsValues(colName)

  For Each row As DataGridViewRow In dgvElements.Rows
    Dim curValue = row.Cells(colName).Value
    Select Case curValue
      Case >= ULArray(4)
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
      Case >= ULArray(3)
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
      Case >= ULArray(2)
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
      Case >= ULArray(1)
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
      Case >= ULArray(0)
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
      Case Else
        worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
    End Select
    rIndex += 1
  Next
End Sub

獲取每個元素的顏色值的方法

 Public Function GetElementColorsValues(elementName As String) As Integer()
  Dim ULArray(5) As Integer
  Select Case elementName
    Case "Arsenic"
      ULArray(0) = 8
      ULArray(1) = 20
      ULArray(2) = 50
      ULArray(3) = 600
      ULArray(4) = 1000
    Case "Cadmium"
      ULArray(0) = 1.5
      ULArray(1) = 10
      ULArray(2) = 15
      ULArray(3) = 30
      ULArray(4) = 1000
    Case "Chromium"
      ULArray(0) = 50
      ULArray(1) = 200
      ULArray(2) = 500
      ULArray(3) = 2800
      ULArray(4) = 25000
    Case "Copper"
      ULArray(0) = 100
      ULArray(1) = 200
      ULArray(2) = 1000
      ULArray(3) = 8500
      ULArray(4) = 25000
    Case "Mercury"
      ULArray(0) = 1
      ULArray(1) = 2
      ULArray(2) = 4
      ULArray(3) = 10
      ULArray(4) = 1000
    Case "Nickle"
      ULArray(0) = 60
      ULArray(1) = 135
      ULArray(2) = 200
      ULArray(3) = 1200
      ULArray(4) = 2500
    Case "Lead"
      ULArray(0) = 60
      ULArray(1) = 100
      ULArray(2) = 300
      ULArray(3) = 700
      ULArray(4) = 2500
    Case "Zinc"
      ULArray(0) = 200
      ULArray(1) = 500
      ULArray(2) = 1000
      ULArray(3) = 5000
      ULArray(4) = 25000
  End Select
  Return ULArray
End Function

暫無
暫無

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

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