自动调整行和列

自动调整

Aspose.Cells 提供了一个 Workbook 类,表示 Microsoft Excel 文件。Workbook 类包含一个 Worksheets 集合,允许访问 Excel 文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供了一系列属性和方法,用于管理工作表。本文探讨了使用 Worksheet 类自动调整行或列。

自动调整行 - 简单

自动调整行宽度和高度最直接的方法是调用 Worksheet 类的 AutoFitRow 方法。AutoFitRow 方法以要调整行的索引作为参数。

// 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);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// 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];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

如何在单元格范围内自动调整行

一行由许多列组成。Aspose.Cells 允许开发人员调用 AutoFitRow 方法的重载版本,根据行内单元格范围中的内容自动调整行。它接受以下参数:

  • 行索引,即要自动调整的行的索引。
  • 第一个列索引,即行的第一个列的索引。
  • 最后列索引,指行的最后一列的索引。

AutoFitRow 方法检查行中所有列的内容,然后自动调整行。

// 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);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// 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];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1, 0, 5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

如何在一系列单元格中自动调整列

一列由许多行组成。通过调用重载版本的AutoFitColumn 方法,可以根据列中单元格范围中的内容自动调整列,该方法接受以下参数:

  • 列索引,要自动调整的列的索引。
  • 第一行索引,列的第一行的索引。
  • 最后行索引,列的最后一行的索引。

AutoFitColumn 方法检查列中所有行的内容,然后自动调整列。

// 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);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// 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];
// Auto-fitting the Column of the worksheet
worksheet.AutoFitColumn(4, 4, 6);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

如何为合并单元格自动调整行高

使用Aspose.Cells,甚至可以为已合并的单元格自动调整行高,使用AutoFitterOptions API。AutoFitterOptions 类提供了AutoFitMergedCellsType属性,可用于为合并单元格自动调整行高。 AutoFitMergedCellsType 接受AutoFitMergedCellsType枚举,其中包括以下成员。

  • 无:忽略合并单元格。
  • 第一行:仅展开第一行的高度。
  • 最后一行:仅展开最后一行的高度。
  • 每行:仅展开每行的高度。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiate a new Workbook
Workbook wb = new Workbook();
// Get the first (default) worksheet
Worksheet _worksheet = wb.Worksheets[0];
// Create a range A1:B1
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2);
// Merge the cells
range.Merge();
// Insert value to the merged cell A1
_worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end";
// Create a style object
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle();
// Set wrapping text on
style.IsTextWrapped = true;
// Apply the style to the cell
_worksheet.Cells[0, 0].SetStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine;
// Autofit rows in the sheet(including the merged cells)
_worksheet.AutoFitRows(options);
// Save the Excel file
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx");

重要知识

高级主题