简体   繁体   English

C#:如何从以编程方式在Power Point中创建的表中删除第一行颜色

[英]C#: How to remove first row color from table created in power point programmatically

i was trying to create table with two columns and many rows programmatically in power point slide and at last i could do it but when i open power point slide then i notice my table first row has blue color but in my code i nowhere mention any color for table. 我试图在Powerpoint幻灯片中以编程方式创建具有两列和许多行的表,最后我可以做到这一点,但是当我打开Power Point幻灯片时,我注意到我的表的第一行是蓝色,但是在我的代码中我没有提到任何颜色为表。

so please have a look at my code and tell me how could i remove blue color from first row? 所以请看一下我的代码,告诉我如何从第一行中删除蓝色?

here is the full code 这是完整的代码

private void Form1_Load(object sender, EventArgs e)
{
    pptNS.ApplicationClass powerpointApplication = null;
    pptNS.Presentation pptPresentation = null;
    pptNS.Slide pptSlide = null;
    pptNS.ShapeRange shapeRange = null;
    pptNS.Shape oShape = null;

    xlNS.ApplicationClass excelApplication = null;
    xlNS.Workbook excelWorkBook = null;
    xlNS.Worksheet targetSheet = null;
    xlNS.ChartObjects chartObjects = null;
    xlNS.ChartObject existingChartObject = null;
    xlNS.Range destRange = null;

    string paramPresentationPath = @"D:\test\Chart Slide.pptx";
    string paramWorkbookPath = @"D:\test\data.xlsx";
    object paramMissing = Type.Missing;


    try
    {
        // Create an instance of PowerPoint.
        powerpointApplication = new pptNS.ApplicationClass();

        // Create an instance Excel.          
        excelApplication = new xlNS.ApplicationClass();

        // Open the Excel workbook containing the worksheet with the chart
        // data.
        excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing);

        // Get the worksheet that contains the chart.
        targetSheet =
            (xlNS.Worksheet)(excelWorkBook.Worksheets["Spain"]);

        // Get the ChartObjects collection for the sheet.
        chartObjects =
            (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));



        // Create a PowerPoint presentation.
        pptPresentation = powerpointApplication.Presentations.Add(
                            Microsoft.Office.Core.MsoTriState.msoTrue);

        // Add a blank slide to the presentation.
        pptSlide =
            pptPresentation.Slides.Add(1, pptNS.PpSlideLayout.ppLayoutBlank);

        // capture range
        //var writeRange = targetSheet.Range["A1:B15"];
        destRange = targetSheet.get_Range("A1:B15");

        System.Array myvalues = (System.Array)destRange.Cells.Value;
        List<Tuple<string, string>> cellData = GetData(myvalues);

        int iRows = cellData.Count;
        int iColumns = 2;
        int row = 1;

        oShape = pptSlide.Shapes.AddTable(iRows, iColumns, 500, 110, 160, 120);

        foreach (Tuple<string, string> item in cellData)
        {
            string strdate = item.Item1;
            string strValue = item.Item2;

            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Text = strdate;
            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Name = "Verdana";
            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Size = 8;
            //oShape.Table.Cell(row, 1).Shape.Fill.ForeColor.RGB = System.Drawing.Color.FromArgb(255, 255, 255).ToArgb(); 
            //oShape.Table.Cell(row, 1).Shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Text = (strValue.StartsWith("0") ?  "0%" : (strValue + "0%"));
            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Name = "Verdana";
            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Size = 8;


            row++;
        }

        oShape.Top = 10;
        oShape.Left =10;

        //copy range
        //destRange.Copy();

        // Paste the chart into the PowerPoint presentation.
        //shapeRange = pptSlide.Shapes.Paste();

        //var table = pptSlide.Shapes.AddTable();
        // Position the chart on the slide.
        //shapeRange.Left = 60;
        //shapeRange.Top = 100;

        // Get or capture the chart to copy.
        //existingChartObject = (xlNS.ChartObject)(chartObjects.Item(1));


        // Copy the chart from the Excel worksheet to the clipboard.
        //existingChartObject.Copy();

        // Paste the chart into the PowerPoint presentation.
        //shapeRange = pptSlide.Shapes.Paste();
        //Position the chart on the slide.
        //shapeRange.Left = 90;
        //shapeRange.Top = 100;

        // Save the presentation.
        pptPresentation.SaveAs(paramPresentationPath,
                        pptNS.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
                        Microsoft.Office.Core.MsoTriState.msoTrue);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        // Release the PowerPoint slide object.
        shapeRange = null;
        pptSlide = null;

        // Close and release the Presentation object.
        if (pptPresentation != null)
        {
            pptPresentation.Close();
            pptPresentation = null;
        }

        // Quit PowerPoint and release the ApplicationClass object.
        if (powerpointApplication != null)
        {
            powerpointApplication.Quit();
            powerpointApplication = null;
        }

        // Release the Excel objects.
        targetSheet = null;
        chartObjects = null;
        existingChartObject = null;

        // Close and release the Excel Workbook object.
        if (excelWorkBook != null)
        {
            excelWorkBook.Close(false, paramMissing, paramMissing);
            excelWorkBook = null;
        }

        // Quit Excel and release the ApplicationClass object.
        if (excelApplication != null)
        {
            excelApplication.Quit();
            excelApplication = null;
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

    }
}

Try this to give background color to cells. 尝试使用此项为单元格赋予背景色。

 xlWorkSheet.Cells[iRowCounter, iColCounter].Interior.Color =
 System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);  

This one is for excel table. 这是一张用于excel表的文件。

I think, you should adding this code. 我认为,您应该添加此代码。

oShape.Table.Cell(row, 1).Shape.Fill.Visible = MsoTriState.msoTrue;
oShape.Table.Cell(row, 1).Shape.Fill.ForeColor.RGB = System.Drawing.Color.FromArgb(255, 255, 255).ToArgb();

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

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