我最近偶然发现了这种奇怪的行为,同时试图从excel中获取样式信息,尤其是边框颜色。
使用Microsoft.Office.Interop.Excel 15.0.0.0 和Excel 2010
我将9个相邻单元格中的边框颜色(xlDiagonalUp)和样式分别设置为红色和连续线。 当通过查询范围要求返回信息时,颜色信息会模糊消失
这是重现该问题的单元测试
[TestCase]
public void TestExcelStuff()
{
var excel = new Application();
var wb = (_Workbook) (excel.Workbooks.Add(Missing.Value));
var sht = wb.ActiveSheet;
// Setup
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < 3; k++)
{
sht.Cells[2 + i, 2 + k].Borders[XlBordersIndex.xlDiagonalUp].Color = XlRgbColor.rgbRed;
sht.Cells[2 + i, 2 + k].Borders[XlBordersIndex.xlDiagonalUp].LineStyle = XlLineStyle.xlContinuous;
}
}
// test
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < 3; k++)
{
Console.WriteLine($"Setting Cell {2+i},{2+k}");
var lineColor = ColorTranslator.FromOle((int)sht.Cells[2 + i, 2 + k].Borders[XlBordersIndex.xlDiagonalUp].Color);
// Works: the Borderline is red and continuous
Assert.AreEqual(Color.Red, lineColor);
Assert.AreEqual((int)XlLineStyle.xlContinuous, sht.Cells[2 + i, 2 + k].Borders[XlBordersIndex.xlDiagonalUp].LineStyle);
}
}
var rng = sht.Range[sht.Cells[2, 2], sht.Cells[4, 4]];
// works: continous line
Assert.AreEqual((int)XlLineStyle.xlContinuous, rng.Borders[XlBordersIndex.xlDiagonalUp].LineStyle);
// for your eyes
excel.Visible = true;
var lineColor1 = ColorTranslator.FromOle((int) rng.Borders[XlBordersIndex.xlDiagonalUp].Color);
// does not Work: the Borderline is black! but why?
Assert.AreEqual(Color.Red, lineColor1);
}
错误或功能?