Copiare e Spostare Fogli di Lavoro
A volte è necessario avere un numero di fogli di lavoro con formattazione e dati comuni. Ad esempio, se si lavora con budget trimestrali, potrebbe essere necessario creare un libro con fogli che contengono gli stessi titoli di colonna, titoli di riga e formule. C’è un modo per farlo: creando un foglio e poi copiandolo.
Aspose.Cells supporta la copia e lo spostamento dei fogli di lavoro all’interno o tra cartelle di lavoro. I fogli di lavoro, completi di dati, formattazione, tabelle, matrici, grafici, immagini e altri oggetti, vengono copiati con il massimo grado di precisione.
Spostare o Copiare Fogli Usando Microsoft Excel
Di seguito sono riportati i passaggi coinvolti nella copia e nel trasferimento dei fogli di lavoro all’interno o tra i fogli di lavoro in Microsoft Excel.
- Per spostare o copiare i fogli in un altro libro, aprire il libro che riceverà i fogli.
- Passare al libro che contiene i fogli da spostare o copiare, e quindi selezionare i fogli.
- Nel menu Modifica, fare clic su Sposta o Copia Foglio.
- Nella finestra di dialogo Al libro, fare clic sul workbook per ricevere i fogli.
- Per spostare o copiare i fogli selezionati in un nuovo workbook, fare clic su Nuovo libro.
- Nella casella Prima del foglio, fare clic sul foglio prima del quale si desidera inserire i fogli spostati o copiati.
- Per copiare i fogli anziché spostarli, selezionare la casella Crea copia.
Copiare Fogli di Lavoro all’interno di un Workbook con Aspose.Cells
Aspose.Cells fornisce un metodo sovraccaricato, Aspose.Cells.WorksheetCollection.AddCopy(), che viene utilizzato per aggiungere un foglio di lavoro alla raccolta e copiare i dati da un foglio di lavoro esistente. Una versione del metodo prende l’indice del foglio di lavoro di origine come parametro. L’altra versione prende il nome del foglio di lavoro di origine.
Nell’esempio seguente viene mostrato come copiare un foglio di lavoro esistente all’interno di un libro.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "book1.xls"; | |
// Open an existing Excel file. | |
Workbook wb = new Workbook(InputPath); | |
// Create a Worksheets object with reference to | |
// the sheets of the Workbook. | |
WorksheetCollection sheets = wb.Worksheets; | |
// Copy data to a new sheet from an existing | |
// sheet within the Workbook. | |
sheets.AddCopy("Sheet1"); | |
// Save the Excel file. | |
wb.Save(dataDir + "CopyWithinWorkbook_out.xls"); |
Copiare i Fogli di Lavoro tra Cartelle di Lavoro
Aspose.Cells fornisce un metodo, Aspose.Cells.Worksheet.Copy() utilizzato per copiare dati e formattazione da un foglio di lavoro di origine a un altro foglio di lavoro all’interno o tra i fogli di lavoro. Il metodo prende l’oggetto del foglio di lavoro di origine come parametro.
L’esempio seguente mostra come copiare un foglio di lavoro da un libro di lavoro a un altro libro di lavoro.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "book1.xls"; | |
// Create a Workbook. | |
// Open a file into the first book. | |
Workbook excelWorkbook0 = new Workbook(InputPath); | |
// Create another Workbook. | |
Workbook excelWorkbook1 = new Workbook(); | |
// Copy the first sheet of the first book into second book. | |
excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]); | |
// Save the file. | |
excelWorkbook1.Save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls"); |
L’esempio seguente mostra come copiare un foglio di lavoro da un libro di lavoro a un altro.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create a new Workbook. | |
Workbook excelWorkbook0 = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet ws0 = excelWorkbook0.Worksheets[0]; | |
// Put some data into header rows (A1:A4) | |
for (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(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls"); |
Sposta i fogli di lavoro all’interno del libro di lavoro
Aspose.Cells fornisce un metodo Aspose.Cells.Worksheet.MoveTo() che viene utilizzato per spostare un foglio di lavoro in un’altra posizione nello stesso foglio di calcolo. Il metodo prende l’indice del foglio di lavoro di destinazione come parametro.
L’esempio seguente mostra come spostare un foglio di lavoro in un’altra posizione all’interno del libro di lavoro.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "book1.xls"; | |
// Open an existing excel file. | |
Workbook wb = new Workbook(InputPath); | |
// Create a Worksheets object with reference to | |
// the sheets of the Workbook. | |
WorksheetCollection sheets = wb.Worksheets; | |
// Get the first worksheet. | |
Worksheet worksheet = sheets[0]; | |
// Move the first sheet to the third position in the workbook. | |
worksheet.MoveTo(2); | |
// Save the excel file. | |
wb.Save(dataDir + "MoveWorksheet_out.xls"); |