2015-05-22 34 views
0

我創建了一個基於DataSet的Excel文件,我想添加一個帶紅綠燈的列。Excel添加交通信號燈

紅綠燈:

int trafficCollor = 1; 
if (trafficCollor == 1) 
{ 
    DataRow NR = DT.NewRow(); 
    NR[0] = ""; //add string with the code for the Excel traffic light yellow 
} 

if (trafficCollor < 1) 
{ 
    DataRow NR = DT.NewRow(); 
    NR[0] = ""; //add string with the code for the Excel traffic light green 
} 

if (trafficCollor > 1) 
{ 
    DataRow NR = DT.NewRow(); 
    NR[0] = ""; //add string with the code for the Excel traffic light red 
} 

Traffic light Excel

如何在Excel中創建了:

//Create an Excel application instance 
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); 
Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Application.Workbooks.Add(); 
Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
excelApp.Visible = false; 
worksheet = excelWorkBook.ActiveSheet; 

//set headers 
for (int i = 1; i < DGV.Columns.Count + 1; i++) 
{ 
    worksheet.Cells[1, i] = DGV.Columns[i - 1].HeaderText; 
} 

createList(worksheet, DGV); 

//Create excel with the choosen name 
Worksheet sheet1 = excelWorkBook.Worksheets[1]; 
worksheet.Name = fileName; 

如果您有任何建議,存檔這樣的事情,讓我知道!

+0

你是如何創建Excel文件?您無法將條件格式添加到「DataTable」中。 –

+0

@Charles Mager請參閱編輯.. –

+0

我沒有看到您在代碼中使用條件格式進行任何操作? –

回答

1

以下是我快速編寫的示例代碼,演示如何在條件格式中使用交通燈。請改變它以滿足您的需求。

using System; 
using System.Windows.Forms; 
using Excel = Microsoft.Office.Interop.Excel; 

Namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     Public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Excel.Application xlexcel; 
      Excel.Workbook xlWorkBook; 
      Excel.Worksheet xlWorkSheet; 

      object misValue = System.Reflection.Missing.Value; 
      xlexcel = new Excel.Application(); 
      xlWorkBook = xlexcel.Workbooks.Add(misValue); 
      xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
      xlexcel.Visible = true; 

      //add data 
      xlWorkSheet.Cells[1, 1] = 1; 
      xlWorkSheet.Cells[2, 1] = 2; 
      xlWorkSheet.Cells[3, 1] = 3; 
      xlWorkSheet.Cells[4, 1] = 4; 

      Excel.Range MyRange = xlWorkSheet.get_Range("A1", "A4"); 

      MyRange.FormatConditions.AddIconSetCondition(); 
      MyRange.FormatConditions.Item (MyRange.FormatConditions.Count).SetFirstPriority(); 
      MyRange.FormatConditions.Item(1).ReverseOrder = false; 
      MyRange.FormatConditions.Item(1).ShowIconOnly = false; 
      MyRange.FormatConditions.Item(1).IconSet = xlWorkBook.IconSets[Excel.XlIconSet.xl3TrafficLights2]; 

      MyRange.FormatConditions.Item(1).IconCriteria(2).Type = Excel.XlConditionValueTypes.xlConditionValuePercent; 
      MyRange.FormatConditions.Item(1).IconCriteria(2).Value = 33; 
      MyRange.FormatConditions.Item(1).IconCriteria(2).Operator = 7; 

      MyRange.FormatConditions.Item(1).IconCriteria(3).Type = Excel.XlConditionValueTypes.xlConditionValuePercent; 
      MyRange.FormatConditions.Item(1).IconCriteria(3).Value = 67; 
      MyRange.FormatConditions.Item(1).IconCriteria(3).Operator = 7; 

      // Rest of the code 
     } 
    } 
} 

截圖

enter image description here