この簡単なチュートリアルでは、手順のリストと実行可能なサンプルコードを使用して、** C#を使用してExcelでセルを回転する方法**について説明します。回転角を設定するだけでなく、他のスタイルのカスタマイズも可能です。 ** C#**を使用してExcelでテキストの向きを変更した後、出力ファイルをXLSX、XLS、PDFなどとして保存できます。
C#を使用してExcelでセルを回転する手順
- NuGetパッケージマネージャーを使用してAspose.Cells for .NETを追加するようにプロジェクトを構成します
- Workbookを作成または開き、ターゲットワークシートにアクセスします
- ターゲットセルへのアクセスを取得し、空の場合は値を入力します
- セルStyleオブジェクトを取得し、RotationAngleを目的の値に設定します
- スタイルをセルに戻します
- テキストを回転させたディスクに変更したブックを保存します
上記の手順では、構成の詳細とタスクを実行するための段階的な手順を使用して、* 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として保存する方法の記事を参照してください。