Форматирование строк и столбцов
Работа со строками
Как настроить высоту строки
Aspose.Cells предоставляет класс Workbook, который представляет файл Microsoft Excel. Класс Workbook содержит WorksheetCollection, позволяющий получить доступ к каждому листу в файле Excel. Лист представлен классом Worksheet. Класс Worksheet предоставляет коллекцию Cells, которая представляет все ячейки на листе.
Коллекция Cells предоставляет несколько методов для управления строками или столбцами на листе. Некоторые из них обсуждаются ниже более подробно.
Как установить высоту строки
Можно установить высоту отдельной строки, вызвав метод SetRowHeight коллекции Cells. Метод 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(); |
Как установить высоту всех строк на листе
Если разработчикам необходимо установить одинаковую высоту всех строк на листе, они могут сделать это, используя свойство StandardHeight коллекции 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(); |
Работа с колонками
Как установить ширину столбца
Установите ширину столбца, вызвав метод коллекции SetColumnWidth Cells. Метод 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(); |
Как установить ширину столбца в пикселях
Установите ширину столбца, вызвав метод коллекции SetColumnWidthPixel Cells. Метод 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"); |
Как установить ширину всех столбцов в листе Excel
Чтобы установить одинаковую ширину столбца для всех столбцов в листе, используйте свойство StandardWidth коллекции 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(); |
Продвинутые темы
- Автоподгонка строк и столбцов
- Преобразование текста в столбцы с использованием Aspose.Cells
- Копирование строк и столбцов
- Удаление пустых строк и столбцов в листе Excel
- Группировка и разгруппировка строк и столбцов
- Скрытие и отображение строк и столбцов
- Вставка или удаление строк в листе Excel
- Вставка и удаление строк и столбцов в файле Excel
- Удалить дублирующиеся строки в рабочем листе
- Обновление ссылок в других листах при удалении пустых столбцов и строк на листе