怎样用c#操作excel里的chart对象

public void CreateChart()
{
    Excel.Worksheet thisWorksheet;
    thisWorksheet = thisWorkbook.ActiveSheet as Excel.Worksheet;
    Excel.ChartObjects charts = 
        (Excel.ChartObjects)thisWorksheet.ChartObjects(Type.Missing);

    // Adds a chart at x = 100, y = 300, 500 points wide and 300 tall.
    Excel.ChartObject chartObj = charts.Add(100, 300, 500, 300);
    Excel.Chart chart = chartObj.Chart;

    // Gets the cells that define the bounds of the data to be charted.
    Excel.Range chartRange = thisWorksheet.get_Range("A5","D8");
    chart.SetSourceData(chartRange,Type.Missing);

    chart.ChartType = Excel.XlChartType.xlXYScatter;
    Excel.SeriesCollection seriesCollection= 
        (Excel.SeriesCollection)chart.SeriesCollection(Type.Missing);
    Excel.Series series = seriesCollection.Item(seriesCollection.Count);
}
 
Find something in google.

Excel excel = new Excel();
            Worksheet sheet = excel.Worksheets[0];
            
            Cells cells = sheet.Cells;
            cells[0,1].PutValue("Income");
            cells[1,0].PutValue("Company A");
            cells[2,0].PutValue("Company B");
            cells[3,0].PutValue("Company C");
            cells[1,1].PutValue(10000);
            cells[2,1].PutValue(20000);
            cells[3,1].PutValue(30000);
            		
            int chartIndex = sheet.Charts.Add(ChartType.Column, 9, 9, 21, 15);
            
            Chart chart = sheet.Charts[chartIndex];
            chart.NSeries.Add("B2:B4", true);
            chart.NSeries.CategoryData = "A2:A4";
            
            ASeries aSeries = chart.NSeries[0];
            aSeries.Name = "=B1";
            chart.IsLegendShown = true;
            chart.Title.Text = "Income Analysis";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值