C#导出Excel图表 [英] C# Export Excel Chart

查看:476
本文介绍了C#导出Excel图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我正在尝试在图表中自动化excel文件,我想将图表导出到JPG文件或类似的东西,但是我收到了这个错误:

来自HRESULT的异常:0x800401A8

我搜索了几个小时并尝试了很多代码,但每次都出现同样的错误。

这是我的代码:



Hello friends
I''m trying to automate an excel file within a chart,and I want to export the chart to JPG file or something like this,but I got this error :
Exception from HRESULT: 0x800401A8
I searched hours and tried many codes but every time I get the same error.
here is my code:

Excel._Workbook workBook;
        Excel.Series series;
        Excel.Range range;
        Excel._Chart chart;

        workBook = (Excel._Workbook)workSheet.Parent;
        chart = (Excel._Chart)workBook.Charts.Add(Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);

        range = workSheet.get_Range(string.Format("C1:C{0}", rowCount), Missing.Value).get_Resize(
            Missing.Value, 1); ;
        chart.ChartWizard(range, Excel.XlChartType.xlColumnClustered, Missing.Value,
            Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, false,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        series = (Excel.Series)chart.SeriesCollection(1);
        series.XValues = workSheet.get_Range("A1", string.Format("B{0}", rowCount));

        series = (Excel.Series)chart.SeriesCollection(1);

        chart.ChartArea.Font.Name = "Tahoma";
        chart.AutoScaling = true;
        chart.ChartStyle = 27;

        Excel.Axis yaxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);

        yaxis.MinimumScaleIsAuto = false;
        yaxis.MinimumScale = 0;

        yaxis.MaximumScaleIsAuto = false;
        yaxis.MaximumScale = 10;

        chart.HasTitle = true;
        chart.ChartTitle.Text = "title";

        chart.Location(Excel.XlChartLocation.xlLocationAsObject, workSheet.Name);

        range = (Excel.Range)workSheet.Rows.get_Item(3, Missing.Value);
        workSheet.Shapes.Item("Chart 1").Top = (float)(double)range.Top;
        range = (Excel.Range)workSheet.Columns.get_Item(5, Missing.Value);
        workSheet.Shapes.Item("Chart 1").Left = (float)(double)range.Left;

        chart.Export(@"C:\Chart1.jpg", "JPG", false);

推荐答案

我没有这样的情况,但我在interopthing上编码了很多。在很多情况下,我的解决方案是通过以下方式以编程方式调用vba代码模块:



Microsoft.Vbe.Interop;



希望这有助于......
I did not had a situation like that but i coded a lot on the interopthing. in a lot of situations, my solution was to programmatically call the vba codemodule via:

Microsoft.Vbe.Interop;

hope this helps...


这篇关于C#导出Excel图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆