使用C#和Excel进行报表开发(三)-生成统计图(Chart)4
来源:互联网 发布:python 隐马尔可夫模型 编辑:程序博客网 时间:2024/06/04 19:48
private void button4_Click(object sender, EventArgs e)
{
try
{
ThisApplication = new Excel.Application();
m_objBooks = (Excel.Workbooks)ThisApplication.Workbooks;
ThisWorkbook = (Excel._Workbook)(m_objBooks.Add(Type.Missing));
ThisApplication.DisplayAlerts = false;
this.DeleteSheet();
this.AddDatasheet();
this.LoadData();
CreateChart();
ThisWorkbook.SaveAs("z:\\Book2.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ThisWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
ThisApplication.Workbooks.Close();
ThisApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisWorkbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisApplication);
ThisWorkbook = null;
ThisApplication = null;
GC.Collect();
this.Close();
}
}
Excel.Application ThisApplication = null;
Excel.Workbooks m_objBooks = null;
Excel._Workbook ThisWorkbook = null;
Excel.Worksheet xlSheet = null;
/**//// <summary>
/// 用生成的随机数作数据
/// </summary>
private void LoadData()
{
Random ran = new Random();
for (int i = 1; i <= 12; i++)
{
xlSheet.Cells[i, 1] = i.ToString() + "月";
xlSheet.Cells[i, 2] = ran.Next(2000).ToString();
}
}
/**//// <summary>
/// 删除多余的Sheet
/// </summary>
private void DeleteSheet()
{
foreach (Excel.Worksheet ws in ThisWorkbook.Worksheets)
if (ws != ThisApplication.ActiveSheet)
{
ws.Delete();
}
foreach (Excel.Chart cht in ThisWorkbook.Charts)
cht.Delete();
}
/**//// <summary>
/// 创建一个Sheet,用来存数据
/// </summary>
private void AddDatasheet()
{
xlSheet = (Excel.Worksheet)ThisWorkbook.
Worksheets.Add(Type.Missing, ThisWorkbook.ActiveSheet,
Type.Missing, Type.Missing);
xlSheet.Name = "数据";
}
{
try
{
ThisApplication = new Excel.Application();
m_objBooks = (Excel.Workbooks)ThisApplication.Workbooks;
ThisWorkbook = (Excel._Workbook)(m_objBooks.Add(Type.Missing));
ThisApplication.DisplayAlerts = false;
this.DeleteSheet();
this.AddDatasheet();
this.LoadData();
CreateChart();
ThisWorkbook.SaveAs("z:\\Book2.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ThisWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
ThisApplication.Workbooks.Close();
ThisApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisWorkbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisApplication);
ThisWorkbook = null;
ThisApplication = null;
GC.Collect();
this.Close();
}
}
Excel.Application ThisApplication = null;
Excel.Workbooks m_objBooks = null;
Excel._Workbook ThisWorkbook = null;
Excel.Worksheet xlSheet = null;
/**//// <summary>
/// 用生成的随机数作数据
/// </summary>
private void LoadData()
{
Random ran = new Random();
for (int i = 1; i <= 12; i++)
{
xlSheet.Cells[i, 1] = i.ToString() + "月";
xlSheet.Cells[i, 2] = ran.Next(2000).ToString();
}
}
/**//// <summary>
/// 删除多余的Sheet
/// </summary>
private void DeleteSheet()
{
foreach (Excel.Worksheet ws in ThisWorkbook.Worksheets)
if (ws != ThisApplication.ActiveSheet)
{
ws.Delete();
}
foreach (Excel.Chart cht in ThisWorkbook.Charts)
cht.Delete();
}
/**//// <summary>
/// 创建一个Sheet,用来存数据
/// </summary>
private void AddDatasheet()
{
xlSheet = (Excel.Worksheet)ThisWorkbook.
Worksheets.Add(Type.Missing, ThisWorkbook.ActiveSheet,
Type.Missing, Type.Missing);
xlSheet.Name = "数据";
}
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)4
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)1
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)2
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)3
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)5
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- Unity3D Shader官方教程翻译(九)----Shader语法:Pass的雾化处理
- 数据结构实验2---表达式求值(“栈”实现)
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)3
- EXTJS初级UI设计进阶教程(2)
- <zz>信息检索中Recall, Precision等参数的理解
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)4
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)5
- 数字图像处理3---图像基本运算
- EXTJS初级UI设计进阶教程(3)
- 打包时sun.misc.ServiceConfigurationError
- 网络基础
- 转: 函数的线程安全和可重入
- unity3d 后台资源加载 yield用法
- ffmpeg开发指南