我需要知道如何将行从DataGridView导出到Excel,包括它们的行颜色。
此时我导出它们但颜色为空白/白色/默认颜色。
我需要在vb.net中执行此操作
答案 0 :(得分:1)
你可以尝试这个:
For Each drow As DataGridViewRow In datagrid1.Rows
xcel.Cells(yourRowInt, yourColInt).Interior.Color = drow.DefaultCellStyle.BackColor
Next
答案 1 :(得分:0)
你能这样试试吗?
Private Sub button5_Click(ByVal sender As Object, ByVal e As EventArgs)
Const WORKSHEETSTARTROW As Integer = 1
Const WORKSHEETSTARTCOL As Integer = 1
Dim excelApp = New Excel.Application
excelApp.Visible = true
Dim excelbk As Excel.Workbook = excelApp.Workbooks.Add(Type.Missing)
Dim xlWorkSheet1 As Excel.Worksheet = CType(excelbk.Worksheets("Sheet1"),Excel.Worksheet)
Dim worksheetRow As Integer = WORKSHEETSTARTROW
Dim rowCount As Integer = 0
Do While (rowCount _
< (dataGridView1.Rows.Count - 1))
Dim worksheetcol As Integer = WORKSHEETSTARTCOL
Dim colCount As Integer = 0
Do While (colCount _
< (dataGridView1.Columns.Count - 1))
Dim xlRange As Excel.Range = CType(xlWorkSheet1.Cells(WORKSHEETSTARTROW, worksheetcol),Excel.Range)
xlRange.Value2 = dataGridView1.Columns(colCount).Name
worksheetcol = (worksheetcol + 1)
If (Not (dataGridView1.Rows(rowCount).Cells(colCount).Style.Font) Is Nothing) Then
xlRange.Font.Bold = dataGridView1.Rows(rowCount).Cells(colCount).Style.Font.Bold
xlRange.Font.Italic = dataGridView1.Rows(rowCount).Cells(colCount).Style.Font.Italic
xlRange.Font.Underline = dataGridView1.Rows(rowCount).Cells(colCount).Style.Font.Underline
xlRange.Font.FontStyle = dataGridView1.Rows(rowCount).Cells(colCount).Style.Font.FontFamily
End If
worksheetcol = (worksheetcol + 1)
colCount = (colCount + 1)
Loop
worksheetRow = (worksheetRow + 1)
rowCount = (rowCount + 1)
Loop
End Sub