从C#中的datagridview导出后,如何在Excel工作表中应用文本的颜色 [英] How can I apply the color for the text in excel sheet after exporting from datagridview in C#

查看:91
本文介绍了从C#中的datagridview导出后,如何在Excel工作表中应用文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,我试图循环gridview中的所有行和列,问题是我需要在导出后导出时在excel表格中应用颜色我需要看到特定单元格的蓝色我该怎么办才能帮到我呢









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;

 }





我的尝试:



i无法做到这一点,因为我是新来的



What I have tried:

i am unable to do this as i am new to this

推荐答案

你需要找到你需要具有不同文字颜色的单元格,并执行以下操作:

如何使用C#在Excel单元格中设置字体颜色 [ ^ ]
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#[^]


这篇关于从C#中的datagridview导出后,如何在Excel工作表中应用文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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