• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Excel在C#中实现添加Book与sheet

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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+1;
}
object[] args2 = new object[1];
args2[0] = array2;
range2.gettype().invokemember("value", bindingflags.setproperty, null, range2, args2);



/// <summary>
/// 将指定sheet页的内容完全复制另一sheet页
/// </summary>
/// <param name="beforeSheet">被复制的sheet页 </param>
/// <param name="afterSheet">目标sheet页 </param>
/// <param name="startRow">复制内容在目标sheet页的开始行 </param>
/// <param name="startColumn">复制内容在目标sheet页的开始列 </param>
/// <returns>是否复制成功 </returns>
private bool CopyRange(Excel._Worksheet beforeSheet, Excel._Worksheet afterSheet, int startRow, int startColumn)
{
bool isCopyRange = false;
try
{
Excel.Range beforeRange = beforeSheet.UsedRange;
int rangeRowCount = beforeRange.Rows.Count;
int rangeColumnCount = beforeRange.Columns.Count;


// 空出将写数据集的EXCEL的范围
for (int row = 0; row <= rangeRowCount - 1; row++)
{

Excel.Range rang = afterSheet.get_Range(afterSheet.Cells[startRow, 1],
afterSheet.Cells[startRow, 1]).EntireRow;
//插入一行。
afterSheet.get_Range(afterSheet.Cells[startRow + row, 1],
afterSheet.Cells[startRow + row, 1]).EntireRow.Insert(Excel.XlDirection.xlUp, null);
Excel.Range rangTarget = afterSheet.get_Range(afterSheet.Cells[startRow + row, 1],
afterSheet.Cells[startRow + row, 1]).EntireRow;
rang.Copy(rangTarget);

}

beforeRange.Copy(afterSheet.get_Range(afterSheet.Cells[startRow, startColumn],
afterSheet.Cells[startRow, startColumn + rangeColumnCount]));

isCopyRange = true;
}
catch
{

}
return isCopyRange;
}
#endregion


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#语言开发的WinForm简易计算器发布时间:2022-07-10
下一篇:
C#根据Url下载文件/获取文件流发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap