Форматировать строки и столбцы

Работа со строками

Регулировка высоты строки

Aspose.Cells предоставляет класс,Рабочая тетрадь , представляющий файл Excel Microsoft.Рабочая тетрадь класс содержитРабочий листКоллекциякоторый позволяет получить доступ к каждому рабочему листу в файле Excel. Рабочий лист представленРабочий лист учебный класс.Рабочий лист класс предоставляетCellsколлекция, представляющая все ячейки рабочего листа.

Cellscollection предоставляет несколько методов для управления строками или столбцами на листе. Некоторые из них обсуждаются ниже более подробно.

Установка высоты строки

Можно установить высоту одной строки, вызвав функциюCells коллекцияSetRowHeight метод.SetRowHeightМетод принимает следующие параметры следующим образом:

  • Индекс строки, индекс строки, высоту которой вы меняете.
  • Высота строки, высота строки, применяемая к строке.
// 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();

Установка высоты всех строк на листе

Если разработчикам нужно установить одинаковую высоту для всех строк на листе, они могут сделать это с помощьюСтандартВысота собственность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);
// 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();

Работа со столбцами

Установка ширины столбца

Задайте ширину столбца, вызвав методCells коллекцияSetColumnWidth метод.SetColumnWidthметод принимает следующие параметры:

  • Индекс столбца, индекс столбца, ширину которого вы меняете.
  • Ширина колонки, желаемая ширина столбца.
// 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();

Установка ширины столбца в пикселях

Задайте ширину столбца, вызвав методCellsколлекцияSetColumnWidthPixelметод.SetColumnWidthPixelметод принимает следующие параметры:

  • Индекс столбца, индекс столбца, ширину которого вы меняете.
  • Ширина колонкижелаемая ширина столбца в пикселях.
// 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");

Установка ширины всех столбцов на листе

Чтобы установить одинаковую ширину столбца для всех столбцов на листе, используйте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();

Предварительные темы