将数据从datagridview导出到Excel时,特定单元格中的颜色更改 [英] Change of Color in specific cells while exporting data from datagridview to Excel

查看:663
本文介绍了将数据从datagridview导出到Excel时,特定单元格中的颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的编码员,



在Google和我们的编码器的帮助下,我管理了使用列标题将datagridview中的数据导出到Excel。但现在我想在Excel的导出数据中突出显示具有不同颜色的特定单元格。请帮忙。导出数据事件的代码如下。



折叠|复制代码

Dim ExcelApp 作为 对象,ExcelBook 作为 对象
Dim ExcelSheet As 对象
Dim i As 整数
Dim j As 整数

创建excel对象
ExcelApp = CreateObject( Excel.Application
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets( 1

使用 Ex celSheet
对于 i = 1 Me .DataGridView1.ColumnCount
.Cells( 1 ,i)= DataGridView1.Columns (i - 1 )。HeaderText
下一步
对于 i = 1 .DataGridView1.RowCount
对于 j = 0 DataGridView1.Columns.Count - 1
.cells(i + 1 ,j + 1 )= DataGridView1.Rows(i - 1 )。 (j).Value
Next
下一步
结束

ExcelApp.Visible = True
'
ExcelSheet = 没什么
ExcelBook = 没什么
ExcelApp = Nothing

解决方案

我想你想要改变Excel中的单元格颜色。所以下面的代码可能对你有帮助。



objxlWS.Range(Cell(startRow,startCol),Cell(EndRow,End Column))。Cells.Interior.ColorIndex = fillColor

Dear Coders,

With the help of Google and Our Coders I Managed to export the data in datagridview to Excel with the Column Headings. But now the thing is I would like to Highlight a specific cell with different colors in the exported data of Excel. Please Help. the code of the export data event is below.

Collapse | Copy Code

Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer

'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)

With ExcelSheet
    For i = 1 To Me.DataGridView1.ColumnCount
        .Cells(1, i) = DataGridView1.Columns(i - 1).HeaderText
    Next
    For i = 1 To Me.DataGridView1.RowCount
        For j = 0 To DataGridView1.Columns.Count - 1
            .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
        Next
    Next
End With

ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing

解决方案

Hi, I guess you want to change the color of cell in Excel. So following code might help you.

objxlWS.Range(Cell(startRow, startCol), Cell(EndRow, End Column)).Cells.Interior.ColorIndex = fillColor


这篇关于将数据从datagridview导出到Excel时,特定单元格中的颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆