Formato de filas y columnas

Trabajar con filas

Ajuste de altura de fila

Aspose.Cells proporciona una clase,Libro de trabajo , que representa un archivo de Excel Microsoft. ÉlLibro de trabajo la clase contiene unColección de hojas de trabajoque permite el acceso a cada hoja de trabajo en el archivo de Excel. Una hoja de trabajo está representada por elHoja de cálculo clase. ÉlHoja de cálculo la clase proporciona unCellscolección que representa todas las celdas de la hoja de trabajo.

ÉlCellscolección proporciona varios métodos para administrar filas o columnas en una hoja de trabajo. Algunos de estos se discuten a continuación con más detalle.

Establecer la altura de una fila

Es posible establecer la altura de una sola fila llamando alCells colecciónEstablecerRowHeight método. ÉlEstablecerRowHeightEl método toma los siguientes parámetros de la siguiente manera:

  • Índice de fila, el índice de la fila cuya altura está cambiando.
  • Altura de la fila, el alto de fila que se aplicará en 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// 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];
// Setting the height of the second row to 13
worksheet.Cells.SetRowHeight(1, 13);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Establecer la altura de todas las filas en una hoja de cálculo

Si los desarrolladores necesitan establecer la misma altura de fila para todas las filas de la hoja de cálculo, pueden hacerlo mediante elAltura estándar propiedad de laCellsrecopilación.

Ejemplo:

// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// 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];
// Setting the height of all rows in the worksheet to 15
worksheet.Cells.StandardHeight = 15;
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Trabajar con columnas

Establecer el ancho de una columna

Establezca el ancho de una columna llamando alCells colecciónEstablecer ancho de columna método. ÉlEstablecer ancho de columnamétodo toma los siguientes parámetros:

  • índice de columna, el índice de la columna cuyo ancho está cambiando.
  • Ancho de columna, el ancho de columna deseado.
// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// 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];
// Setting the width of the second column to 17.5
worksheet.Cells.SetColumnWidth(1, 17.5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Configuración del ancho de columna en píxeles

Establezca el ancho de una columna llamando alCellscolecciónEstablecerColumnaAnchoPíxelmétodo. ÉlEstablecerColumnaAnchoPíxelmétodo toma los siguientes parámetros:

  • índice de columna, el índice de la columna cuyo ancho está cambiando.
  • Ancho de columnael ancho de columna deseado en píxeles.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
string outDir = RunExamples.Get_OutputDirectory();
//Load source Excel file
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx");
//Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Set the width of the column in pixels
worksheet.Cells.SetColumnWidthPixel(7, 200);
workbook.Save(outDir + "SetColumnWidthInPixels_Out.xlsx");

Establecer el ancho de todas las columnas en una hoja de trabajo

Para establecer el mismo ancho de columna para todas las columnas de la hoja de trabajo, use elCells colecciónAncho estándarpropiedad.

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// 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];
// Setting the width of all columns in the worksheet to 20.5
worksheet.Cells.StandardWidth = 20.5;
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Temas avanzados