用C#改变单元格的背景

发布于 2024-11-07 22:56:05 字数 293 浏览 6 评论 0原文

我正在使用 C# 开发一个程序来操作 Excel 文档,并且我正在使用

    Microsoft.Office.Interop.Excel._Worksheet worksheet;

当我向 ax,y 单元格插入内容时我使用:

worksheet.Cells[x, y] = "something";

现在我想知道是否可以更改 backColor来自 C# 的 Cells[x,y]。

谢谢。

I'm developing an program using C# to manipulate an Excel document, and I'm using

    Microsoft.Office.Interop.Excel._Worksheet worksheet;

When I insert something to a x,y cell I use :

worksheet.Cells[x, y] = "something";

Now I want to know if it's possible to change the backColor of the Cells[x,y] from C#.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相守太难 2024-11-14 22:56:05

尝试

worksheet.Cells[x, y].Interior.Color

您将无法直接使用.Net 中的颜色,它们需要翻译。

建议使用以下(显然是从银色改变的):

worksheet.Cells[x, y].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);

Try

worksheet.Cells[x, y].Interior.Color

You won't be able to use the Colors in .Net directly, they will require a translation.

It is recommended to use the following (obviously change from silver) :

worksheet.Cells[x, y].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);
如若梦似彩虹 2024-11-14 22:56:05

是的,您可以为单元格或整列或整行着色。

下面的代码将为您提供帮助。

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[2, 4]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);

else

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 3], xlWorkSheet.Cells[2, 3]).Interior.Color = Excel.XlRgbColor.rgbRed;

这里xlWorksheet是excel Worksheet对象。

get_Range 采用 2 个变量,一个是起始单元格,另一个是结束单元格。

因此,如果您指定两个值相同,则只有一个单元格被着色。

xlWorkSheet.cells[row, column] 用于指定单元格。

System.Drawing.ColorTranslator.ToOle(SystemDrawing.Color.Green)用于定义OLE格式的颜色。

Excel.XlRgbColor.rgbRed 是一种为单元格着色的 Excel 方法
此方法可以访问大量颜色,可以在此处找到 颜色列表

Yes you can color a cell or a entire column or entire row.

The below code will help you out.

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[2, 4]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);

else

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 3], xlWorkSheet.Cells[2, 3]).Interior.Color = Excel.XlRgbColor.rgbRed;

Here xlWorksheet is the object excel Worksheet object.

get_Range takes 2 variable one start cell and other is end cell.

so if you specify both the values same then only one cell is colored.

xlWorkSheet.cells[row, column] is used to specify a cell.

System.Drawing.ColorTranslator.ToOle(SystemDrawing.Color.Green) is used to define the color in OLE format.

Excel.XlRgbColor.rgbRed is a excel way of coloring the cells
This method gives access to large number of colors which can be found here list of colors

虫児飞 2024-11-14 22:56:05

您可以这样做:(

private static readonly int DEL_PERF_FIRST_DATA_ROW = 10;
private static readonly int SUN_ORDERS_COLUMN = 3;
private static readonly int TUE_ORDERS_COLUMN = 5;
private static readonly int THU_ORDERS_COLUMN = 7;
private static readonly int SAT_ORDERS_COLUMN = 9;
private static Color ALTERNATE_WEEKDAY_COLUMNS_COLOR = Color.LightGray;
. . .
int curRow = DEL_PERF_FIRST_DATA_ROW;

每次将一行写入工作表时,curRow 都会递增。)

// Pale Violetize (light gray, actually) Sun, Tues, Thurs, and Saturday columns
var sundayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SUN_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, SUN_ORDERS_COLUMN]];
sundayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var tuesdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, TUE_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, TUE_ORDERS_COLUMN]];
tuesdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var thursdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, THU_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, THU_ORDERS_COLUMN]];
thursdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var saturdayColumnRange =_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SAT_ORDERS_COLUMN], _xlSheetDelPerf.Cells[curRow - 1, SAT_ORDERS_COLUMN]];
saturdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

You can do it like this:

private static readonly int DEL_PERF_FIRST_DATA_ROW = 10;
private static readonly int SUN_ORDERS_COLUMN = 3;
private static readonly int TUE_ORDERS_COLUMN = 5;
private static readonly int THU_ORDERS_COLUMN = 7;
private static readonly int SAT_ORDERS_COLUMN = 9;
private static Color ALTERNATE_WEEKDAY_COLUMNS_COLOR = Color.LightGray;
. . .
int curRow = DEL_PERF_FIRST_DATA_ROW;

(curRow gets incremented each time a row is written to the sheet.)

// Pale Violetize (light gray, actually) Sun, Tues, Thurs, and Saturday columns
var sundayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SUN_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, SUN_ORDERS_COLUMN]];
sundayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var tuesdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, TUE_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, TUE_ORDERS_COLUMN]];
tuesdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var thursdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, THU_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, THU_ORDERS_COLUMN]];
thursdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var saturdayColumnRange =_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SAT_ORDERS_COLUMN], _xlSheetDelPerf.Cells[curRow - 1, SAT_ORDERS_COLUMN]];
saturdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;
浅唱ヾ落雨殇 2024-11-14 22:56:05

对于想要将十六进制颜色设置为背景的任何人都可以使用:

worksheet.get_Range("A1", "F1").Interior.Color = ColorTranslator.FromHtml("#52b69a");

For anyone who want to set Hex color to the background can use:

worksheet.get_Range("A1", "F1").Interior.Color = ColorTranslator.FromHtml("#52b69a");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文