调整行高和列宽

处理行

调整行高

Aspose.Cells提供了一个代表Microsoft Excel文件的类WorkbookWorkbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类代表。Worksheet类提供了一个Cells集合,表示工作表中的所有单元格。Cells集合提供了若干方法来管理工作表中的行或列。以下是其中一些更详细的讨论。

设置行的高度

可以通过调用Cells集合的SetRowHeight方法来设置单行的高度。SetRowHeight方法采用以下参数:

  • 行索引,要更改高度的行的索引。
  • 行高,要应用于该行的行高。
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleRowsAndColumns = dirPath + u"sampleRowsAndColumns.xlsx";
//Path of output excel file
U16String outputRowsAndColumns = outPath + u"outputRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Setting the height of the second row to 35
worksheet.GetCells().SetRowHeight(1, 35);
//Save the Excel file.
workbook.Save(outputRowsAndColumns);
Aspose::Cells::Cleanup();

设置工作表中所有行的高度

如果开发人员需要为工作表中的所有行设置相同的行高,可以使用Cells集合的SetStandardHeight方法。

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleRowsAndColumns = dirPath + u"sampleRowsAndColumns.xlsx";
//Path of output excel file
U16String outputRowsAndColumns = outPath + u"outputRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Setting the height of all rows in the worksheet to 25
worksheet.GetCells().SetStandardHeight(25);
//Save the Excel file.
workbook.Save(outputRowsAndColumns);
Aspose::Cells::Cleanup();

使用列

设置列的宽度

通过调用 Cells 集合的 SetColumnWidth 方法来设置列的宽度。SetColumnWidth 方法接受以下参数:

  • 列索引,要更改其宽度的列的索引。
  • 列宽度,所需的列宽度。
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleRowsAndColumns = dirPath + u"sampleRowsAndColumns.xlsx";
//Path of output excel file
U16String outputRowsAndColumns = outPath + u"outputRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Setting the width of the second column to 56.5
worksheet.GetCells().SetColumnWidth(1, 56.5);
//Save the Excel file.
workbook.Save(outputRowsAndColumns);
Aspose::Cells::Cleanup();

设置工作表中所有列的宽度

要在工作表中为所有列设置相同的列宽度,请使用 Cells 集合的 SetStandardWidth 方法。

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleRowsAndColumns = dirPath + u"sampleRowsAndColumns.xlsx";
//Path of output excel file
U16String outputRowsAndColumns = outPath + u"outputRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Setting the width of all columns in the worksheet to 20.5
worksheet.GetCells().SetStandardWidth(20.5);
//Save the Excel file.
workbook.Save(outputRowsAndColumns);
Aspose::Cells::Cleanup();