Source from: huide Control Network http://www.evget.com/zh-CN/Info/catalog/18074.html
Sometimes, you need to combine multiple worksheets into one. If the manual operation is simple, it is time-consuming and labor-consuming, and the effect is not good, but aspose. cells can be easily implemented. This article will show how to read a source workbook and combine the worksheets into oneCodeExample.
We will copy the content of multiple source worksheets in the workbook to one worksheet.
Source workbook
Here we first create a source workbook with three worksheets. See all three worksheets of the image.
Output Work thin
After running the following sample code, you will get a workbook with only one worksheet, which contains all the data in the preceding three worksheets ,:
Sample Code:
[C #]
String filepath = @ "C: \ source.xlsx"; workbook = new Workbook (filepath); workbook destworkbook = new Workbook (); worksheet destsheet = destworkbook. worksheets [0]; int totalrowcount = 0; For (INT I = 0; I <workbook. worksheets. count; I ++) {worksheet sourcesheet = workbook. worksheets [I]; range sourcerange = sourcesheet. cells. maxdisplayrange; range destrange = destsheet. cells. createRange (sourcerange. firstrow + totalrowcount, sourcerange. firstcolumn, sourcerange. rowcount, sourcerange. columncount); destrange. copy (sourcerange); totalrowcount = sourcerange. rowcount + totalrowcount;} destworkbook. save ("output.xlsx ");
[VB]
Dim filepath as string = "C: \ source.xlsx" dim workbook as workbook = new Workbook (filepath) dim destworkbook as workbook = new Workbook () dim destsheet as worksheet = destworkbook. worksheets (0) dim totalrowcount as integer = 0for I = 0 to workbook. worksheets. count-1 dim sourcesheet as worksheet = workbook. worksheets (I) dim sourcerange as range = sourcesheet. cells. maxdisplayrange dim destrange as range = destsheet. cells. createRange (sourcerange. firstrow + totalrowcount, sourcerange. firstcolumn, _ sourcerange. rowcount, sourcerange. columncount) destrange. copy (sourcerange) totalrowcount = sourcerange. rowcount + totalrowcountnextdestworkbook. save ("output.xlsx ")