如何使用 C# 在 Excel 中旋转单元格

这个简短的教程解释了如何在步骤列表和可运行示例代码的帮助下使用 C# 在 Excel 中旋转单元格。它提供的选项不仅可以设置旋转角度,还可以自定义其他样式。使用 C#** 在 Excel 中更改**文本方向后,输出文件可以保存为 XLSXXLS、PDF 等。

使用 C# 在 Excel 中旋转单元格的步骤

  1. 使用 NuGet 包管理器配置项目以添加 Aspose.Cells for .NET
  2. 创建或打开 Workbook 并访问其目标工作表
  3. 访问目标单元格并在为空时输入一些值
  4. 获取单元格 Style 对象并将 RotationAngle 设置为所需的值
  5. 将样式设置回单元格
  6. 将修改后的工作簿保存在其中包含旋转文本的磁盘上

上述步骤描述了如何使用 C# 在 Excel 中倾斜单元格,并借助配置详细信息和逐步完成任务的过程。它提供了设置旋转角度的选项,也可以自定义其他样式。您不需要任何其他第三方工具来完成该过程。

使用 C# 在 Excel 中旋转单元格的代码

using Aspose.Cells;
namespace HowToRotateACellInExcelUsingCSharp
{
class Program
{
static void Main(string[] args) // Main function to rotate a cell in Excel using CSharp
{
// Instantiate a Cell License to avoid watermark in the output XLSX after
// rotating text in the cell
Aspose.Cells.License licForCells = new Aspose.Cells.License();
licForCells.SetLicense("Aspose.Cells.lic");
// Create a new empty workbook for testing cell rotation
Workbook wbForRotatedText = new Workbook();
// Get access to the first worksheet where text will be placed
Worksheet wsForRotatedText = wbForRotatedText.Worksheets[0];
// Get Cells collection from the target worksheet
Cells cellsForRotatedText = wsForRotatedText.Cells;
// Get access to the target cell for setting the sample text
Cell cellForRotatedText = cellsForRotatedText["D5"];
// Set some text value in the target cell
cellForRotatedText.PutValue("Text to be rotated");
// Get the style object from the selected cell
Style objStyle = cellForRotatedText.GetStyle();
// Set the rotation angle of the text in the style object
objStyle.RotationAngle = 60;
// Set the style back to the target cell
cellForRotatedText.SetStyle(objStyle);
// Save the workbook containing the rotated text in it
wbForRotatedText.Save("RotateText_test.xlsx");
System.Console.WriteLine("Done");
}
}
}

此代码演示了通过使用 Style 类属性 RotationAngle 使用 C#* 在 Excel 中配置 *文本方向的过程。您可以设置任何样式属性,例如字体、前景色、背景色、边框、笔样式等。

本教程教我们如何使用 C# 旋转 Excel 单元格。如果您想将此输出文件保存为 PDF,请参阅 如何使用 C# 将 Excel 文件保存为 PDF 上的文章。

 简体中文