Exemplo n.º 1
0
        public void DrawStat(int dataBRow, int dataBCol, int dataERow, int dataECol, string workSheetName, string title, string xName, string yName, masExcelDrawStatType charType, bool displayDataLabel, byte baseCol, bool hasLegend)
        {
            Excel.Worksheet xlSheet = null;
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                if (sheet.Name == workSheetName)
                {
                    xlSheet = sheet;
                    break;
                }
            }
            if (xlSheet == null)
            {
                xlSheet = AddNewSheet(workSheetName);
            }
            if (xlSheet == null)
            {
                //MsgBox.ShowInfo("Excel添加新工作薄失败!");
                return;
            }
            Excel.ChartObjects chartObjects = (Excel.ChartObjects)xlSheet.ChartObjects(Type.Missing);
            Excel.ChartObject  chartObj     = chartObjects.Add(5, 300 * _count + 20, (dataECol - dataBCol) * GetWidth() * (dataERow - dataBRow) + 200, 250);
            Excel.Chart        xlChart      = chartObj.Chart;
            XlChartType        xlCharType   = GetXlCharTypeFromDrawStatType(charType);
            Range range = worksheet.get_Range(worksheet.Cells[dataERow, dataECol] as Excel.Range, worksheet.Cells[dataBRow, dataBCol] as Excel.Range);

            xlChart.ChartWizard(range, xlCharType, Type.Missing, baseCol == 1 ? Excel.XlRowCol.xlColumns : Excel.XlRowCol.xlRows,
                                1, 1, hasLegend, title, xName, yName, "");
            //xlChart.SetSourceData(range, Excel.XlRowCol.xlColumns);
            //xlChart.ChartTitle.Text = title;
            //xlChart.ChartTitle.Caption
            //xlChart.HasLegend = true;

            if (displayDataLabel)
            {
                for (int i = 0; i < dataERow - dataBRow; i++)
                {
                    ((Excel.Series)xlChart.SeriesCollection(i + 1)).ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowValue,
                                                                                    missing, missing, missing);
                }
            }
            _count++;
        }
Exemplo n.º 2
0
        public void DrawStat(int dataBRow, int dataBCol, int dataERow, int dataECol, string workSheetName, string title, string xName, string yName, masExcelDrawStatType charType, bool displayDataLabel, bool isUnion, int unionBRow, int unionBCol, byte baseCol)
        {
            bool addNewSheet = false;

            Excel.Worksheet xlSheet = null;
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                if (sheet.Name == workSheetName)
                {
                    xlSheet = sheet;
                    break;
                }
            }
            if (xlSheet == null)
            {
                xlSheet     = AddNewSheet(workSheetName);
                addNewSheet = true;
            }
            if (xlSheet == null)
            {
                MsgBox.ShowInfo("Excel添加新工作薄失败!");
                return;
            }
            Range range = null;

            if (baseCol != 1)
            {
                range = worksheet.get_Range(worksheet.Cells[dataERow, dataECol] as Excel.Range, worksheet.Cells[dataBRow, dataBCol] as Excel.Range);
            }
            else
            {
                range = worksheet.get_Range(worksheet.Cells[dataERow, dataECol] as Excel.Range, worksheet.Cells[dataBRow, dataBCol + 1] as Excel.Range);
            }

            int rangCount = range.Rows.Count;

            if (isUnion)
            {
                if (baseCol != 1)
                {
                    range = xlApp.Union(worksheet.get_Range(worksheet.Cells[unionBRow, dataECol] as Excel.Range, worksheet.Cells[unionBRow, unionBCol] as Excel.Range), range);
                }
                else
                {
                    range = xlApp.Union(worksheet.get_Range(worksheet.Cells[unionBRow, dataECol] as Excel.Range, worksheet.Cells[unionBRow, dataBCol + 1] as Excel.Range), range);
                    range = xlApp.Union(worksheet.get_Range(worksheet.Cells[unionBRow, unionBCol] as Excel.Range, worksheet.Cells[unionBRow, unionBCol] as Excel.Range), range);
                    range = xlApp.Union(worksheet.get_Range(worksheet.Cells[dataERow, unionBCol] as Excel.Range, worksheet.Cells[dataBRow, unionBCol] as Excel.Range), range);
                }
                rangCount += 1;
            }

            Excel.ChartObjects chartObjects = (Excel.ChartObjects)xlSheet.ChartObjects(Type.Missing);
            Excel.ChartObject  chartObj     = chartObjects.Add(5, 300 * _count + 20, (dataECol - dataBCol) * GetWidth() * (rangCount - 1) + 200, 250);
            Excel.Chart        xlChart      = chartObj.Chart;
            XlChartType        xlCharType   = GetXlCharTypeFromDrawStatType(charType);


            xlChart.ChartWizard(range, xlCharType, Type.Missing, baseCol == 1 ? Excel.XlRowCol.xlColumns : Excel.XlRowCol.xlRows,
                                1, 1, true, title, xName, yName, "");


            if (displayDataLabel)
            {
                for (int i = 0; i < rangCount - 1; i++)
                {
                    ((Excel.Series)xlChart.SeriesCollection(i + 1)).ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowValue,
                                                                                    missing, missing, missing);
                }
            }
            if (!addNewSheet)
            {
                _count++;
            }
        }