第一种方法 :代码控制上下标
Spread_Excel.Cells[i, j].ResetCellType();
RichTextBox rtbsup = new RichTextBox();
rtbsup.SelectedText = "100m3";
rtbsup.SelectionStart = 4;
rtbsup.SelectionLength = 1;
rtbsup.SelectionCharOffset = 5; //位移的像素,正数为上移,负数为下移
RichTextCellType rtct = new RichTextCellType();
Spread_Excel.Cells[i, j].CellType = rtct;
Spread_Excel.Cells[i, j].Value = rtbsup.Rtf;
得到的结果是这样的:
方法二是手动设置富文本,再右键设置上标,代码如下:
private void btnRichTextBox_Click(object sender, EventArgs e)
{
FarPoint.Win.Spread.FpSpread Spread_Excel = Spread_RecdCertif;
FarPoint.Win.Spread.SheetView sv = Spread_Excel.ActiveSheet;
FarPoint.Win.Spread.CellType.RichTextCellType rtb = new FarPoint.Win.Spread.CellType.RichTextCellType();
rtb.Multiline = true;
rtb.WordWrap = true;
string text = Spread_Excel.ActiveSheet.GetText(startRow, startCol).ToString();
sv.Cells[startRow, startCol].CellType = rtb;
sv.Cells[startRow, startCol].Value = text;
Spread_Excel.EditModeOn += Spread_Excel_EditModeOn;
}
RichTextBox editor;
private void Spread_Excel_EditModeOn(object sender, EventArgs e)
{
FarPoint.Win.Spread.FpSpread Spread_Excel = (FarPoint.Win.Spread.FpSpread)sender;
editor = Spread_Excel.EditingControl as RichTextBox;
if (editor != null)
{
ContextMenu cm = new ContextMenu();
MenuItem itm = new MenuItem();
itm.Text = "斜体";
itm.Click += new EventHandler(itm_Click);
cm.MenuItems.Add(itm);
itm = new MenuItem();
itm.Text = "粗体";
itm.Click += new EventHandler(itm_Click);
cm.MenuItems.Add(itm);
itm = new MenuItem();
itm.Text = "上标";
itm.Click += new EventHandler(itm_Click);
cm.MenuItems.Add(itm);
itm = new MenuItem();
itm.Text = "下标";
itm.Click += new EventHandler(itm_Click);
cm.MenuItems.Add(itm);
editor.ContextMenu = cm;
}
}
void itm_Click(object sender, EventArgs e)
{
if (sender.ToString().Contains("斜体"))
{
Font font = new System.Drawing.Font(editor.SelectionFont, FontStyle.Italic);
editor.SelectionFont = font;
}
else if (sender.ToString().Contains("粗体"))
{
Font font = new System.Drawing.Font(editor.SelectionFont, FontStyle.Bold);
editor.SelectionFont = font;
}
else if (sender.ToString().Contains("上标"))
editor.SelectionCharOffset = 5; //位移的像素,正数为上移,负数为下移
else if (sender.ToString().Contains("下标"))
editor.SelectionCharOffset = -5;
}
得到的结果是这样的:
想请问如何通过代码控制得到方法二的效果。(也就是通过方法一得到方法二的结果)。
谢谢。
|