ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 在c#如何:使用 COM Interop 创建 Excel 电子表格

在c#如何:使用 COM Interop 创建 Excel 电子表格

原创 Linux操作系统 作者:wisdomone1 时间:2011-08-18 21:51:45 0 删除 编辑
http://msdn.microsoft.com/zh-cn/library/ms173186%28v=VS.80%29.aspx

从上述链接摘选关于application,workbook,worksheet,range的cs代码


using System;
using System.Reflection; 
using Microsoft.Office.Interop.Excel;

public class CreateExcelWorksheet
{
    static void Main()
    {
//excel应用程序
 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct."); return; }
//excel应用程序可用
xlApp.Visible = true; //在excel应用程序中添加workbook(工作薄) Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
//同理,在workbook添加worksheets,相当于excel一个页签
Worksheet ws = (Worksheet)wb.Worksheets[1]; if (ws == null) { Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct."); } // Select the Excel cells, in the range c1 to c7 in the worksheet.
//选中某一个范围
Range aRange = ws.get_Range("C1", "C7"); if (aRange == null) { Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs."); } // Fill the cells in the C1 to C7 range of the worksheet with the number 6. Object[] args = new Object[1]; args[0] = 6;

//反射gettype()
 aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args); // Change the cells in the C1 to C7 range of the worksheet to the number 8. aRange.Value2 = 8; } }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-705295/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
提供针对oracle初学者及进阶的数据库培训,欢迎大家咨询: 微信: wisdomone 微信公众号: lovedb 工作经历: 中国普天 北京科蓝 北京云和恩墨 北京神州新桥

注册时间:2008-04-04

  • 博文量
    2189
  • 访问量
    11977424