Formater les lignes et les colonnes

Travailler avec des lignes

Réglage de la hauteur de rangée

Aspose.Cells fournit une classe,Cahier , qui représente un fichier Excel Microsoft. LeCahier classe contient unWorksheetCollectionqui permet d’accéder à chaque feuille de calcul dans le fichier Excel. Une feuille de calcul est représentée par leFeuille de travail classe. LeFeuille de travail la classe offre uneCellscollection qui représente toutes les cellules de la feuille de calcul.

LeCellscollection fournit plusieurs méthodes pour gérer les lignes ou les colonnes dans une feuille de calcul. Certains d’entre eux sont discutés ci-dessous plus en détail.

Définition de la hauteur d’une ligne

Il est possible de définir la hauteur d’une seule ligne en appelant leCells de la collectionDéfinir la hauteur de ligne méthode. LeDéfinir la hauteur de ligneprend les paramètres suivants comme suit :

  • Indice de ligne, l’index de la ligne dont vous modifiez la hauteur.
  • Hauteur de ligne, la hauteur de ligne à appliquer sur la ligne.
// 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();

Définition de la hauteur de toutes les lignes d’une feuille de calcul

Si les développeurs doivent définir la même hauteur de ligne pour toutes les lignes de la feuille de calcul, ils peuvent le faire en utilisant leHauteur standard propriété de laCellsle recueil.

Exemple:

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

Travailler avec des colonnes

Définition de la largeur d’une colonne

Définissez la largeur d’une colonne en appelant laCells de la collectionDéfinirLargeurColonne méthode. LeDéfinirLargeurColonneméthode prend les paramètres suivants :

  • Indice de colonne, l’index de la colonne dont vous modifiez la largeur.
  • Largeur de colonne, la largeur de colonne souhaitée.
// 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();

Définition de la largeur de colonne en pixels

Définissez la largeur d’une colonne en appelant laCellsde la collectionDéfinirLargeurColonnePixelméthode. LeDéfinirLargeurColonnePixelméthode prend les paramètres suivants :

  • Indice de colonne, l’index de la colonne dont vous modifiez la largeur.
  • Largeur de colonnela largeur de colonne souhaitée en pixels.
// 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");

Définition de la largeur de toutes les colonnes dans une feuille de calcul

Pour définir la même largeur de colonne pour toutes les colonnes de la feuille de calcul, utilisez laCells de la collectionLargeur standardla propriété.

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

Sujets avancés