Copiar hojas de trabajo entre libros de trabajo

Aspose.Cells proporciona un método, Aspose.Cells.Worksheet.Copy(), que se usa para copiar datos y formato de una hoja de trabajo de origen a otra hoja de trabajo dentro o entre libros de trabajo. El método toma el objeto de la hoja de cálculo de origen como parámetro.

El siguiente ejemplo muestra cómo copiar una hoja de trabajo de un libro de trabajo a otro libro de trabajo.

string FilePath = @"..\..\..\Archivos de muestra\";

string FileName = FilePath + "Copiar hoja entre Workbook.xlsx";

//Crear un nuevo libro de trabajo.

Libro de trabajo excelWorkbook0 = nuevo libro de trabajo ();

//Obtener la primera hoja de trabajo del libro.

Hoja de trabajo ws0 = excelWorkbook0.Worksheets[0];

//Pon algunos datos en las filas del encabezado (A1:A4)

 para (int i = 0; i< 5; i++)

{

    ws0.Cells[i, 0].PutValue(string.Format("Header Row {0}", i));

}

//Put some detail data (A5:A999)

for (int i = 5; i < 1000; i++)

{

    ws0.Cells[i, 0].PutValue(string.Format("Detail Row {0}", i));

}

//Define a pagesetup object based on the first worksheet.

PageSetup pagesetup = ws0.PageSetup;

//The first five rows are repeated in each page...

//It can be seen in print preview.

pagesetup.PrintTitleRows = "$1:$5";

//Create another Workbook.

Workbook excelWorkbook1 = new Workbook();

//Get the first worksheet in the book.

Worksheet ws1 = excelWorkbook1.Worksheets[0];

//Name the worksheet.

ws1.Name = "MySheet";

//Copy data from the first worksheet of the first workbook into the

//first worksheet of the second workbook.

ws1.Copy(ws0);

//Save the excel file.

excelWorkbook1.Save(FileName);

Descargar código de muestra

El siguiente ejemplo muestra cómo copiar una hoja de trabajo de un libro de trabajo a otro libro de trabajo.

 //Crear un nuevo libro de trabajo.

Libro de trabajo excelWorkbook0 = nuevo libro de trabajo ();

//Obtener la primera hoja de trabajo del libro.

Hoja de trabajo ws0 = excelWorkbook0.Worksheets[0];

//Pon algunos datos en las filas del encabezado (A1:A4)

 para (int i = 0; i< 5; i++)

{

	ws0.Cells[i, 0].PutValue(string.Format("Header Row {0}", i));

}

//Put some detail data (A5:A999)

for (int i = 5; i < 1000; i++)

{

	ws0.Cells[i, 0].PutValue(string.Format("Detail Row {0}", i));

}

//Define a pagesetup object based on the first worksheet.

PageSetup pagesetup = ws0.PageSetup;

//The first five rows are repeated in each page...

//It can be seen in print preview.

pagesetup.PrintTitleRows = "$1:$5";

//Create another Workbook.

Workbook excelWorkbook1 = new Workbook();

//Get the first worksheet in the book.

Worksheet ws1 = excelWorkbook1.Worksheets[0];

//Name the worksheet.

ws1.Name = "MySheet";

//Copy data from the first worksheet of the first workbook into the

//first worksheet of the second workbook.

ws1.Copy(ws0);

//Save the excel file.

excelWorkbook1.Save("copyworksheet.xls");

Descargar código de muestra