自动调整行和列
Contents
[
Hide
]
Microsoft Excel 允许用户根据其内容自动调整单元格的宽度和高度。此功能也可通过 Aspose.Cells 获得,因此开发人员可以在运行时自动调整单元格的尺寸。
汽车配件
Aspose.Cells提供了工作簿表示 Microsoft Excel 文件的类。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了广泛的属性和方法来管理工作表。本文着眼于使用工作表自动调整行或列的类。
自动调整行 - 简单
自动调整行的宽度和高度的最直接的方法是调用工作表班级自动调整行方法。这自动调整行方法将(要调整大小的行的)行索引作为参数。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
Cells 范围内的 AutoFit 行
一行由许多列组成。 Aspose.Cells 允许开发人员通过调用自动调整行方法。它采用以下参数:
- 行索引,即将被自动调整的行的索引。
- 第一列索引,该行第一列的索引。
- 最后一列索引,该行最后一列的索引。
这自动调整行方法检查行中所有列的内容,然后自动调整行。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
Cells 范围内的 AutoFit 列
一列由许多行组成。通过调用重载版本,可以根据列中单元格范围内的内容自动调整列自动调整列采用以下参数的方法:
- 列索引即将被自动拟合的列的索引。
- 第一行索引,列第一行的索引。
- 最后一行索引,列最后一行的索引。
这自动调整列方法检查列中所有行的内容,然后自动调整列。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
合并的 AutoFit 行 Cells
使用 Aspose.Cells,即使对于已使用合并的单元格,也可以自动调整行AutoFitter选项 API. AutoFitter选项类提供AutoFitMergedCellsType可用于自动调整合并单元格行的属性。AutoFitMergedCellsType接受AutoFitMergedCellsType具有以下成员的可枚举。
- 无:忽略合并的单元格。
- FirstLine:只扩展第一行的高度。
- LastLine:只扩展最后一行的高度。
- eachLine:只扩展每一行的高度。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
您也可以尝试使用的重载版本自动调整行 & 自动调整列接受一系列行/列的方法和一个实例AutoFitter选项根据您的需要自动调整选定的行/列AutoFitter选项因此。
上述方法的签名如下:
- AutoFitRows(int startRow,int endRow,AutoFitter选项选项)
- AutoFitColumns(int firstColumn,int lastColumn,AutoFitter选项选项)
重要须知
如果合并单元格,则不会应用 AutoFit 方法,这与 Microsoft Excel 中的行为相同。您可以使用自动过滤器 API 来解决这个问题。此外,如果单元格中的文本被换行,则自动调整列方法也不会被应用。您需要知道的另一件事是自动调整方法很费时间。因此,您应该尽可能少地调用这些方法,以确保您的应用程序的效率。