使用C#在Excel文档中创建图表
2008-04-18 23:09
736 查看
using MSExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
string path;
MSExcel.Application excelApp;
MSExcel.Workbook excelDoc;
path = @"d:/test.xlsx";
excelApp = new MSExcel.ApplicationClass();
if(File.Exists(path))
{
File.Delete(path);
}
Object nothing = Missing.Value;
excelDoc = excelApp.Workbooks.Add(nothing);
MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheet[1];
MSExcel.Range r;
r = ws.get_Range("A1","A1");
r.Value2 = "3";
r = ws.get_Range("A2","A2");
r.Value2 = "5.7";
r = ws.get_Range("A3","A3");
r.Value2 = "4.8";
r = ws.get_Range("A4","A4");
r.Value2 = "9.2";
r = ws.get_Range("A5","A5");
r.Value2 = "6.4";
excelDoc.Charts.Add(nothing,nothing,nothing,nothing);
excelDoc.ActiveChart.ChartType = MSExcel.XlChartType.xlColumnClustered;
excelDoc.ActiveChart.SetSourceData(ws.get_Range("A1","A5"),MSExcel.XlRowCol.xlColumns);
excelDoc.ActiveChart.Location(MSExcel.XlChartLocation.xlLocationAsObject,"sheet1");
excelDoc.ActiveChart.HasTitle = true;
excelDoc.ActiveChart.ChartTitle.Text = "创建图表";
excelDoc.ActiveChart.hasDataTable = false;
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
excelDoc.SaveAs(path,format,nothing,nothing,nothing,nothing,
MSExcel.XlSaveAsAccessMode.xlExclusive,nothing,nothing,nothing,nothing,nothing);
excelDoc.Close(nothing,nothing,nothing);
excelApp.Quit();
}
}
using System.IO;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
string path;
MSExcel.Application excelApp;
MSExcel.Workbook excelDoc;
path = @"d:/test.xlsx";
excelApp = new MSExcel.ApplicationClass();
if(File.Exists(path))
{
File.Delete(path);
}
Object nothing = Missing.Value;
excelDoc = excelApp.Workbooks.Add(nothing);
MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheet[1];
MSExcel.Range r;
r = ws.get_Range("A1","A1");
r.Value2 = "3";
r = ws.get_Range("A2","A2");
r.Value2 = "5.7";
r = ws.get_Range("A3","A3");
r.Value2 = "4.8";
r = ws.get_Range("A4","A4");
r.Value2 = "9.2";
r = ws.get_Range("A5","A5");
r.Value2 = "6.4";
excelDoc.Charts.Add(nothing,nothing,nothing,nothing);
excelDoc.ActiveChart.ChartType = MSExcel.XlChartType.xlColumnClustered;
excelDoc.ActiveChart.SetSourceData(ws.get_Range("A1","A5"),MSExcel.XlRowCol.xlColumns);
excelDoc.ActiveChart.Location(MSExcel.XlChartLocation.xlLocationAsObject,"sheet1");
excelDoc.ActiveChart.HasTitle = true;
excelDoc.ActiveChart.ChartTitle.Text = "创建图表";
excelDoc.ActiveChart.hasDataTable = false;
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
excelDoc.SaveAs(path,format,nothing,nothing,nothing,nothing,
MSExcel.XlSaveAsAccessMode.xlExclusive,nothing,nothing,nothing,nothing,nothing);
excelDoc.Close(nothing,nothing,nothing);
excelApp.Quit();
}
}
相关文章推荐
- Excel中 如何使用不连贯的数据来创建图表(C#)
- 使用C#创建Excel文档
- VC使用Excel创建保存新workbook文档 删除worksheet
- 使用xml来创建excel文档,运行时无需excel
- C#使用NOPI库实现导入Excel文档
- C#操作office进行Excel图表创建,保存本地,word获取
- C#调用NPOI自动创建Excel文档(一)
- C#调用NPOI创建Excel文档样式设置方法总结
- [C#]使用Gembox.SpreadSheet向Excel写入数据及图表
- HOWTO:在 Visual C# .NET 中使用自动化创建 Excel 宏
- C#调用NPOI创建Excel文档合并后的单元格写入数据问题一则
- 使用C#向Excel文档写入数据
- 使用XML创建Excel文档
- VS+C#由创建的Excel生成PDF文档
- Net使用Microsoft.Office.Interop.Excel;创建Excel文件(插入数据、修改格式、生成图表)的方法,以及Excel查看加密
- C#调用NPOI创建Excel文档单元格写入问题一则
- 使用XML创建Excel文档
- 使用C#对Excel文档的相关操作
- 使用MyXls修改已有excel文档,在save时报错“未创建新实例”
- C#使用POI把DataGridView中数据导出到excel文档中