Essential XlsIO是一款类似Excel的电子数据表格控件,可以对Excel文件进行各种操作,对单元格进行各种格式化,设置单元格类型、插入公式等,这篇文章主要介绍如何使用Essential XlsIO提供的API来从一个Excel文件中的单元格复制内容到另外一个Excel中,具体如下:
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook sourceWorkbook = application.Workbooks.Open("SourceWorkbook.xlsx", ExcelOpenType.Automatic);
IWorkbook destinationWorkbook = application.Workbooks.Open("DestinationWorkbook.xlsx", ExcelOpenType.Automatic);
IWorksheet SourceWorksheet = SourceWorkbook.Worksheets[0];
//The first worksheet object in the worksheets collection in the Destination Workbook is accessed.
IWorksheet DestinationWorksheet = DestinationWorkbook.Worksheets[0];
//Assigning an object to the range of cells (90 rows) both for source and destination.
IRange sourceRange = SourceWorksheet.Range[1, 1, 90, 100];
IRange destinationRange = DestinationWorksheet.Range[1, 1, 90, 100];
//Copying (90 rows) from Source to Destination worksheet.
sourceRange.CopyTo(destinationRange);
destinationWorkbook.SaveAs("CopyingRange.xlsx");
destinationWorkbook.Close();
excelEngine.Dispose();