C#Interop Excel创建直方图

时间:2018-08-07 15:41:57

标签: c# excel histogram excel-interop

我想在已经存在的电子表格xlWorkSheetDia上创建直方图,以在电子表格xlWorkSheetData中显示数据。

我遇到的主要问题是创建直方图,因为Excel.XlChartType不建议我使用xlHistogram枚举器。我尝试查找该枚举数,并根据此问题中的建议自行创建它:C# - why Histogram does not work in Excel 2016?

我用对象资源管理器查询了枚举数,它也是118(十进制)。但是在应用图表类型时出现了超出范围的异常。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            try
            {
                string path = @"C:\TestData.csv";
                string destPath = @"C:\TestData.txt";
                //Create an instance for word app
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheetDia, xlWorkSheetData;
                xlApp.Visible = true;  //Set status for word application is to be visible or not.

                System.IO.File.Copy(sourceFileName: path, destFileName: destPath, overwrite: true); // use txt file instead of csv, for correct delimiter parsing
                //Open WorkBook to have csv Data
                xlApp.Workbooks.OpenText(destPath, DataType: Excel.XlTextParsingType.xlDelimited, Semicolon: true);
                //Creating necessary objects
                xlWorkBook = xlApp.ActiveWorkbook;
                xlWorkSheetData = xlWorkBook.Sheets[1];
                xlWorkSheetData.Name = "Data";
                xlWorkSheetDia = xlWorkBook.Sheets.Add();
                xlWorkSheetDia.Name = "Diagrams";

                //Add chart 
                var charts = xlWorkSheetDia.ChartObjects() as Excel.ChartObjects;
                var chartObject = charts.Add(60, 10, 300, 300) as Excel.ChartObject;
                var chart = chartObject.Chart;
                //Move Chart
                chartObject.Chart.Location(Where: Excel.XlChartLocation.xlLocationAsObject, Name:xlWorkSheetDia.Name); //already gets created on the right place(first spreadsheet)
                //Set chart range
                chart.SetSourceData(xlWorkSheetData.get_Range("A1", "A5"));//Data on second spreadsheet
                //Set chart properties.
                Excel.XlChartType myHistogram = (Excel.XlChartType) 118;


                chart.ChartType = myHistogram; //<-The exception happens here


                //cleanup
                xlWorkBook.Close(false);
                xlApp.Quit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

以及一些测试数据:

A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;
2,138;2,066;2,022;2,076;0,297;0;0;0,77;1,898;1,864;1,798;1,859;1,715;20,032;8,599;0,039;0;0;0,336;Iteration: 5 of 500
0,298;0,295;0,298;0,297;0,297;0;0;0,302;0,296;0,296;0,297;0,297;0,299;0,3;0,296;0,037;0;0;0,409;Iteration: 6 of 500
0,297;0,299;0,296;0,297;0,311;0;0;0,295;0,295;0,295;0,295;0,295;0,295;0,306;0,306;0,039;0;0;0,372;Iteration: 7 of 500
0,299;0,298;0,295;0,297;0,295;0;0;0,294;0,307;0,295;0,295;0,296;0,297;0,296;0,295;0,04;0;0;0,368;Iteration: 8 of 500

当我尝试记录用于创建直方图的宏时,得到以下命令:

Sub Makro1()
'
' Makro1 Makro
'

'
    Range("A1:A5").Select
    ActiveSheet.Shapes.Range(Array("Chart 1")).Select
End Sub

在我看来,这与我在代码示例中尝试的方式完全不同。我不知道该如何用C#编写代码,因为我没有相同的方法。

有什么建议或暗示可以解决我的问题吗?

0 个答案:

没有答案