Adatta righe e colonne

Montaggio automatico

Aspose.Cells fornisce aCartella di lavoroclass che rappresenta un file Excel Microsoft. IlCartella di lavorola classe contiene unFogli di lavororaccolta 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 fornisce un’ampia gamma di proprietà e metodi per la gestione di un foglio di lavoro. Questo articolo esamina l’utilizzo diFoglio di lavoroclass per adattare automaticamente righe o colonne.

Riga AutoFit - Semplice

L’approccio più diretto per ridimensionare automaticamente la larghezza e l’altezza di una riga consiste nel chiamare il metodoFoglio di lavoro classeAutoFitRow metodo. IlAutoFitRowIl metodo accetta 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();

AutoFit Row in un intervallo di Cells

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 diAutoFitRowmetodo. Richiede i seguenti parametri:

  • Indice di riga, l’indice della riga che sta per essere adattata automaticamente.
  • Indice della prima colonna, l’indice della prima colonna della riga.
  • Indice dell’ultima colonna, l’indice dell’ultima colonna della riga.

IlAutoFitRowIl metodo controlla il contenuto 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();

Colonna AutoFit in un intervallo di Cells

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 sovraccaricata diAdatta colonnametodo che accetta i seguenti parametri:

  • Indice di colonnal’indice della colonna che sta per essere adattata automaticamente.
  • Indice prima riga, l’indice della prima riga della colonna.
  • Indice dell’ultima riga, l’indice dell’ultima riga della colonna.

IlAdatta colonnaIl metodo controlla il contenuto di tutte le righe nella colonna e quindi adatta automaticamente la 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();

Adatta righe per unione Cells

Con Aspose.Cells è possibile adattare automaticamente le righe anche per le celle che sono state unite utilizzando ilAutoFitterOptions API. AutoFitterOptionsla classe fornisceAutoFitMergedCellsType proprietà che può essere utilizzata per adattare automaticamente le righe per le celle unite.AutoFitMergedCellsTypeaccettaAutoFitMergedCellsType enumerabile che ha i seguenti membri.

  • Nessuno: ignora le celle unite.
  • FirstLine: espande solo l’altezza della prima riga.
  • LastLine: espande solo l’altezza dell’ultima riga.
  • EachLine: 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 da sapere

Argomenti avanzati