Adatta automaticamente righe e colonne con C++
Adattamento automatico
Aspose.Cells fornisce una classe Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione Worksheets che permette l’accesso a ogni foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet offre un’ampia gamma di metodi per gestire un foglio di lavoro. Questo articolo esplora l’uso della classe Worksheet per adattare automaticamente righe o colonne.
Adatta automaticamente la riga - Semplice
L’approccio più semplice per adattare automaticamente la larghezza e l’altezza di una riga è chiamare il metodo AutoFitRow della classe Worksheet. Il metodo AutoFitRow accetta come parametro un indice di riga (della riga da ridimensionare).
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Auto-fit the 2nd row (index 1) of the worksheet
worksheet.AutoFitRow(1);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Row auto-fitted and file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Come adattare automaticamente la riga in un intervallo di celle
Una riga è composta da molte colonne. Aspose.Cells permette 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. Prende 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 verifica i contenuti di tutte le colonne della riga e quindi la adatta automaticamente.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"output.xlsx";
// Open the Excel file
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1, 0, 5);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row auto-fitted and file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Come adattare automaticamente la colonna in un intervallo di celle
Una colonna è composta da molte righe. È possibile adattare automaticamente una colonna in base ai contenuti in un intervallo di celle della colonna chiamando una versione sovraccaricata del metodo AutoFitColumn che prende 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 verifica i contenuti di tutte le righe nella colonna e quindi adatta automaticamente la colonna.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Auto-fit the 5th column (index 4) from row 4 to 6
worksheet.AutoFitColumn(4, 4, 6);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Column auto-fitted and file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Come adattare automaticamente le righe per le celle unite
Con Aspose.Cells, è possibile adattare automaticamente le righe anche per celle unite utilizzando l’API AutoFitterOptions. La classe AutoFitterOptions fornisce la proprietà GetAutoFitMergedCellsType() che può essere usata per adattare automaticamente le righe delle celle unite. GetAutoFitMergedCellsType() accetta l’enumerazione AutoFitMergedCellsType, che presenta i seguenti membri:
- Nessuno: Ignora le celle unite.
- PrimaLinea: espande solo l’altezza della prima riga.
- UltimaLinea: espande solo l’altezza dell’ultima riga.
- OgniLinea: espande solo l’altezza di ogni riga.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Output directory
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Instantiate a new Workbook
Workbook wb;
// Get the first (default) worksheet
Worksheet worksheet = wb.GetWorksheets().Get(0);
// Create a range A1:B1
Range range = worksheet.GetCells().CreateRange(0, 0, 1, 2);
// Merge the cells
range.Merge();
// Insert value to the merged cell A1
worksheet.GetCells().Get(0, 0).SetValue(u"A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
Style style = worksheet.GetCells().Get(0, 0).GetStyle();
// Set wrapping text on
style.SetIsTextWrapped(true);
// Apply the style to the cell
worksheet.GetCells().Get(0, 0).SetStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options;
// Set auto-fit for merged cells
options.SetAutoFitMergedCellsType(AutoFitMergedCellsType::EachLine);
// Autofit rows in the sheet (including the merged cells)
worksheet.AutoFitRows(options);
// Save the Excel file
wb.Save(outDir + u"AutofitRowsforMergedCells.xlsx");
std::cout << "Autofit rows for merged cells completed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Puoi anche provare a utilizzare le versioni sovraccaricate dei metodi AutoFitRows e AutoFitColumns che accettano un intervallo di righe/colonne e un’istanza di AutoFitterOptions per adattare automaticamente le righe/colonne selezionate con i tuoi AutoFitterOptions desiderati.
Le firme dei metodi sopra indicati sono le seguenti:
- AutoFitRows(int startRow, int endRow, AutoFitterOptions options)
- AutoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)