简体   繁体   中英

Setting point color in excel C# is giving Invalid Parameter exception

Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = activeSheet.Range["E1", "E3"];
series1.Values = activeSheet.Range["F1", "F3"];

series1.Points(0).Format.Fill.ForeColor.RGB = Color.Green.ToArgb();

Gives a invalid parameter exception.

Here's the details:

System.Runtime.InteropServices.COMException was unhandled by user code
  HResult=-2146827284
  Message=Invalid Parameter
  Source=S
  ErrorCode=-2146827284
  StackTrace:
       at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
       at Microsoft.Office.Interop.Excel.Series.Points(Object Index)
       at OMSS.BobStats.DrawFractionChart(Worksheet activeSheet, ChartObjects xlCharts, Range xRange, Range yRange) in c:\Users\t-jasonj\Documents\Visual Studio 2012\Projects\BobStats\ExcelAddIn1\BobStats.cs:line 104
       at OMSS.BobStats.DrawCharts(Worksheet activeSheet, Range range, Int32 length) in c:\Users\t-jasonj\Documents\Visual Studio 2012\Projects\BobStats\ExcelAddIn1\BobStats.cs:line 66
       at OMSS.BobStats.ThisAddIn_Startup(Object sender, EventArgs e) in c:\Users\t-jasonj\Documents\Visual Studio 2012\Projects\BobStats\ExcelAddIn1\BobStats.cs:line 52
       at Microsoft.Office.Tools.AddInImpl.OnStartup()
       at Microsoft.Office.Tools.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup()
       at Microsoft.Office.Tools.AddInBase.OnStartup()
       at OMSS.BobStats.FinishInitialization() in c:\Users\t-jasonj\Documents\Visual Studio 2012\Projects\BobStats\ExcelAddIn1\BobStats.Designer.cs:line 59
       at Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools.EntryPoint.FinishInitialization()
       at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)
       at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()
  InnerException: 

you either use

series1.Points(1).Format.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(Color.Green);

or

series1.Points(1).Format.Fill.ForeColor.RGB = Excel.XlRgbColor.rgbGreen

Edit: Note that Points are 1 indexed not 0 indexed.

It doesn't like the A in ToArgb(). Use the & operator to get rid of it:

series1.Points(1).Format.Fill.ForeColor.RGB = Color.Green.ToArgb() & 0xffffff;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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