0
私はdataTableを持ち、Microsoft.Office.Interopを使用してExcelファイルにデータを挿入します。私はdataTableを繰り返し、各行と特定の列をチェックしています。そして、特定の列の値に基づいて、その行を色付けしたいと思います。私がコメントしたセクションの問題に直面している。コードを使用すると、最後の色を取り、他の色を上書きします。Microsoft.Office.Interopを使用するC#でExcelシートの行と列範囲を条件付きで塗りつぶし
"Failed"と "Running" "Interrupted"と "Succeeded"という条件があり、dataTableの特定の列と比較する必要があり、それに基づいて背景を色付けする必要があるとしますデータの
Sample code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using log4net;
using System.Data;
namespace ReadAppendExcel
{
public static class DataTable_Extensions
{
/// <summary>
/// Export DataTable to Excel file
/// </summary>
/// <param name="DataTable">Source DataTable</param>
/// <param name="ExcelFilePath">Path to result file name</param>
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void ExportToExcel(this System.Data.DataTable DataTable, string ExcelFilePath = null)
{
log4net.Config.BasicConfigurator.Configure();
ILog log = log4net.LogManager.GetLogger(typeof(Program));
try
{
int ColumnsCount;
log.Info("In the ExportToExcel function");
if (DataTable == null || (ColumnsCount = DataTable.Columns.Count) == 0)
throw new Exception("ExportToExcel: Null or empty input table!\n");
// load excel, and create a new workbook
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbooks.Add();
// single worksheet
Microsoft.Office.Interop.Excel._Worksheet Worksheet = Excel.ActiveSheet;
int RowsCount = DataTable.Rows.Count;
object[] Header = new object[ColumnsCount];
object[] RowsCol = new object[RowsCount];
// column headings
for (int i = 0; i < ColumnsCount; i++)
Header[i] = DataTable.Columns[i].ColumnName;
Microsoft.Office.Interop.Excel.Range HeaderRange = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, ColumnsCount]));
HeaderRange.Value = Header;
HeaderRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange);
HeaderRange.Font.Bold = true;
// DataCells
object[,] Cells = new object[RowsCount, ColumnsCount];
Excel.Range range = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[2, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[RowsCount + 1, ColumnsCount]));
range.Value = RowsCol;
for (int j = 0; j < RowsCount; j++)
{
for (int i = 0; i < ColumnsCount; i++)
Cells[j, i] = DataTable.Rows[j][i];
if (DataTable.Rows[j][1].ToString() == "Failed") // CHECKING CONDITION WITH THE DATATABLE
{
// USE TO COLOR THE ROW AND THE COLUMN RANGE
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
else if (DataTable.Rows[j][1].ToString() == "Running")
{
// USE TO COLOR THE ROW AND THE COLUMN RANGE
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
}
else if (DataTable.Rows[j][1].ToString() == "Interrupted")
{
// USE TO COLOR THE ROW AND THE COLUMN RANGE
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
}
else if (DataTable.Rows[j][1].ToString() == "Succeeded")
{
range.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
}
}
Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[2, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[RowsCount + 1, ColumnsCount])).Value = Cells;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Color = Color.Black.ToArgb();
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Color = Color.Black.ToArgb();
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Color = Color.Black.ToArgb();
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Color = Color.Black.ToArgb();
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Color = Color.Black.ToArgb();
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Color = Color.Black.ToArgb();
// check fielpath
if (ExcelFilePath != null && ExcelFilePath != "")
{
try
{
Worksheet.SaveAs(ExcelFilePath);
Excel.Quit();
log.Info("excel successfully created");
}
catch (Exception ex)
{
log.Info("ExportToExcel: Excel file could not be saved! Check filepath.\n" + ex.Message.ToString());
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message);
}
}
else // no filepath is given
{
Excel.Visible = true;
}
}
catch (Exception ex)
{
log.Info("ExportToExcel: \n" + ex.Message.ToString());
throw new Exception("ExportToExcel: \n" + ex.Message);
}
}
}
}
すべきですか?細胞は着色されていませんか?次のようにValueを追加してみてください: 'DataTable.Rows [j] [1] .Value.ToString()' – joemartin94
最後の条件が最後の条件と同じように上書きされているので、それは成功です。 –
すべてのifは最後のものと同じではありません。最後にこれを入れてください: 'range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); ' – joemartin94