用C#或VB.net如何访问Excel中插入的图表
我现在需要用VB.net编写一段代码,严重用户是否按照要求在Excel中生成了图表,以及图表的格式是否正确。但是不知如何编写代码。
图表我研究应该为Chart,但是却无法得到这些对象。
请高手帮忙指点一下!
[解决办法]
/// <summary>
/// 创建标准图表,存放到新开的表单中
/// </summary>
/// <param name= "Ex "> Excel.Application实例 </param>
/// <param name= "szRang "> 数据源 </param>
/// <param name= "xlType "> 图表类型 </param>
/// <param name= "nByRC "> 图表数据源按行或列 </param>
/// <param name= "szTitle "> 图表标题,缺省为空 </param>
/// <param name= "nLegend "> 图例,缺省为0 </param>
/// <param name= "bShowVal "> 是否在图上显示值,缺省为false </param>
public static void CreateChart_STD(Microsoft.Office.Interop.Excel.Application Ex, String szRang, Microsoft.Office.Interop.Excel.XlChartType xlType, Microsoft.Office.Interop.Excel.XlRowCol nByRC,
String szTitle, int nLegend, bool bShowVal) {
Excel.Range rng = Ex.get_Range(szRang, Type.Missing);
rng.Select();
Excel._Chart xlChart = (Excel.Chart)Ex.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlChart.ChartType = xlType;
xlChart.SetSourceData(rng.get_Range(szRang, Type.Missing), nByRC);
//图表标题
if (szTitle != " ") {
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = szTitle;
xlChart.ChartTitle.Font.Name = "Arial ";
xlChart.ChartTitle.Font.Size = 12;
xlChart.ChartTitle.Font.Bold = true;
}
//图例
if (nLegend < 0) {
xlChart.HasLegend = false;
xlChart.HasDataTable = true;
xlChart.DataTable.ShowLegendKey = true;
xlChart.DataTable.Font.Name = "Arial ";
xlChart.DataTable.Font.Size = 9;
}
else if (nLegend == 0)
xlChart.HasLegend = false;
else {
xlChart.HasLegend = true;
if (nLegend == 1) xlChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionBottom;
if (nLegend == 2) xlChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight;
}
// //是否显示值
// if(bShowVal==true) xlChart.ApplyDataLabels (Excel.XlDataLabelsType.xlDataLabelsShowNone ,false,Type.Missing ,Type.Missing );
// else xlChart.ApplyDataLabels (Excel.XlDataLabelsType.xlDataLabelsShowValue ,false,Type.Missing ,Type.Missing );
////Y轴网格线/字体
//Excel.Axis axis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
//axis.HasMajorGridlines = true;
//axis.Border.LineStyle = Excel.XlLineStyle.xlDot;
//axis.TickLabels.Font.Name = "Arial ";
//axis.TickLabels.Font.Size = 9;
////X轴网格线/字体
//axis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
//axis.HasMajorGridlines = true;
//axis.Border.LineStyle = Excel.XlLineStyle.xlDot;
//axis.TickLabels.Font.Name = "Arial ";
//axis.TickLabels.Font.Size = 9;
//图例颜色,为柱状或条状时,允许变更其颜色
if (xlType == Excel.XlChartType.xlColumnClustered || xlType == Excel.XlChartType.xlBarClustered) {
Excel.ChartGroup grp = (Excel.ChartGroup)xlChart.ChartGroups(1);
grp.GapWidth = 20;
grp.VaryByCategories = true;
}
//图表背景区底色
// xlChart.ChartArea.Fill.PresetTextured (Office.MsoPresetTexture.msoTextureBlueTissuePaper );
// xlChart.PlotArea.Fill.PresetTextured (Office.MsoPresetTexture.msoTextureBlueTissuePaper);
xlChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;
//在完成图表操作后执行图表定位
//新开一个图表存放 Chart
xlChart.Location(Excel.XlChartLocation.xlLocationAsNewSheet, Type.Missing);
}