本文介绍使用spire.xls for .net来复制excel中的指定单元格区域,包括复制单元格文本及单元格样式。复制时,可在工作簿中的同一个sheet工作表内复制,也可在不同工作簿的不同sheet工作表间复制。
在同一个sheet工作表中复制
c#
using spire.xls;
namespace copyranges_xls
{
class program
{
static void main(string[] args)
{
//加载excel工作簿
workbook book = new workbook();
book.loadfromfile("sample.xlsx", excelversion.version2013);
//获取第一个工作表
worksheet sheet = book.worksheets[0];
//将源数据数据复制到目标单元格区域
sheet.copy(sheet.range["a1:f1"], sheet.range["a6:f6"], true);
//保存文档
book.savetofile("result.xlsx", excelversion.version2013);
system.diagnostics.process.start("result.xlsx");
}
}
}
vb.net
imports spire.xls
namespace copyranges_xls
class program
private shared sub main(args as string())
'加载excel工作簿
dim book as new workbook()
book.loadfromfile("sample.xlsx", excelversion.version2013)
'获取第一个工作表
dim sheet as worksheet = book.worksheets(0)
'将源数据数据复制到目标单元格区域
sheet.copy(sheet.range("a1:f1"), sheet.range("a6:f6"), true)
'保存文档
book.savetofile("result.xlsx", excelversion.version2013)
system.diagnostics.process.start("result.xlsx")
end sub
end class
end namespace
在不同工作簿的sheet工作表间复制
c#
using spire.xls;
namespace copyranges2
{
class program
{
static void main(string[] args)
{
//加载excel工作簿1
workbook book1 = new workbook();
book1.loadfromfile("sample.xlsx", excelversion.version2013);
//加载excel工作簿2
workbook book2 = new workbook();
book2.loadfromfile("test.xlsx");
//获取工作表
worksheet sheet1 = book1.worksheets[0];
worksheet sheet2 = book2.worksheets[1];
//将源数据数据复制到目标单元格区域
sheet1.copy(sheet1.range["a1:f1"], sheet2.range["b4:g4"], true);
//保存工作簿2
book2.savetofile("result2.xlsx", excelversion.version2013);
system.diagnostics.process.start("result2.xlsx");
}
}
}
vb.net
imports spire.xls
namespace copyranges2
class program
private shared sub main(args as string())
'加载excel工作簿1
dim book1 as new workbook()
book1.loadfromfile("sample.xlsx", excelversion.version2013)
'加载excel工作簿2
dim book2 as new workbook()
book2.loadfromfile("test.xlsx")
'获取工作表
dim sheet1 as worksheet = book1.worksheets(0)
dim sheet2 as worksheet = book2.worksheets(1)
'将源数据数据复制到目标单元格区域
sheet1.copy(sheet1.range("a1:f1"), sheet2.range("b4:g4"), true)
'保存工作簿2
book2.savetofile("result2.xlsx", excelversion.version2013)
system.diagnostics.process.start("result2.xlsx")
end sub
end class
end namespace