Autoajustar filas y columnas

Ajuste automático

Aspose.Cells proporciona unLibro de trabajoclase que representa un archivo de Excel Microsoft. ÉlLibro de trabajola clase contiene unHojas de trabajocolección que permite el acceso a cada hoja de trabajo en un archivo de Excel. Una hoja de trabajo está representada por elHoja de cálculo clase. ÉlHoja de cálculo La clase proporciona una amplia gama de propiedades y métodos para administrar una hoja de cálculo. Este artículo analiza el uso de laHoja de cálculoclase para autoajustar filas o columnas.

Autoajustar fila - Simple

El enfoque más sencillo para cambiar automáticamente el tamaño del ancho y el alto de una fila es llamar alHoja de cálculo claseAutoajustar fila método. ÉlAutoajustar filaEl método toma un índice de fila (de la fila a redimensionar) como parámetro.

// 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();

Autoajustar fila en un rango de Cells

Una fila se compone de muchas columnas. Aspose.Cells permite a los desarrolladores ajustar automáticamente una fila según el contenido de un rango de celdas dentro de la fila llamando a una versión sobrecargada delAutoajustar filamétodo. Toma los siguientes parámetros:

  • Índice de fila, el índice de la fila que se va a ajustar automáticamente.
  • Índice de la primera columna, el índice de la primera columna de la fila.
  • Índice de la última columna, el índice de la última columna de la fila.

ÉlAutoajustar filaEl método verifica el contenido de todas las columnas en la fila y luego ajusta automáticamente la fila.

// 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();

Autoajustar columna en un rango de Cells

Una columna se compone de muchas filas. Es posible ajustar automáticamente una columna en función del contenido de un rango de celdas de la columna llamando a una versión sobrecargada deAutoajustar columnamétodo que toma los siguientes parámetros:

  • índice de columnael índice de la columna a punto de autoajustarse.
  • Índice de la primera fila, el índice de la primera fila de la columna.
  • Índice de la última fila, el índice de la última fila de la columna.

ÉlAutoajustar columnaEl método verifica el contenido de todas las filas en la columna y luego ajusta automáticamente la columna.

// 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();

Autoajustar filas para combinar Cells

Con Aspose.Cells es posible autoajustar filas incluso para celdas que se han combinado usando elAutoFitterOpciones API. AutoFitterOpcionesclase proporcionaAutoFitMergedCellsType propiedad que se puede usar para autoajustar filas para celdas combinadas.AutoFitMergedCellsTypeaceptaAutoFitMergedCellsType enumerable que tiene los siguientes miembros.

  • Ninguno: ignora las celdas combinadas.
  • FirstLine: solo expande la altura de la primera fila.
  • LastLine: solo expande la altura de la última fila.
  • Cada línea: solo expande la altura de cada fila.
// 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 saber

Temas avanzados