本文介绍如何使用 C# **自动调整 Excel。它讨论了 IDE 配置、一系列步骤以及使用 C# **自动适应 Excel 的代码片段。我们还将学习自动调整 Excel 工作表中的特定行或列。
使用 C# 在 Excel 中自动调整行和列的步骤
- 准备 IDE 以使用 Aspose.Cells for .NET 自动调整行和列
- 使用 Workbook 类加载输入文件
- 使用 worksheet 类对象及其索引或名称访问任何工作表
- 自动调整行和列
- 使用 Save 方法导出输出工作簿
这些步骤总结了如何使用 C# 在 Excel 中自动调整单元格大小。您需要使用从零开始的索引或指定工作表名称来访问特定工作表。随后,在导出输出 Excel 文件之前自动调整行和列。
使用 C# 在 Excel 中自动调整行和列的代码
using System; | |
using Aspose.Cells; | |
class Program | |
{ | |
static void Main(string[] args) // Autofit Excel file using C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Load the Excel file | |
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook("autofit.xlsx"); | |
// Get reference of desired worksheet | |
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto fit the columns and rows | |
worksheet.AutoFitRows(); worksheet.AutoFitColumns(); | |
// Save workbook | |
workbook.Save("autofit-excel.xlsx"); | |
Console.WriteLine("Done"); | |
} | |
} |
此示例代码展示了使用 C#* 自动调整 Excel 列的简单过程。同样,它会调整行高的大小以调整单元格内的单元格内容。但是,您还可以使用 AutoFitRow(index)、AutoFitColumn(index) 等重载方法,其中行索引和列索引是从零开始的数字,以根据您的要求处理特定的行或列。
本文详细阐述了使用 C#* 自动调整行。要在单元格级别上进行文本换行,请阅读有关 如何使用 C# 在 Excel 中自动换行 的文章。