如何在DataTable c#中设置DataRow的背景色
How to set background color of DataRow in DataTable c#
我正在将我的 DataTable 导出到 Excel。
所以在导出之前,我添加了一个新行,我想为这一行设置一些背景颜色。
这是我的代码...
DataRow newRow = datatable3.NewRow();
for (int i = 0; i < datatable3.Columns.Count; i++)
{
newRow[i] = "Hello";
}
//newRow.BackGroundColor = "Red" - Something like this.
这里我将我的 DataTable 导出到 Excel。
using (XLWorkbook wb = new XLWorkbook())
{
foreach (DataTable dt in ds.Tables)
{
//Add DataTable as Worksheet.
wb.Worksheets.Add(dt, dt.TableName.ToString());
}
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
return File(MyMemoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ext);
}
}
显示是这样的
如何更改行背景颜色?
您似乎在使用 ClosedXMS dll。 check their documentation on using colors
您可以使用在 Excel.Range 上找到的内饰 属性。
// The following just shows how the variables are created (based on creating a new Excel Spreadsheet)
var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Add(Missing.Value);
var xlWorksheet = xlWorkbook.Worksheets[1];
// Now the actual code needed
var xlRange = xlWorksheet.UsedRange;
// This select the entire top row, but you can select your own range based on your data
var titleRange = xlRange.Range["A1", string.Concat(((char)(xlRange.Columns.Count + 64)), 1)];
// The following line sets the fill colour
titleRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);
@Gopal
只是针对@Conor 的回答有点具体!。你可以试试这个-
//对于多个列表中的特定Datatable
var ws = wb.Worksheets.Add(dt, dt.TableName.ToString());
for (int j = 1; j <= ds.Tables[3].Columns.Count; j++) //This is for fourth datatable/sheet
{
ws.Cell(2, j).Style.Fill.BackgroundColor = XLColor.FromArgb(255, 255, 0); //All columns of second row
}
XlColor.FromArgb(//RGB颜色代码);此静态方法使您可以自由指定 RGB 颜色代码,您可以通过正在使用的 excel 模板轻松获得。
我正在将我的 DataTable 导出到 Excel。 所以在导出之前,我添加了一个新行,我想为这一行设置一些背景颜色。 这是我的代码...
DataRow newRow = datatable3.NewRow();
for (int i = 0; i < datatable3.Columns.Count; i++)
{
newRow[i] = "Hello";
}
//newRow.BackGroundColor = "Red" - Something like this.
这里我将我的 DataTable 导出到 Excel。
using (XLWorkbook wb = new XLWorkbook())
{
foreach (DataTable dt in ds.Tables)
{
//Add DataTable as Worksheet.
wb.Worksheets.Add(dt, dt.TableName.ToString());
}
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
return File(MyMemoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ext);
}
}
显示是这样的
如何更改行背景颜色?
您似乎在使用 ClosedXMS dll。 check their documentation on using colors
您可以使用在 Excel.Range 上找到的内饰 属性。
// The following just shows how the variables are created (based on creating a new Excel Spreadsheet)
var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Add(Missing.Value);
var xlWorksheet = xlWorkbook.Worksheets[1];
// Now the actual code needed
var xlRange = xlWorksheet.UsedRange;
// This select the entire top row, but you can select your own range based on your data
var titleRange = xlRange.Range["A1", string.Concat(((char)(xlRange.Columns.Count + 64)), 1)];
// The following line sets the fill colour
titleRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);
@Gopal
只是针对@Conor 的回答有点具体!。你可以试试这个-
//对于多个列表中的特定Datatable
var ws = wb.Worksheets.Add(dt, dt.TableName.ToString());
for (int j = 1; j <= ds.Tables[3].Columns.Count; j++) //This is for fourth datatable/sheet
{
ws.Cell(2, j).Style.Fill.BackgroundColor = XLColor.FromArgb(255, 255, 0); //All columns of second row
}
XlColor.FromArgb(//RGB颜色代码);此静态方法使您可以自由指定 RGB 颜色代码,您可以通过正在使用的 excel 模板轻松获得。