从datagridview vb.net导出到Excel时的颜色单元格

我有代码,在一个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 

此循环中的variablesi用作DataGridView行的索引。 它从零(0)开始不是1.这将跳过DataGridView的第一行。 开始一个零,它似乎正常工作。

其次,我无法得到下面的线路正常工作。 “As(Arsen)”部分似乎被忽略。 我把文本改为“Arsen”,看起来工作正常。

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

将上面的行更改为:

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

通过这些更改,代码似乎按照单列“Arsen”的预期运行。 由于每列可能有相同的值不同的颜色,我猜我缺less的东西。 深入的if-then-else语句看起来过于复杂。 下面的代码使用Select / Switch语句。 为了帮助,创build一个简单的Sub来改变给定列的颜色。 通过这种方式,您可以识别该列,并根据需要为该特定列进行颜色更改。 希望这可以帮助。

对原始代码的更改(有些variables名已更改)在注释中进行标记

 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