public XlChartObject Add(XlRowCol rowCol)
        {
            object[] paramArray = new object[4];
            paramArray[0] = rowCol;
            paramArray[1] = Missing.Value;
            paramArray[2] = Missing.Value;
            paramArray[3] = Missing.Value;

            object returnValue = InstanceType.InvokeMember("Add", BindingFlags.InvokeMethod, null, ComReference, paramArray, XlLateBindingApiSettings.XlThreadCulture);

            if (null == returnValue)
            {
                return(null);
            }
            XlChartObject newClass = new XlChartObject(this, returnValue);

            ListChildReferences.Add(newClass);
            return(newClass);
        }
Example #2
0
        public static void CreateColClusterBarGraph(
            int left, int top, int width, int height,
            _Worksheet oSheet, Range dataRange,
            string strXAxis, string strChartTitle, double titleFontSize,
            string strXTitle, double xFontSize, List <string> listLegend,
            string strYTitle, double yFontSize, XlRowCol rowCol)
        {
            ChartObjects chartObjs = (ChartObjects)oSheet.ChartObjects(Type.Missing);
            ChartObject  chartObj  = chartObjs.Add(left + 100, top, 425, 315);
            Chart        xlChart   = chartObj.Chart;

            xlChart.SetSourceData(dataRange, Type.Missing);
            xlChart.ChartType = XlChartType.xlColumnClustered;

            xlChart.PlotBy = rowCol;

            // Xaxis data labels
            Series oS = (Series)xlChart.SeriesCollection(1);

            string strVersion = m_oXL.Application.Version;

            if (strVersion == "11.0")  // Office 2003 Excel
            {
                string strStartRow, strEndRow, strStartCol, strEndCol;
                int    nPos = -1, nEnd = -1, nStartCol, nEndCol, nStartRow, nEndRow;

                // Sample incoming: "='Total Lane-Miles Per
                // Condition'!$B$5:$F$5"; or "='Total Lane-Miles Per
                // Condition'!$B$5:$B$10";
                nPos = strXAxis.IndexOf('$');
                if (nPos > -1)
                {
                    nEnd        = strXAxis.IndexOf('$', nPos + 1);
                    strStartCol = strXAxis.Substring(nPos + 1, nEnd - nPos - 1);

                    nPos        = strXAxis.IndexOf(':');
                    strStartRow = strXAxis.Substring(nEnd + 1, nPos - nEnd - 1);

                    nPos      = strXAxis.IndexOf('$', nPos);
                    nEnd      = strXAxis.IndexOf('$', nPos + 1);
                    strEndCol = strXAxis.Substring(nPos + 1, nEnd - nPos - 1);
                    strEndRow = strXAxis.Substring(nEnd + 1);

                    nStartRow = int.Parse(strStartRow);
                    nEndRow   = int.Parse(strEndRow);

                    nStartCol = GetColumnNumber(strStartCol + strStartRow);
                    nEndCol   = GetColumnNumber(strEndCol + strStartRow);

                    nPos      = strXAxis.IndexOf('!');
                    strXAxis  = strXAxis.Substring(0, nPos + 1);
                    strXAxis += "R" + nStartRow.ToString() + "C" + nStartCol.ToString() + ":R" + nEndRow.ToString() +
                                "C" + nEndCol.ToString();
                }
            }
            oS.XValues = strXAxis;

            // Add title:
            if (strChartTitle != "")
            {
                xlChart.HasTitle        = true;
                xlChart.ChartTitle.Text = strChartTitle;
            }

            // Xaxis title
            if (strXTitle != "")
            {
                Axis xAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory,
                                                XlAxisGroup.xlPrimary);
                xAxis.HasTitle            = true;
                xAxis.AxisTitle.Text      = strXTitle;
                xAxis.AxisTitle.Font.Size = xFontSize;
            }

            // legend:
            xlChart.HasLegend = false;
            if (listLegend != null)
            {
                if (listLegend.Count > 0)
                {
                    xlChart.HasLegend = true;

                    int seriesNum = 1;
                    foreach (string str in listLegend)
                    {
                        oS      = (Series)xlChart.SeriesCollection(seriesNum);
                        oS.Name = str;
                        seriesNum++;
                    }
                }
            }

            if (strYTitle != "")
            {
                Axis yAxis = (Axis)xlChart.Axes(XlAxisType.xlValue,
                                                XlAxisGroup.xlPrimary);
                yAxis.HasTitle            = true;
                yAxis.AxisTitle.Text      = strYTitle;
                yAxis.AxisTitle.Font.Size = yFontSize;
            }
        }
        /// <summary>为指定图表设置源数据区域。
        /// </summary>
        /// <param name="Source">包含源数据的区域。</param>
        /// <param name="PlotBy">指定数据绘制方式。可为以下 XlRowCol 常量之一:xlColumns 或 xlRows。</param>
        public void SetSourceData(Range Source, XlRowCol? PlotBy = null)
        {
            _objaParameters = new object[2] {
                Source._objRange,
                PlotBy == null ? System.Type.Missing : PlotBy
            };

            _objChart.GetType().InvokeMember("SetSourceData", BindingFlags.InvokeMethod, null, _objChart, _objaParameters);
        }
Example #4
0
 //插入图表操作
 //public void InsertPictures(string Filename, string ws, int Height, int Width)
 //插入图片操作二
 //{
 //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
 //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
 //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
 //}
 //public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)
 //插入图片操作三
 //{
 //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
 //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
 //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
 //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
 //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
 //}
 public void InsertActiveChart(XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, XlRowCol ChartDataType)
 {
     ChartDataType = XlRowCol.xlColumns;
     wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
     {
         wb.ActiveChart.ChartType = ChartType;
         wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
         wb.ActiveChart.Location(XlChartLocation.xlLocationAsObject, ws);
     }
 }
        /// <summary>修改给定图表的属性。可使用本方法快速设置图表的格式,而不必逐个设置所有属性。本方法是非交互式的,并且仅更改指定的属性。
        /// </summary>
        /// <param name="Source">包含新图表源数据的区域。如果省略本参数,Microsoft Excel 将编辑活动图表工作表或活动工作表上处于选定状态的图表。</param>
        /// <param name="Gallery">用于指定图表类型的 XlChartType 的常量之一。</param>
        /// <param name="Format">内置自动套用格式的选项编号。可为从 1 到 10 的数字,其取值取决于库的类型。如果省略此参数,Microsoft Excel 将根据库的类型和数据源选择默认值。</param>
        /// <param name="PlotBy">指定每个系列的数据是来自行还是来自列。可以是以下 XlRowCol 常量之一:xlRows 或 xlColumns。</param>
        /// <param name="CategoryLabels">指定包含分类标签的源范围内的行数或列数的整数。合法值为从 0(零)至小于相应分类或系列的最大个数间的某一数字。</param>
        /// <param name="SeriesLabels">指定包含系列标志的源范围内的行数或列数的整数。合法值为从 0(零)至小于相应分类或系列的最大个数间的某一数字。</param>
        /// <param name="HasLegend">若要包括图例,则为 True。</param>
        /// <param name="Title">图表标题文字。</param>
        /// <param name="CategoryTitle">分类轴标题文字。</param>
        /// <param name="ValueTitle">数值轴标题文字。</param>
        /// <param name="ExtraTitle">三维图表的系列轴标题,或二维图表的次数值轴标题。</param>
        public void ChartWizard(object Source = null, XlChartType? Gallery = null, int? Format = null, XlRowCol? PlotBy = null, int? CategoryLabels = null, int? SeriesLabels = null, bool? HasLegend = null, string Title = null, string CategoryTitle = null, string ValueTitle = null, string ExtraTitle = null)
        {
            _objaParameters = new object[11] {
                Source == null ? System.Type.Missing : Source,
                Gallery == null ? System.Type.Missing : Gallery,
                Format == null ? System.Type.Missing : Format,
                PlotBy == null ? System.Type.Missing : PlotBy,
                CategoryLabels == null ? System.Type.Missing : CategoryLabels,
                SeriesLabels == null ? System.Type.Missing : SeriesLabels,
                HasLegend == null ? System.Type.Missing : HasLegend,
                Title == null ? System.Type.Missing : Title,
                CategoryTitle == null ? System.Type.Missing : CategoryTitle,
                ValueTitle == null ? System.Type.Missing : ValueTitle,
                ExtraTitle == null ? System.Type.Missing : ExtraTitle
            };

            _objChart.GetType().InvokeMember("ChartWizard", BindingFlags.InvokeMethod, null, _objChart, _objaParameters);
        }