用.NET 操作Excel编程;
来源:互联网 发布:java中重载 编辑:程序博客网 时间:2024/05/22 01:34
引用:D:\Program Files\Microsoft Office\Office\EXCEL9.OLB
1. 创建一个新Excel的Application:
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
}
2. 让这个工程可见:
exc.set_Visible(0, true);
3. 获取WorkBooks集合:
Workbooks workbooks = exc.Workbooks;
4. 加入新的WorkBook:
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
5. 获取WorkSheets集合:
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}
6. 给单元格设置变量:
Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);
例程:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
class Excel {
public static int Main() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}
exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}
Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}
现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
const int nCell = 5;
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCell];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
Object[] args2 = new Object[1];
args2[0] = array2;
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);
1. 创建一个新Excel的Application:
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
}
2. 让这个工程可见:
exc.set_Visible(0, true);
3. 获取WorkBooks集合:
Workbooks workbooks = exc.Workbooks;
4. 加入新的WorkBook:
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
5. 获取WorkSheets集合:
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}
6. 给单元格设置变量:
Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);
例程:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
class Excel {
public static int Main() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}
exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}
Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}
现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
const int nCell = 5;
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCell];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
Object[] args2 = new Object[1];
args2[0] = array2;
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);
- 用.NET 操作Excel编程;
- .NET操作EXCEL编程
- 用ASP.NET操作Excel
- 用ASP.NET操作Excel
- 用ASP.NET操作Excel
- 用ASP.NET操作Excel
- Asp.Net用OWC操作Excel
- Asp.Net用OWC操作Excel
- Asp.Net用OWC操作Excel
- Asp.Net用OWC操作Excel
- .net 操作EXCEL
- .net 操作EXCEL
- .net 操作excel
- .net 操作EXCEL
- C#.Net操作Excel
- .Net C# 操作Excel
- .net操作EXCEL
- .Net操作Excel实践
- 各种排序
- 寄存器变量
- ZooKeeper搭建系列集 (这套很全,也很详细)
- 加密---Cipher
- UTC = GMT ; UTC + 8 = Beijing
- 用.NET 操作Excel编程;
- 随想录(以师带徒的原则)
- C++中Overload、Overwrite及Override的区别
- 复习struts2之请求参数接收
- win7 下面 xp mode 无法ping 通
- HTML中Post和Get区别
- Android编译错误:You have tried to change the API from what has been previously approved.
- HDU-1753 实数高精度加法
- IBM RSA (IBM rational software architect ) V8 建模学习之四 C++类图转换成c++代码