Inserimento ed eliminazione di righe e colonne di file Excel

introduzione

Sia che si crei un nuovo foglio di lavoro da zero o che si lavori su un foglio di lavoro esistente, potrebbe essere necessario aggiungere ulteriori righe o colonne per contenere più dati. Al contrario, potremmo anche dover eliminare righe o colonne da posizioni specificate nel foglio di lavoro. Per soddisfare questi requisiti, Aspose.Cells fornisce un insieme molto semplice di classi e metodi, discussi di seguito.

Gestisci righe e colonne

Aspose.Cells offre un corsoCartella di lavoro , che rappresenta un file Excel Microsoft. IlCartella di lavoro la classe contiene unFogli di lavoro raccolta che consente l’accesso a ciascun foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato daFoglio di lavoro classe. IlFoglio di lavoro la classe fornisce aCellsraccolta che rappresenta tutte le celle del foglio di lavoro.

IlCellscollection fornisce diversi metodi per gestire righe e colonne in un foglio di lavoro. Alcuni di questi sono discussi di seguito.

Inserisci righe e colonne

Inserisci una riga

Inserisci una riga nel foglio di lavoro in qualsiasi posizione chiamando il metodoInserisciRiga metodo delCells collezione. IlInserisciRigaIl metodo accetta l’indice della riga in cui verrà inserita la nuova riga.

// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRow(2);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Inserisci più righe

Per inserire più righe in un foglio di lavoro, chiama il metodoInserisciRighe metodo delCells collezione. IlInserisciRighemetodo accetta due parametri:

  • Indice di riga, l’indice della riga da cui verranno inserite le nuove righe.
  • Numero di righe, il numero totale di righe che devono essere inserite.
// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting 10 rows into the worksheet starting from 3rd row
worksheet.Cells.InsertRows(2, 10);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Inserisci una riga con formattazione

Per inserire una riga con opzioni di formattazione, utilizzare ilInserisciRighesovraccarico che prendeInsertOptions come parametro. Impostare ilCopyFormatType proprietà diInsertOptions classe conCopyFormatType Enumerazione. IlCopyFormatTypeL’enumerazione ha tre membri come elencato di seguito.

  • SameAsAbove: formatta la riga come la riga precedente.
  • SameAsBelow: formatta la riga come sotto la riga.
  • Cancella: cancella la formattazione.
// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting Formatting options
InsertOptions insertOptions = new InsertOptions();
insertOptions.CopyFormatType = CopyFormatType.SameAsAbove;
// Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRows(2, 1, insertOptions);
// Saving the modified Excel file
workbook.Save(dataDir + "InsertingARowWithFormatting.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Inserisci una colonna

Gli sviluppatori possono anche inserire una colonna nel foglio di lavoro in qualsiasi posizione chiamando il metodoInserisci colonna metodo delCellscollezione. IlInserisci colonnaIl metodo accetta l’indice della colonna in cui verrà inserita la nuova colonna.

// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting a column into the worksheet at 2nd position
worksheet.Cells.InsertColumn(1);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Elimina righe e colonne

Elimina più righe

Per eliminare più righe da un foglio di lavoro, chiama il metodoElimina righe metodo delCells collezione. IlElimina righemetodo accetta due parametri:

  • Indice di riga, l’indice della riga da cui verranno eliminate le righe.
  • Numero di righe, il numero totale di righe che devono essere eliminate.
// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.OpenOrCreate);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting 10 rows from the worksheet starting from 3rd row
worksheet.Cells.DeleteRows(2, 10);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Elimina una colonna

Per eliminare una colonna dal foglio di lavoro in qualsiasi posizione, chiama il metodoElimina colonna metodo delCells collezione. IlElimina colonnaIl metodo accetta l’indice della colonna da eliminare.

// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting a column from the worksheet at 5th position
worksheet.Cells.DeleteColumn(4);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();