如何使用aspose.cell将一个工作表复制到工作簿中的另一个工作表

时间:2014-05-08 10:10:22

标签: c# asp.net aspose-cells

如何使用带有c#的asp.net复制一个工作表值并在aspose.cell中的工作簿中粘贴另一个工作表?

谢谢并尊重, Parthiban K。

1 个答案:

答案 0 :(得分:3)

我在Aspose担任社交媒体开发人员。 Aspose.Cells提供多种选项来实现您期望的结果。您可以将数据从一个工作表导出到另一个工作表,然后使用导入数据到目标工作表。您还可以将所有数据作为范围复制到目标工作表。检查以下样本:

将范围从第一个工作表复制到目标工作表

//Open the workbook
Workbook workbook = new Workbook("book1.xlsx");

//Select source worksheet
Worksheet worksheet = workbook.Worksheets[0];


//Select Destination Worksheet
Worksheet destSheet = workbook.Worksheets[1]; 

//Get the range of cells with all the data from source worksheet
Aspose.Cells.Range sourceRange = worksheet.Cells.MaxDisplayRange;

//Create a range with same row and column count as source worksheet
Aspose.Cells.Range destRange = destSheet.Cells.CreateRange(sourceRange.FirstRow, sourceRange.FirstColumn, sourceRange.RowCount, sourceRange.ColumnCount);

//Select Paste Options
PasteOptions options = new PasteOptions();

options.PasteType = PasteType.All;

//Copy the range from source worksheet to destination.
destRange.Copy(sourceRange, options);

//Save the updated worksheet
workbook.Save("book1.xlsx");

从一个工作表导出数据并导入到另一个工作表

//Open the workbook
Workbook workbook = new Workbook("book1.xlsx");

//Select source worksheet
Worksheet worksheet = workbook.Worksheets[0];

//Exporting the of worksheet to DataTable
DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxRow, worksheet.Cells.MaxColumn, true);

//Select Destination Worksheet
Worksheet destSheet = workbook.Worksheets[1];

//Import data to destination worksheet
destSheet.Cells.ImportDataTable(dataTable, true, "A1");

//Save the updated worksheet
workbook.Save("book1.xlsx");