![](/img/trans.png)
[英]How can I copy specific cells(with Value,Color,Font) from one excel file to another excel file C#?
[英]How to render the Excel cells font color with C#?
我需要在Excel中呈現一種顏色,當單元格內容等於“ true”時,字體顏色為綠色!當字體內容等於“ false”時,顏色為紅色。這是我的代碼:
private void colorRender(Worksheet workSheet)
{
for(int i=0;i<workSheet.Rows.Count;i++)
{
for(int j=0;j<workSheet.Columns.Count;j++)
{
if(workSheet.Columns.Name=="校驗結果")
{
if(workSheet.Cells[i,j].ToString()=="false")
{
//if the cells was equal to false,set the font color red,others green.
//Microsoft.Office.Interop.Excel.Range range=Microsoft.Office.Interop.Excel.Worksheets.
}
}
}
}
}
當我在此處書寫時遇到問題:工作表不包含get_range函數。
您必須強制轉換要作為范圍的Cell對象,然后嘗試設置字體樣式。
if (workSheet.Columns.Name == "校驗結果")
{
Excel.Range range = workSheet.Cells[i, j] as Excel.Range;
if (range.Value2.ToString() == "false")
{
range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
else
{
range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.