Chinaunix首页 | 论坛 | 博客
  • 博客访问: 351879
  • 博文数量: 51
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 613
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-06 17:19
文章分类

全部博文(51)

文章存档

2011年(1)

2010年(3)

2009年(27)

2008年(20)

我的朋友

分类:

2009-12-07 09:44:38

C#.NET实现EXCEL操作类 - .NET读取和设置EXCEL电子表格单元的参考代码

using System;
using Excel;

namespace ExcelExample
{
///
/// Summary description for Class1.
///

class ExcelClass
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
Excel.Application excelApp = new Excel.ApplicationClass(); // Creates a new Excel Application
excelApp.Visible = false; // Makes Excel visible to the user.

// The following line adds a new workbook
Excel.Workbook newWorkbook = excelApp.Workbooks.Add(
XlWBATemplate.xlWBATWorksheet);

// The following code opens an existing workbook
string workbookPath = "c:/SomeWorkBook.xls"; // Add your own path here
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);

// The following gets the Worksheets collection
Excel.Sheets excelSheets = excelWorkbook.Worksheets;

// The following gets Sheet1 for editing
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

// The following gets cell A1 for editing
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");

// The following sets cell A1's value to "Hi There"
excelCell.Value2 = "Hi There";
excelApp.Visible = true;
}
}
阅读(1110) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~