【发布时间】: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