使用Microsoft.Office.Interop.Excel的C#从其他WorkSheet添加新的SERIES

时间:2019-06-17 05:59:44

标签: c# excel-interop

我希望Excel看起来像这样:

enter image description here

enter image description here

我必须尝试创建工作簿和工作表,而不是添加图表并从另一个图表获取范围 对于下一步,我想显示多个在一个图表中看起来像这样的线图 但就目前而言,我只是不知道该怎么做

这是我的代码,也是我尝试过的:

但是我有一个错误

System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'

我尝试了其他方法,但在所有情况下都遇到此错误...
我没有找到任何例子。 而且我认为,在随机尝试中,我永远不会找到正确的方法来做到这一点...

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EXCELTEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Microsoft.Office.Interop.Excel.Application xlApp;
            xlApp = new Microsoft.Office.Interop.Excel.Application
            {
                DisplayAlerts = true,
                Visible = true
            };
            Workbook reportWorkbook = xlApp.Workbooks.Add();
            Worksheet reportWorksheet = reportWorkbook.Worksheets.Add();

            reportWorksheet.Cells[1, "B"] = "Volt";
            reportWorksheet.Cells[1, "A"] = "Capacity";

            int c = 0;
            int i = 2;
            double v = 2.5;
            double vDiff = 0.050;
            while ( i < 30 )
            {

                reportWorksheet.Cells[i, "B"] = v;
                reportWorksheet.Cells[i, "A"] = c;

                v += vDiff;
                vDiff -= 0.001;
                c += 10;
                i++;
            }

            Worksheet ChartWorksheet = reportWorkbook.Worksheets.Add();
            Range chartRange;
            ChartObjects xlCharts = (ChartObjects)ChartWorksheet.ChartObjects(Type.Missing);
            ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
            Chart chartPage = myChart.Chart;
            chartRange = ChartWorksheet.get_Range("'Sheet2'!A1", "'Sheet2'!B29");
            chartPage.SetSourceData(chartRange);
            //chartPage.ChartType = Excel.XlChartType.xlLine;
            chartPage.ChartType = XlChartType.xlLine;


        }
    }
}

1 个答案:

答案 0 :(得分:2)

代替使用此

  

chartRange = ChartWorksheet.get_Range("'Sheet2'!A1", "'Sheet2'!B29");

使用以下代码选择范围

chartRange = reportWorksheet.get_Range("A1","B39");

您应该使它正常工作。

编辑

要使Volt处于一边,请尝试以下操作:

Worksheet ChartWorksheet = reportWorkbook.Worksheets.Add();
ChartWorksheet = reportWorkbook.ActiveSheet;
ChartObjects xlCharts = (ChartObjects)ChartWorksheet.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
SeriesCollection oSeriesCollection = (SeriesCollection)myChart.Chart.SeriesCollection();
Series Data = oSeriesCollection.NewSeries();
Data.XValues = reportWorksheet.get_Range("A2", "A29");
Data.Values = reportWorksheet.get_Range("B2", "B29");
Data.Name = "Volt";
chartPage.ChartType = XlChartType.xlLineMarkers;

渲染的输出:

enter image description here