使用Interop在PowerPoint 2010中使用折线图 [英] Working with line charts in PowerPoint 2010 using Interop
问题描述
亲爱的专家,
我在PowerPoint 2010中进行了一些自动化。我希望使用c#.net自动化一些图表,引用Microsoft powerpoint interop assembly。
我在某种程度上取得了成功,但随着我开始访问图表的属性,它变得无法访问。
Dear Expert,
I was making some automation in PowerPoint 2010. Where I want to automate some charts using c# .net referring Microsoft powerpoint interop assemblies.
I got success to some extent but as I moved to access chart's properties, its just becoming unreachable.
public void createMyChart()
{
//Instantiate slide object
Microsoft.Office.Interop.PowerPoint.Slide objSlide = null;
//Access the first slide of presentation
objSlide = objPres.Slides[1];
//Select firs slide and set its layout
objSlide.Select();
objSlide.Layout = Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank;
//Add a default chart in slide
//pptNS.Shape objShape = objSlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xl3DColumn, 20F, 30F, 400F, 300F);
objSlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xlLine, 20F, 30F, 400F, 300F);
objSlide.Shapes[1].Chart.Select();
//Access the added chart
Microsoft.Office.Interop.PowerPoint.Chart objChart = null;
objChart = objSlide.Shapes[1].Chart;
//objChart.HasTitle = true;
//objChart.ChartTitle.Caption = "US Chart";
//Access the chart data
Microsoft.Office.Interop.PowerPoint.ChartData chartData = objChart.ChartData;
//Create instance to Excel workbook to work with chart data
Microsoft.Office.Interop.Excel.Workbook dataWorkbook = (Microsoft.Office.Interop.Excel.Workbook)chartData.Workbook;
//Accessing the data worksheet for chart
Microsoft.Office.Interop.Excel.Worksheet dataSheet = dataWorkbook.Worksheets[1];
//Applying the set range on chart data table
Microsoft.Office.Interop.Excel.ListObject tbl1 = dataSheet.ListObjects["Table1"];
xlNS.Range testRange = dataSheet.get_Range("A1", Type.Missing);
xlNS.Range tr1 = testRange.get_End(xlNS.XlDirection.xlDown);
xlNS.Range tr2 = tr1.get_End(xlNS.XlDirection.xlDown);
xlNS.Range dateRange = dataSheet.Cells.get_Range("A1", tr2);
dateRange.EntireColumn.Cells.NumberFormat = "m/dd/yyyy";
xlNS.Range rg1 = dataSheet.get_Range("B1", Type.Missing);
xlNS.Range rg2 = rg1.get_End(xlNS.XlDirection.xlDown);
xlNS.Range rg3 = rg2.get_End(xlNS.XlDirection.xlDown);
xlNS.Range finalRange = dataSheet.Cells.get_Range("A1", rg3);
tbl1.Resize(finalRange);
//Setting chart title
objChart.ChartTitle.Font.Italic = true;
objChart.ChartTitle.Text = "2007 Sales";
objChart.ChartTitle.Font.Size = 18;
objChart.SeriesCollection("M1% YoY Index");
//objChart.ChartTitle.Font.Color = "Dark Red";
objChart.ChartTitle.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
objChart.PlotArea.Select();
objChart.HasLegend = true;
objChart.Legend.Position = pptNS.XlLegendPosition.xlLegendPositionBottom;
//objChart.ChartTitle.Format.Line.ForeColor.RGB = Color.Black.ToArgb();
//Accessing Chart value Y-axis
Microsoft.Office.Interop.PowerPoint.Axis valaxis = objChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);
valaxis.HasTitle = true;
//Setting values axis units
valaxis.MajorUnit = 6.8;
valaxis.MinorUnitIsAuto = true;
valaxis.MinimumScaleIsAuto = true;
valaxis.MaximumScaleIsAuto = true;
valaxis.MajorGridlines.Format.Line.ForeColor.RGB = 0;
valaxis.Format.Line.ForeColor.RGB = 0;
valaxis.TickLabels.Font.Size = 12;
//Accessing Chart Value Y-Axis
int MajorUnit_X = 0;
MajorUnit_X = MajorUnitX_Axis.minDivValue(93);
pptNS.Axis xAxis = objChart.Axes(pptNS.XlAxisType.xlCategory, pptNS.XlAxisGroup.xlPrimary);
xAxis.MinorUnitIsAuto = true;
xAxis.MinimumScaleIsAuto = true;
xAxis.MaximumScaleIsAuto = true;
xAxis.MajorUnit = 12;
xAxis.Format.Line.ForeColor.RGB = 0;
xAxis.TickLabels.Font.Size = 12;
xAxis.TickLabels.Font.FontStyle = "Regular";
xAxis.TickLabelSpacingIsAuto = true;
xAxis.TickLabels.NumberFormat = "[$-409]mmm-yy;@";
int tempValue = xAxis.TickLabels.Alignment;
pptNS.XlTickLabelOrientation obj = pptNS.XlTickLabelOrientation.xlTickLabelOrientationDownward;
//objChart.RightAngleAxes = false;
// Save the presentation as a PPTX
objPres.SaveAs("C:\\VSTOSampleChart", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Microsoft.Office.Core.MsoTriState.msoTrue);
//Close Workbook and presentation
dataWorkbook.Application.Quit();
objPres.Application.Quit();
objPPT.Quit();
}
我使用的是VSTO。我想将折线图的线条颜色更改为暗红色,默认为蓝色。通常我们会去格式化数据系列菜单。我希望它使用c#interop做同样的事情。
谢谢和问候。
I am using VSTO. I want to change Line color of line chart to Dark Red which is by default Blue. Usually we go to Format Data series menu for the same. I want it to do the same using c# interop.
Thanks and regards.
推荐答案
-409] mmm-yy; @;
int tempValue = xAxis.TickLabels.Alignment;
pptNS.XlTickLabelOrientation obj = pptNS.XlTickLabelOrientation.xlTickLabelOrientationDownward;
// objChart.RightAngleAxes = false;
// 将演示文稿另存为PPTX
objPres.SaveAs( C:\\\ \\ _VSTOSampleChart,Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Microsoft.Office.Core.MsoTriState.msoTrue);
// 关闭工作簿和演示文稿
dataWorkbook.Application.Quit();
objPres.Application.Quit();
objPPT.Quit();
}
-409]mmm-yy;@"; int tempValue = xAxis.TickLabels.Alignment; pptNS.XlTickLabelOrientation obj = pptNS.XlTickLabelOrientation.xlTickLabelOrientationDownward; //objChart.RightAngleAxes = false; // Save the presentation as a PPTX objPres.SaveAs("C:\\VSTOSampleChart", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Microsoft.Office.Core.MsoTriState.msoTrue); //Close Workbook and presentation dataWorkbook.Application.Quit(); objPres.Application.Quit(); objPPT.Quit(); }
我使用的是VSTO。我想将折线图的线条颜色更改为暗红色,默认为蓝色。通常我们会去格式化数据系列菜单。我希望它使用c#interop来做同样的事情。
谢谢和问候。
I am using VSTO. I want to change Line color of line chart to Dark Red which is by default Blue. Usually we go to Format Data series menu for the same. I want it to do the same using c# interop.
Thanks and regards.
你可以这样做来改变线条颜色,
foreach(var tmp in objChart.SeriesCollection())
{
//检查要更改颜色的系列
if(tmp.Name ==Series1)
{
tmp.Border.Color = Color.Green;
}
}
You can do this to change line color,
foreach (var tmp in objChart.SeriesCollection())
{
//Check for the series that you want to change color
if (tmp.Name == "Series1")
{
tmp.Border.Color = Color.Green;
}
}
这篇关于使用Interop在PowerPoint 2010中使用折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!