【问题标题】:Color cells when exporting to Excel from datagridview vb.net从 datagridview vb.net 导出到 Excel 时的颜色单元格
【发布时间】:2017-07-19 08:11:58
【问题描述】:

我有代码可以根据标准为 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

我希望能够引用列标题名称而不是必须引用列号,因为这些可能会有所不同,但名称是不变的。另外,如果所有行都正确着色,我会很高兴...

非常感谢任何帮助。

【问题讨论】:

    标签: excel vb.net datagridview colors export


    【解决方案1】:

    在不知道颜色编码代表什么的情况下,我将此部分留给您决定。从发布的图片中可以看出,颜色不一定代表简单的值,因为相同的值是不同的颜色,具体取决于列。

    先发表评论

    我已经设法得到一个导出,其中着色跳过第一行但其他颜色正确。

    这又是一个简单的索引问题。下面的行循环通过DataGridViews 行。

    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 来更改给定列的颜色。通过这种方式,您可以识别该列并根据该特定列的需要进行颜色更改。希望这会有所帮助。

    对原始代码的更改(更改了一些变量名)在 cmets 中标记

    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
    

    【讨论】:

    • 再次感谢 JohnG。我忘了提到有几个元素具有不同的限制。我不想用太多的代码来混淆这个问题,所以我只包含了第一组元素。您的代码确实为输出着色,但根据一条规则为所有列着色。我将在我的代码中包含其他规则和标题。
    • 你是对的。我可以很容易地看到着色用于您的问题中未定义的内容。因此,在没有这些信息的情况下继续下去是没有意义的。由于每个元素的值可能有不同的颜色编码,我创建了基本的SetColColor 方法,您应该能够修改代码以适应每个元素的不同颜色。您可能会认为每个元素都可以是一个Element 类,并且每个元素对象都将包含着色约束。祝你好运。
    • 再次感谢 JohnG。最后一个问题:我在哪里指定 SetColColor 方法中的列名?我尝试了几种变体,但都没有奏效。
    • 假设您要遍历每个元素列,上面的代码只是遍历从第 2 列开始的所有列。For index As Integer = 2 To dgvElements.Columns.Count 在这里我获取列名称并将该名称传递给SetColColor方法。在此方法中,您可以查看列名 (colName) 并为该元素列应用适当的着色逻辑。同样,由于您没有指定这种着色应该如何工作,所以我让它简单地为所有列着色。我希望这是有道理的。
    • 请查看 SetColColor 方法的更新代码,以适应每个元素的不同值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    相关资源
    最近更新 更多