Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Visual-Studio VB.NET
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
Posted 4-Dec-12 1:57am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 393
1 Sergey Alexandrovich Kryukov 232
2 Richard Deeming 180
3 Snehasish_Nandy 171
4 ChauhanAjay 170
0 Sergey Alexandrovich Kryukov 6,367
1 OriginalGriff 5,588
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100