简体   繁体   English

背景颜色 c# interpo

[英]Background color c# interpo

I am using Visual Studio C# and Microsoft.Office.Interop.Excel I can't get the good color from cell in excel sheet.我正在使用 Visual Studio C# 和Microsoft.Office.Interop.Excel我无法从 Excel 工作表中的单元格中获得好的颜色。 My question is: how I can get background color from cell.我的问题是:如何从单元格中获取背景颜色。

I tried this code :我试过这个代码:

var bachgroundColor = ws.Cells[7, 3].Interior.Color;

but the result is: System___ComObject但结果是: System___ComObject

The major issue with Interop is that DDE mechanism works fine some times. Interop 的主要问题是 DDE 机制有时可以正常工作。 But many time the data exchange will be affected and you loss data without error or warning alert or exception.There is nice lib called EPPlus, that makes your relationships with excel much easier and secure.但是很多时候数据交换会受到影响,您会丢失数据而不会出现错误或警告警报或异常。有一个名为 EPPlus 的好库,它使您与 excel 的关系更加轻松和安全。 You can find it on NuGet.你可以在 NuGet 上找到它。

Color CellColor = sheet.Cells[x, y].Interior.Color;

Change Cell Font and Background Color 更改单元格字体和背景颜色

First, you can get the int value of the Interior Color.首先,您可以获得内部颜色的 int 值。 And then convert it to type "Color".然后将其转换为“颜色”类型。

Here is a demo you can refer to.这是一个演示,您可以参考。

string strFileName = @"D:\test.xls";
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
Excel.Workbook workBook = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];

int colorNumber = System.Convert.ToInt32(((Range)worksheet.Cells[1, 2]).Interior.Color);
Color color = System.Drawing.ColorTranslator.FromOle(colorNumber);
//this.BackColor = color;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM