セルの特定位置の文字のみフォントカラーやスタイルを変更したい
対象製品
SPREAD for Windows Forms 8.0J
詳細
セルの特定位置の文字のみフォントカラーやスタイルを変更するには、リッチテキスト型セル(RichTextCellTypeクラス)を使用します。リッチテキスト型セルのValueプロパティにRTFを指定しますが、RichTextBoxコントロールを使用してRTFを作成することが可能です。なお、リッチテキスト型セルの機能は基本的に標準の「リッチテキストボックスコントロール」と同等となります。
◎サンプルコード(VB)
◎サンプルコード(C#)
◎サンプルコード(VB)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' リッチテキスト文字列の作成
Dim rtfText As String = String.Empty
Using temp As New RichTextBox()
temp.Text = "ABC" + vbCrLf + "DEF" + vbCrLf + "GHI"
temp.SelectionStart = 0
temp.SelectionLength = 3
temp.SelectionColor = Color.Red
temp.SelectionFont = New Font("MS UI Gothic", 9)
temp.SelectionStart = 4
temp.SelectionLength = 3
temp.SelectionColor = Color.Blue
temp.SelectionFont = New Font("MS UI Gothic", 12, FontStyle.Bold Or FontStyle.Italic)
temp.SelectionStart = 8
temp.SelectionLength = 3
temp.SelectionColor = Color.Green
temp.SelectionFont = New Font("MS UI Gothic", 15, FontStyle.Underline)
temp.SelectionStart = 0
temp.SelectionLength = 12
temp.SelectionAlignment = HorizontalAlignment.Center
rtfText = temp.Rtf
End Using
'リッチテキスト型セルの定義
Dim rich As New FarPoint.Win.Spread.CellType.RichTextCellType()
rich.Multiline = True
FpSpread1.ActiveSheet.Cells(0, 0).CellType = rich
FpSpread1.ActiveSheet.Cells(0, 0).Value = rtfText
FpSpread1.ActiveSheet.SetRowHeight(0, 80)
End Sub
' リッチテキスト文字列の作成
Dim rtfText As String = String.Empty
Using temp As New RichTextBox()
temp.Text = "ABC" + vbCrLf + "DEF" + vbCrLf + "GHI"
temp.SelectionStart = 0
temp.SelectionLength = 3
temp.SelectionColor = Color.Red
temp.SelectionFont = New Font("MS UI Gothic", 9)
temp.SelectionStart = 4
temp.SelectionLength = 3
temp.SelectionColor = Color.Blue
temp.SelectionFont = New Font("MS UI Gothic", 12, FontStyle.Bold Or FontStyle.Italic)
temp.SelectionStart = 8
temp.SelectionLength = 3
temp.SelectionColor = Color.Green
temp.SelectionFont = New Font("MS UI Gothic", 15, FontStyle.Underline)
temp.SelectionStart = 0
temp.SelectionLength = 12
temp.SelectionAlignment = HorizontalAlignment.Center
rtfText = temp.Rtf
End Using
'リッチテキスト型セルの定義
Dim rich As New FarPoint.Win.Spread.CellType.RichTextCellType()
rich.Multiline = True
FpSpread1.ActiveSheet.Cells(0, 0).CellType = rich
FpSpread1.ActiveSheet.Cells(0, 0).Value = rtfText
FpSpread1.ActiveSheet.SetRowHeight(0, 80)
End Sub
◎サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
// リッチテキスト文字列の作成
String rtfText = null;
using (RichTextBox temp = new RichTextBox())
{
temp.Text = "ABC" + "¥r¥n" + "DEF" + "¥r¥n" + "GHI";
temp.SelectionStart = 0;
temp.SelectionLength = 3;
temp.SelectionColor = Color.Red;
temp.SelectionFont = new Font("MS UI Gothic", 9);
temp.SelectionStart = 4;
temp.SelectionLength = 3;
temp.SelectionColor = Color.Blue;
temp.SelectionFont = new Font("MS UI Gothic", 12, FontStyle.Bold | FontStyle.Italic);
temp.SelectionStart = 8;
temp.SelectionLength = 3;
temp.SelectionColor = Color.Green;
temp.SelectionFont = new Font("MS UI Gothic", 15, FontStyle.Underline);
temp.SelectionStart = 0;
temp.SelectionLength = 12;
temp.SelectionAlignment = HorizontalAlignment.Center;
rtfText = temp.Rtf;
}
// リッチテキスト型セルの定義
FarPoint.Win.Spread.CellType.RichTextCellType rich = new FarPoint.Win.Spread.CellType.RichTextCellType();
rich.Multiline = true;
fpSpread1.ActiveSheet.Cells[0, 0].CellType = rich;
fpSpread1.ActiveSheet.Cells[0, 0].Value = rtfText;
fpSpread1.ActiveSheet.SetRowHeight(0, 80);
}
{
// リッチテキスト文字列の作成
String rtfText = null;
using (RichTextBox temp = new RichTextBox())
{
temp.Text = "ABC" + "¥r¥n" + "DEF" + "¥r¥n" + "GHI";
temp.SelectionStart = 0;
temp.SelectionLength = 3;
temp.SelectionColor = Color.Red;
temp.SelectionFont = new Font("MS UI Gothic", 9);
temp.SelectionStart = 4;
temp.SelectionLength = 3;
temp.SelectionColor = Color.Blue;
temp.SelectionFont = new Font("MS UI Gothic", 12, FontStyle.Bold | FontStyle.Italic);
temp.SelectionStart = 8;
temp.SelectionLength = 3;
temp.SelectionColor = Color.Green;
temp.SelectionFont = new Font("MS UI Gothic", 15, FontStyle.Underline);
temp.SelectionStart = 0;
temp.SelectionLength = 12;
temp.SelectionAlignment = HorizontalAlignment.Center;
rtfText = temp.Rtf;
}
// リッチテキスト型セルの定義
FarPoint.Win.Spread.CellType.RichTextCellType rich = new FarPoint.Win.Spread.CellType.RichTextCellType();
rich.Multiline = true;
fpSpread1.ActiveSheet.Cells[0, 0].CellType = rich;
fpSpread1.ActiveSheet.Cells[0, 0].Value = rtfText;
fpSpread1.ActiveSheet.SetRowHeight(0, 80);
}
キーワード
セル型