Я создаю Datatable в C # и экспортирую его в таблицу Excel ... Как сделать цвет в ячейках столбца компании с помощью циклов, ЕСЛИ название компании - wipro, то желтый ELSE red.HELP!
Iя пытаюсь этот код в Visual Studio 2015, но это не правильно,
DataTable dt = tablelist.Columns[i];
if (DataColumnCollection.tablelist.Column.Company = "Wipro")
{
Console.WriteLine(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow));
}
else
{
Console.WriteLine(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red));
}
namespace Datatable2Excel
{
public partial class Form1 : Form
{
DataTable ctr_raw_table = new DataTable();
public Form1()
{
InitializeComponent();
ctr_raw_table.Columns.Add("Name", typeof(String));
ctr_raw_table.Columns.Add("Company", typeof(String));
ctr_raw_table.Columns.Add("Job", typeof(String));
ctr_raw_table.Columns.Add("Level", typeof(String));
ctr_raw_table.Columns.Add("ID", typeof(String));
ctr_raw_table.Columns.Add("Address", typeof(String));
string[] row = new string[] { "John", "Wipro", "Engineer", "0203794", "India", "Mumbai" };
ctr_raw_table.Rows.Add(row);
row = new string[] { "David", "TCS", "Engineer", "02065394", "India", "Thane" };
ctr_raw_table.Rows.Add(row);
row = new string[] { "Albert", "Infosys", "Engineer", "0206394", "India", "Mumbai" };
ctr_raw_table.Rows.Add(row);
row = new string[] { "Daniel", "IBM", "Engineer", "02036594", "India", "Mumbra" };
ctr_raw_table.Rows.Add(row);
row = new string[] { "James", "Accenture", "Engineer", "02560394", "India", "Bhiwandi" };
ctr_raw_table.Rows.Add(row);
row = new string[] { "Jacob", "L&T", "Engineer", "02036594", "India", "kalyan" };
ctr_raw_table.Rows.Add(row);
}
private void button1_Click(object sender, EventArgs e)
{
Export_Ctr_Excel(ctr_raw_table, "Output");
}
private void Export_Ctr_Excel(DataTable tablelist, string excelFilename)
{
Microsoft.Office.Interop.Excel.Application objexcelapp = new Microsoft.Office.Interop.Excel.Application();
objexcelapp.Application.Workbooks.Add(Type.Missing);
objexcelapp.Columns.AutoFit();
for (int i = 1; i < tablelist.Columns.Count + 1; i++)
{
Microsoft.Office.Interop.Excel.Range xlRange = (Microsoft.Office.Interop.Excel.Range)objexcelapp.Cells[1, i];
xlRange.Font.Bold = -1;
int Row = 1;
int Column = 1;
Microsoft.Office.Interop.Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)objexcelapp.Cells[Row, Column];
rng.EntireColumn.Font.Bold = true;
rng.EntireColumn.Font.Italic = true;
rng.EntireColumn.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.HotPink);
rng.EntireColumn.BorderAround();
xlRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
xlRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
objexcelapp.Cells[1, i] = tablelist.Columns[i - 1].ColumnName;
DataTable dt = tablelist.Columns[i];
if (DataColumnCollection.tablelist.Column.Company = "Wipro")
{
Console.WriteLine(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow));
}
else
{
Console.WriteLine(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red));
}
}
for (int i = 0; i < tablelist.Rows.Count; i++)
{
for (int j = 0; j < tablelist.Columns.Count; j++)
{
if (tablelist.Rows[i][j] != null)
{
Microsoft.Office.Interop.Excel.Range xlRange = (Microsoft.Office.Interop.Excel.Range)objexcelapp.Cells[i + 2, j + 1];
xlRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
xlRange.Borders.Weight = 1d;
xlRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
objexcelapp.Cells[i + 2, j + 1] = tablelist.Rows[i][j].ToString();
}
}
}
objexcelapp.Columns.AutoFit();
System.Windows.Forms.Application.DoEvents();
if (Directory.Exists("C:\\CTR_Data\\"))
{
objexcelapp.ActiveWorkbook.SaveCopyAs("C:\\CTR_Data\\" + excelFilename + ".xlsx");
}
else
{
Directory.CreateDirectory("C:\\CTR_Data\\");
objexcelapp.ActiveWorkbook.SaveCopyAs("C:\\CTR_Data\\" + excelFilename + ".xlsx");
}
objexcelapp.ActiveWorkbook.Saved = true;
System.Windows.Forms.Application.DoEvents();
foreach (Process proc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
proc.Kill();
}
}
private object Worksheets(string v)
{
throw new NotImplementedException();
}
}
}
Я ожидаю, чтобы изменить цвет ячейки с помощью циклов в C # не на листе Excel ... Я хочу, когда я буду экспортировать свою таблицу данныхв Excel это уже произойдет