using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Owc11;
namespace WindowsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
this.print1();
this.print2();
}
public void print1()
{
int[] Month = new int[3] { 1, 2, 3 };
Double[] Count = new double[3] { 120, 240, 220 };
ChartSpace mychartSpace = new ChartSpace();
ChChart mychart = mychartSpace.Charts.Add(0);
mychart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱型图
mychart.HasLegend = true;
mychart.HasTitle = true;
mychart.Title.Caption = "一季度总结";
mychart.Axes[0].HasTitle = true;
mychart.Axes[0].Title.Caption = "月份";
mychart.Axes[1].HasTitle = true;
mychart.Axes[1].Title.Caption = "销量";
mychart.SeriesCollection.Add(0);
mychart.SeriesCollection.Add(1);
mychart.SeriesCollection.Add(2);
mychart.SeriesCollection[0].Caption = "一月份";
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Month[0]);
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Count[0]);
mychart.SeriesCollection[1].Caption = "二月份";
mychart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Month[1]);
mychart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Count[1]);
mychart.SeriesCollection[2].Caption = "三月份";
mychart.SeriesCollection[2].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Month[2]);
mychart.SeriesCollection[2].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Count[2]);
mychartSpace.ExportPicture(Application.StartupPath + @"/test1.jpg", "jpg", 500, 600);
}
public void print2()
{
//创建X坐标的值,表示月份
int[] Month ={1,2,3};
//创建Y坐标的值,表示销售额
double[] Count = {120,240,220 };
string strDataName = "";
string strData = "";
//创建图表空间
ChartSpace mychartSpace = new ChartSpace();
//在图表空间内添加一个图表对象
ChChart mychart = mychartSpace.Charts.Add(0);
//设置每块饼的数据
for (int i = 0; i < Count.Length;i++ )
{
strDataName += Month[i] + "/t";
strData += Count[i].ToString() + "/t";
}
//设置图表类型,本例使用饼状图
mychart.Type = ChartChartTypeEnum.chChartTypePie;
//设置图表的一些属性
//是否需要图例
mychart.HasLegend = true;
//是否需要主题
mychart.HasTitle = true;
//主题内容
mychart.Title.Caption = "一季度总结";
//添加图标块
mychart.SeriesCollection.Add(0);
//设置图标块的属性
//分类属性
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strDataName);
//值属性
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strData);
//显示百分比
ChDataLabels mytb = mychart.SeriesCollection[0].DataLabelsCollection.Add();
mytb.HasPercentage = true;
//生成图片
mychartSpace.ExportPicture(Application.StartupPath + @"/test2.jpg", "jpg", 500, 600);
}
//导出数据到Excel表
public void Excel()
{
int[] Month = new int[3] { 1, 2, 3 };
Double[] Count = new double[3] { 120, 240, 220 };
ChartSpace mychartSpace = new ChartSpace();
//创建表容器
SpreadsheetClass myexl = new SpreadsheetClass();
//获取当前表
Worksheet mysheet = myexl.ActiveSheet;
//添加表标题
mysheet.Cells[1, 1] = "销售额";
mysheet.Cells[1, 2] = "季度";
//添加数据
for (int i = 0; i < 3;i++ )
{
//Cells的内容通常是指表格中的第几行和第几列,二维数
mysheet.Cells[i + 2, 1] = Count[i];
mysheet.Cells[i + 2, 2] = Month[i];
}
//导出表格
myexl.Export(Application.StartupPath + @"/Excel.xls",SheetExportActionEnum.ssExportActionOpenInExcel,SheetExportFormat.ssExportXMLSpreadsheet);
}
private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1.Image = new Bitmap(Application.StartupPath + @"/test1.jpg");
}
private void button2_Click(object sender, EventArgs e)
{
this.pictureBox1.Image = new Bitmap(Application.StartupPath + @"/test2.jpg");
}
private void button3_Click(object sender, EventArgs e)
{
this.Excel();
}
}
}