2

我在下面编写了一段代码,以使用 c# 代码更改 ppt 表格单元格的背景颜色,但没有任何反应:

//creating powerpoint aaplication
PowerPoint.Application pptApp = new PowerPoint.Application();
pptApp.Visible = Office.MsoTriState.msoTrue;
var pptPresent = pptApp.Presentations;
var fileOpen = pptPresent.Open(@file, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, Office.MsoTriState.msoTrue);
// getting first slide
PowerPoint.Slide objSlide = fileOpen.Slides[1];
PowerPoint.Shapes item = objSlide.Shapes;
// getting first shape
var shape1 = item[1];
// check if shape is table
if (shape1.HasTable == Office.MsoTriState.msoTrue)
{
    // change the table cell to red color
    shape1.Table.Cell(2, 5).Shape.Fill.BackColor.RGB = System.Drawing.Color.Red.ToArgb();
    // make it visible
    shape1.Fill.Visible = Office.MsoTriState.msoTrue;
}
// saving the ppt
fileOpen.SaveAs(openFolder + subStr + ".pptx",PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Office.MsoTriState.msoTrue);
// close the ppt
fileOpen.Close();

上面的代码没有按预期工作,有人可以帮助我吗?

4

1 回答 1

2

要更改单元格颜色,需要使用 ForeColor 而不是 BackColor。绘制颜色可能无法按预期工作。要改变这一点,请使用 ColorTranslator。

shape1.Table.Cell(2, 5).Shape.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
于 2013-01-27T20:30:28.007 回答