Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my code where i am trying to loop all the rows and columns in gridview the problem is i need to apply color for the text in excel sheet after export that is after exporting i need to see the blue color for particular cell how can i do this can anyone help me out




public void ExportToExcel(DataGridView gridviewID, string excelFilename)
 {
     Microsoft.Office.Interop.Excel.Application objexcelapp = new Microsoft.Office.Interop.Excel.Application();
     objexcelapp.Application.Workbooks.Add(Type.Missing);
     objexcelapp.Columns.ColumnWidth = 25;

     for (int i = 1; i < gridviewID.Columns.Count + 1; i++)
     {
         objexcelapp.Cells[1, i] = gridviewID.Columns[i - 1].HeaderText;
     }
     /*For storing Each row and column value to excel sheet*/
     for (int i = 0; i < gridviewID.Rows.Count; i++)
     {
         for (int j = 0; j < gridviewID.Columns.Count; j++)
         {
             if (gridviewID.Rows[i].Cells[j].Value != null)
             {
                 objexcelapp.Cells[i + 2, j + 1] = gridviewID.Rows[i].Cells[j].Value.ToString();



             }
         }

         progressBar1.PerformStep();
         label3.Text = i + "/" + Convert.ToInt32(gridviewID.Rows.Count);
     }

     objexcelapp.ActiveWorkbook.SaveCopyAs("\\Desktop\\" + FileName + ".xlsx");
     MessageBox.Show("Your excel file exported successfully at  FileName + ".xlsx");
     objexcelapp.ActiveWorkbook.Saved = true;
     progressBar1.Visible = false;
     label3.Visible = false;

 }


What I have tried:

i am unable to do this as i am new to this
Posted
Updated 27-Nov-17 21:58pm
v2

1 solution

You need to find the cell you need to have a different text color and do something like:
How to Set font color in Excel Cell Using C#[^]
 
Share this answer
 
Comments
kav@94 28-Nov-17 4:02am    
i had already tried them they are not working
Kenneth Haugland 28-Nov-17 4:32am    
Should just be that:
objexcelapp.Cells[i + 2, j + 1].Style.Font.Color = Color.FromArgb(255, 125, 125);
That what I did anyway
kav@94 29-Nov-17 0:19am    
Hi i am getting below errors
'Color' is an ambiguous reference between 'System.Drawing.Color' and 'DocumentFormat.OpenXml.Spreadsheet.Color'
'object' does not contain a definition for 'Style' and no extension method 'Style' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Kenneth Haugland 29-Nov-17 1:52am    
Add System.Drawing as a referance and type:
oSheet.get_Range("A2").Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

That worked for me.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900