VB.NET 如何向excel中插入一行或一列

rely 2006-04-28 03:14:15
VB.NET 如何向excel中插入一行或一列, 如何设置单元格格式如颜色和边框,
居中, 自动列宽等


急急,在线等
...全文
1226 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinweihong 2006-04-29
  • 打赏
  • 举报
回复
会Excel宏的话,任何语言操作都是一个样..
xjtandqt 2006-04-28
  • 打赏
  • 举报
回复
到这里看:
http://blog.csdn.net/xjtandqt/archive/2005/12/16/553929.aspx
rely 2006-04-28
  • 打赏
  • 举报
回复
不行啊,我已经得到了sheet对象,怎么在这个sheet插入一行呢

Dim xlbook As Excel.Workbook
Dim ws1 As Excel.Worksheet

xlbook = objExcelApp.Workbooks.Open(path)
ws1 = xlbook.Sheets.Item(1)
zhaoliang_chen 2006-04-28
  • 打赏
  • 举报
回复
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;306023
zhaoliang_chen 2006-04-28
  • 打赏
  • 举报
回复
首先的一步就是使用Tlbimp这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:

TlbImp Excel9.olb Excel.dll

只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。
下面就让我们具体看看C#是如何使用这些东东吧。

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);
copico 2006-04-28
  • 打赏
  • 举报
回复
http://www.codeproject.com/info/search.asp?cats=6&searchkw=Excel&Submit1=Search&author=&sd=15+Nov+1999&ed=28+Apr+2006

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧