Adatta automaticamente righe e colonne

Adattamento automatico

Aspose.Cells fornisce una classe Workbook che rappresenta un file di Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente l’accesso a ciascun foglio di lavoro in un file di Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una vasta gamma di proprietà e metodi per la gestione di un foglio di lavoro. Questo articolo esamina l’utilizzo della classe Worksheet per adattare automaticamente le righe o le colonne.

Adatta automaticamente la riga - Semplice

L’approccio più diretto per ridimensionare automaticamente larghezza e altezza di una riga è chiamare il metodo AutoFitRow della classe Worksheet. Il metodo AutoFitRow richiede un indice di riga (della riga da ridimensionare) come parametro.

// 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.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// 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];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Come adattare automaticamente la riga in un intervallo di celle

Una riga è composta da molte colonne. Aspose.Cells consente agli sviluppatori di adattare automaticamente una riga in base al contenuto in un intervallo di celle all’interno della riga chiamando una versione sovraccaricata del metodo AutoFitRow. Richiede i seguenti parametri:

  • Indice riga, l’indice della riga da adattare automaticamente.
  • Primo indice colonna, l’indice della prima colonna della riga.
  • Ultimo indice colonna, l’indice dell’ultima colonna della riga.

Il metodo AutoFitRow controlla i contenuti di tutte le colonne nella riga e quindi adatta automaticamente la 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);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// 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];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1, 0, 5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Come adattare automaticamente la colonna in un intervallo di celle

Una colonna è composta da molte righe. È possibile adattare automaticamente una colonna in base al contenuto in un intervallo di celle nella colonna chiamando una versione sovraccarica del metodo AutoFitColumn che richiede i seguenti parametri:

  • Indice colonna, l’indice della colonna da adattare automaticamente.
  • Primo indice riga, l’indice della prima riga della colonna.
  • Ultimo indice di riga, l’indice dell’ultima riga della colonna.

Il metodo AutoFitColumn controlla il contenuto di tutte le righe nella colonna e quindi adatta automaticamente la larghezza della 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);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// 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];
// Auto-fitting the Column of the worksheet
worksheet.AutoFitColumn(4, 4, 6);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Come adattare automaticamente le righe per le celle unite

Con Aspose.Cells è possibile adattare automaticamente le righe anche per le celle che sono state unite utilizzando l’API AutoFitterOptions. La classe AutoFitterOptions fornisce la proprietà AutoFitMergedCellsType che può essere utilizzata per adattare automaticamente le righe per le celle unite. AutoFitMergedCellsType accetta l’enumerazione AutoFitMergedCellsType che ha i seguenti membri:

  • Nessuno: Ignora celle unite.
  • PrimaRiga: Espande solo l’altezza della prima riga.
  • UltimaRiga: Espande solo l’altezza dell’ultima riga.
  • OgniRiga: Espande solo l’altezza di ogni riga.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiate a new Workbook
Workbook wb = new Workbook();
// Get the first (default) worksheet
Worksheet _worksheet = wb.Worksheets[0];
// Create a range A1:B1
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2);
// Merge the cells
range.Merge();
// Insert value to the merged cell A1
_worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end";
// Create a style object
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle();
// Set wrapping text on
style.IsTextWrapped = true;
// Apply the style to the cell
_worksheet.Cells[0, 0].SetStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine;
// Autofit rows in the sheet(including the merged cells)
_worksheet.AutoFitRows(options);
// Save the Excel file
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx");

Importante sapere

Argomenti avanzati

  • Adattare automaticamente le righe per le celle unite