Beispiel #1
0
        Word.InlineShape InsertChart(Excel._Chart oChart, Word.Document oDoc, string filename, string placementName, float width, float height, float scale)
        {
            object oMissing = Missing.Value;
            string filepath = System.IO.Path.GetTempPath() + filename + ".png";

            oChart.ChartArea.Width  = width;
            oChart.ChartArea.Height = height;
            oChart.Export(filepath, "png", false);
            object oEndOfDoc = placementName;

            Word.Range       insertPoint = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            Word.InlineShape oShape      = insertPoint.InlineShapes.AddPicture(filepath, ref oMissing, ref oMissing, ref oMissing);
            oShape.Width  = width * scale;
            oShape.Height = height * scale;
            return(oShape);
        }
Beispiel #2
0
        protected void DrawChart(XlChartType ChartType)
        {
            // Variable declaration

            object misValue = System.Reflection.Missing.Value;

            ExcelObj.Range chartRange = null;

            try
            {
                this.Validate();

                // split the cell range for the work sheet range
                string cell1 = strCellRange.Split(':')[0];
                string cell2 = strCellRange.Split(':')[1];

                base.InitWorkSheet();


                if (base.SheetIndex > 0 && base.xlWorkSheet != null)
                {
                    ExcelObj.ChartObjects xlCharts = (ExcelObj.ChartObjects)base.xlWorkSheet.ChartObjects(Type.Missing);

                    try
                    {
                        // Define the cell range to the work sheet
                        chartRange = xlWorkSheet.get_Range(cell1, cell2);
                    }
                    catch (Exception)
                    {
                        base.ClearObject();

                        throw new Exception("Invalid Range");
                    }

                    // fix chart position by left,top,width,height
                    ExcelObj.ChartObject chartObj = (ExcelObj.ChartObject)xlCharts.Add(dblChartLeft, dblChartTop, dblChartWidth, dblChartHeight);

                    // Define char object
                    ExcelObj._Chart chart = chartObj.Chart;

                    // assign the datasource for the chart
                    chart.SetSourceData(chartRange, misValue);

                    // set the chart type.
                    chart.ChartType = ChartType;

                    // Ledgend show / hide
                    chart.HasLegend = this.Options.ShowLedgend;

                    // assign the chart title based on empty
                    if (!string.IsNullOrEmpty(strChartTitle))
                    {
                        chart.HasTitle        = true;
                        chart.ChartTitle.Text = strChartTitle;
                    }
                    else
                    {
                        chart.HasTitle = false;
                    }

                    XlDataLabelsType XlDataLabelsType = (XlDataLabelsType)this.Options.DataLabelsType;

                    XlDataLabelsType XlLedgendKey = (XlDataLabelsType)this.Options.LedgendKey;

                    // assign the data lable properties
                    chart.ApplyDataLabels(XlDataLabelsType, XlLedgendKey, this.Options.AutoText, this.Options.HasLeaderLines,
                                          this.Options.ShowSeriesName, this.Options.ShowCategoryName, this.Options.ShowValue,
                                          this.Options.ShowPercentage, this.Options.ShowBubbleSize, this.Options.Separator);

                    if (this.strImagecopy != null && !string.IsNullOrEmpty(this.strImagecopy))
                    {
                        string strExten = new FileInfo(this.strImagecopy).Extension;
                        strExten = strExten.Replace(".", "");
                        chart.Export(this.strImagecopy, strExten.ToUpper(), misValue);
                    }

                    base.SaveWorkBook(true);
                }
            }
            catch (ValidationException Vex)
            {
                base.ClearObject();
                throw Vex;
            }
            catch (Exception ex)
            {
                base.ClearObject();

                throw new Exception("DrawChart : " + ex.Message);
            }
        }