c#调用Excel需要引用命名空间
using Microsoft.Office.Interop.Excel;
由于该程序不复杂,主要是根据不同数据画表和图,画的图像也并不复杂,因为画图和画表的操作会重复使用,所以,我把画表和图的操作放在一个类中
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; namespace Excel { class SheetControl { Worksheet sheet; public SheetControl(Worksheet sheet) { this.sheet = sheet; } /// <summary> /// 根据条件绘制图表 /// </summary> /// <param name="rg">绘制图表的区域</param> /// <param name="data">数据区域</param> /// <param name="type">图表类型,XlChartType枚举类型</param> /// <param name="xlrc">设置以行或者列为系列,XlRowCol枚举类型</param> /// <param name="title">表格标题</param> /// <param name="CategoryTitle">表格分类坐标名称,即横坐标名称</param> /// <param name="ValueTitle">表格数据坐标的名称,即纵坐标名称</param> public void CreateChart(Range rg, Range data, Object type = null, XlRowCol xlrc = XlRowCol.xlColumns, string title = null, string CategoryTitle = null, string ValueTitle = null) { ChartObjects charts = (ChartObjects)sheet.ChartObjects(Type.Missing); Chart