Formattare righe e colonne

Lavorare con le righe

Come regolare l’altezza della riga

Aspose.Cells fornisce una classe, Workbook, che rappresenta un file di Microsoft Excel. La classe Workbook contiene un WorksheetCollection che consente di accedere a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells che rappresenta tutte le celle nel foglio di lavoro.

La raccolta Cells fornisce diversi metodi per gestire righe o colonne in un foglio di lavoro. Alcuni di questi sono discussi in seguito in maggior dettaglio.

Come impostare l’altezza di una riga

È possibile impostare l’altezza di una singola riga chiamando il metodo SetRowHeight della raccolta Cells. Il metodo SetRowHeight prende i seguenti parametri come segue:

  • Indice di riga, l’indice della riga a cui si sta modificando l’altezza.
  • Altezza della riga, l’altezza della riga da applicare alla riga.
// 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();

Come impostare l’altezza di tutte le righe in un foglio di lavoro

Se gli sviluppatori hanno bisogno di impostare la stessa altezza della riga per tutte le righe nel foglio di lavoro, possono farlo utilizzando la proprietà StandardHeight della raccolta Cells.

Esempio:

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

Lavorare con colonne

Come impostare la larghezza di una colonna

Imposta la larghezza di una colonna chiamando il metodo SetColumnWidth della raccolta Cells. Il metodo SetColumnWidth prende i seguenti parametri:

  • Indice di colonna, l’indice della colonna a cui si sta modificando la larghezza.
  • Larghezza di colonna, la larghezza desiderata della colonna.
// 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();

Come impostare la larghezza della colonna in pixel

Imposta la larghezza di una colonna chiamando il metodo SetColumnWidthPixel della raccolta Cells. Il metodo SetColumnWidthPixel prende i seguenti parametri:

  • Indice di colonna, l’indice della colonna a cui si sta modificando la larghezza.
  • Larghezza della colonna, la larghezza della colonna desiderata in pixel.
// 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");

Come impostare la larghezza di tutte le colonne in un foglio di lavoro

Per impostare la stessa larghezza della colonna per tutte le colonne nel foglio di lavoro, utilizzare la proprietà StandardWidth della raccolta Cells.

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

Argomenti avanzati