我有一个工作代码,它将Excel数据导入数据网格视图,并根据每列的不同限制值应用背景颜色。我想将它导出到Excel时保留背景颜色,但我无法找到如何在我的代码中实现它。
首先举例说明如何将背景颜色应用于datagridview:
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
For i As Double = 0 To Me.DataGridView2.Rows.Count - 1
If Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value < Ul1Zn Then
Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Style.BackColor = Color.DodgerBlue
ElseIf Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value >= Ul1Zn And Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value < Ul2Zn Then
Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Style.BackColor = Color.LawnGreen
ElseIf Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value >= Ul2Zn And Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value < Ul3Zn Then
Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Style.BackColor = Color.Yellow
ElseIf Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value >= Ul3Zn And Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value < Ul4Zn Then
Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Style.BackColor = Color.Orange
ElseIf Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value >= Ul4Zn And Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value < Ul5Zn Then
Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Style.BackColor = Color.Red
ElseIf Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Value >= Ul5Zn Then
Me.DataGridView2.Rows(i).Cells("Zn (Sink)").Style.BackColor = Color.BlueViolet
End If
Next
(编辑)这是我的导出代码:
Private Sub ExportToExcel()
' 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 = "ExportedFromDatGrid"
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'Loop through each row and read value from each column.
For i As Integer = 0 To DataGridView2.Rows.Count - 2
For j As Integer = 0 To DataGridView2.Columns.Count - 1
' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
If cellRowIndex = 1 Then
worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView2.Columns(j).HeaderText
Else
worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView2.Rows(i).Cells(j).Value
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 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
'Gir ikke feilmelding, men lager heller ikke farge
For i As Integer = 0 To DataGridView2.Rows.Count - 2
For j As Integer = 0 To DataGridView2.Columns.Count - 1
If Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL1As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL1As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL2As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL2As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL3As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL3As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul4As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul4As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul5As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul5As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next
'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 = 2
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
进展!我删除了关于“如果列名是As(Arsen)......”的论点,但是现在整行变为彩色,我只希望相关列下的各个单元格受到影响。显然,我的原始列指定参数不起作用。有谁猜到它出了什么问题?
以前的代码。没有彩色背景,但没有崩溃:
For i As Integer = 0 To DataGridView2.Rows.Count - 2
For j As Integer = 0 To DataGridView2.Columns.Count - 1
If Me.DataGridView2.CurrentCell.OwningColumn.Name = "As (Arsen)" And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL1As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
ElseIf Me.DataGridView2.CurrentCell.OwningColumn.Name = "As (Arsen)" And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL1As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL2As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
ElseIf Me.DataGridView2.CurrentCell.OwningColumn.Name = "As (Arsen)" And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL2As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL3As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
ElseIf Me.DataGridView2.CurrentCell.OwningColumn.Name = "As (Arsen)" And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL3As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul4As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
ElseIf Me.DataGridView2.CurrentCell.OwningColumn.Name = "As (Arsen)" And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul4As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul5As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
ElseIf Me.DataGridView2.CurrentCell.OwningColumn.Name = "As (Arsen)" And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul5As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next