Ajustar automáticamente filas y columnas

Ajuste automático

Aspose.Cells proporciona una clase Workbook que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección Worksheets que permite acceder a cada hoja de cálculo en un archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet ofrece una amplia gama de propiedades y métodos para gestionar una hoja de cálculo. Este artículo examina el uso de la clase Worksheet para ajustar automáticamente filas o columnas.

Ajuste automático de fila - Simple

El enfoque más directo para ajustar automáticamente el ancho y alto de una fila es llamar al método AutoFitRow de la clase Worksheet. El método AutoFitRow toma como parámetro el índice de la fila a redimensionar.

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

Cómo AutoAjustar una Fila en un Rango de Celdas

Una fila está compuesta por muchas columnas. Aspose.Cells permite a los desarrolladores ajustar automáticamente una fila en función del contenido en un rango de celdas dentro de la fila llamando a una versión sobrecargada del método AutoFitRow. Toma los siguientes parámetros:

  • Índice de la fila, el índice de la fila 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.

El método AutoFitRow 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();

Cómo AutoAjustar una Columna en un Rango de Celdas

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

  • Índice de columna, el índice de la columna que se va a ajustar automáticamente.
  • Í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.

El método AutoFitColumn 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();

Cómo AutoAjustar Filas para Celdas Fusionadas

Con Aspose.Cells es posible ajustar automáticamente filas incluso para celdas que han sido fusionadas utilizando la API AutoFitterOptions. La clase AutoFitterOptions proporciona la propiedad AutoFitMergedCellsType que se puede utilizar para ajustar automáticamente filas para celdas fusionadas. AutoFitMergedCellsType acepta el enumerador AutoFitMergedCellsType que tiene los siguientes miembros.

  • Ninguna: Ignorar celdas fusionadas.
  • PrimeraLínea: Solo expande la altura de la primera fila.
  • ÚltimaLínea: Solo expande la altura de la última fila.
  • EachLine: 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