[英]C# using Microsoft.Office.Interop.Excel Add new SERIES With range from other WorkSheet
I want the Excel to look like that:我希望 Excel 看起来像这样:
I have to try to create a workbook and Worksheets than add a chart and get a range from another chart for the next step I want to show multiple graphs of lines that looks this that in one chart But for now, I just don't know how to do this我必须尝试创建一个工作簿和工作表,而不是添加一个图表并从另一个图表中获取范围以进行下一步我想在一个图表中显示多个看起来像这样的线图但现在,我只是不知道这该怎么做
this is my code and what I have tried:这是我的代码和我尝试过的:
But I have got an error as但我有一个错误
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
I have tried other ways but in all the cases I got this error...我尝试过其他方法,但在所有情况下我都遇到了这个错误......
I did not find any example of that.. And I think In random tries I will never get the right way to do that...我没有找到任何例子......而且我认为在随机尝试中我永远不会得到正确的方法来做到这一点......
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;
}
}
}
Instead of using this而不是使用这个
chartRange = ChartWorksheet.get_Range("'Sheet2'!A1", "'Sheet2'!B29");
Use the below code to select the range使用以下代码选择范围
chartRange = reportWorksheet.get_Range("A1","B39");
and you should be getting it working..你应该让它工作..
To get the Volt on side, try following:要将 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;
The rendered output:渲染的输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.