本教程将演示如何通过spire.xls for .net来将一个excel工作表按指定范围拆分为多个工作表。关于excel工作表合并请查阅这篇教程 - 。
测试文档:
c#
//加载excel工作簿,并获取第一张工作表
workbook bookoriginal = new workbook();
bookoriginal.loadfromfile("sample.xlsx");
worksheet sheet = bookoriginal.worksheets[0];
//新建工作簿1
workbook newbook1 = new workbook();
//获取源excel工作表中第1行至第10行的数据,将它们复制到newbook1的第一个工作表
worksheet newsheet1 = newbook1.worksheets[0];
cellrange range1 = sheet.range[1, 1, 10, sheet.lastcolumn];
sheet.copy(range1, newsheet1.range[1, 1]);
//重复以上操作步骤
workbook newbook2 = new workbook();
worksheet newsheet2 = newbook2.worksheets[0];
cellrange range2 = sheet.range[11, 1, 17, sheet.lastcolumn];
sheet.copy(range2, newsheet2.range[1, 1],true);
workbook newbook3 = new workbook();
worksheet newsheet3 = newbook3.worksheets[0];
cellrange range3 = sheet.range[18, 1, 26, sheet.lastcolumn];
sheet.copy(range3, newsheet3.range[1, 1],true);
//保存三个excel文件
newbook1.savetofile("表1.xlsx", excelversion.version2010);
newbook2.savetofile("表2.xlsx", excelversion.version2010);
newbook3.savetofile("表3.xlsx", excelversion.version2010);
vb.net
'加载excel工作簿,并获取第一张工作表
dim bookoriginal as new workbook()
bookoriginal.loadfromfile("sample.xlsx")
dim sheet as worksheet = bookoriginal.worksheets(0)
'新建工作簿1
dim newbook1 as new workbook()
'获取源excel工作表中第1行至第10行的数据,将它们复制到newbook1的第一个工作表
dim newsheet1 as worksheet = newbook1.worksheets(0)
dim range1 as cellrange = sheet.range(1, 1, 10, sheet.lastcolumn)
sheet.copy(range1, newsheet1.range(1, 1))
'重复以上操作步骤
dim newbook2 as new workbook()
dim newsheet2 as worksheet = newbook2.worksheets(0)
dim range2 as cellrange = sheet.range(11, 1, 17, sheet.lastcolumn)
sheet.copy(range2, newsheet2.range(1, 1), true)
dim newbook3 as new workbook()
dim newsheet3 as worksheet = newbook3.worksheets(0)
dim range3 as cellrange = sheet.range(18, 1, 26, sheet.lastcolumn)
sheet.copy(range3, newsheet3.range(1, 1), true)
'保存三个excel文件
newbook1.savetofile("表1.xlsx", excelversion.version2010)
newbook2.savetofile("表2.xlsx", excelversion.version2010)
newbook3.savetofile("表3.xlsx", excelversion.version2010)
拆分结果: