找回密码
 立即注册

QQ登录

只需一步,快速开始

bingdaoice

中级会员

44

主题

208

帖子

608

积分

中级会员

积分
608
bingdaoice
中级会员   /  发表于:2020-6-19 12:44  /   查看:7173  /  回复:14
1金币
第一种方法    :代码控制上下标
                                            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;
得到的结果是这样的: image.png505271911.png



方法二是手动设置富文本,再右键设置上标,代码如下:
        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;
        }

得到的结果是这样的: image.png260642089.png

想请问如何通过代码控制得到方法二的效果。(也就是通过方法一得到方法二的结果)。
谢谢。

14 个回复

正序浏览
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-23 15:36:02
15#
不客气
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-23 14:12:17
14#
好的,感谢Richard.Ma!
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-23 13:57:36
13#
你好,没有办法设置自动缩小
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-23 12:44:20
12#
再请问一下版主。RichTextCellType可以设置自动缩小字体吗?
比如: image.png50877379.png
这里我设置了自动换行,但字符还是显示不完整,我想自动缩小可以么?                                
RichTextCellType rtct = new RichTextCellType();
                                            rtct.Multiline = true;
                                            rtct.WordWrap = true;
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-23 10:41:21
10#
谢谢版主的解答,此问题终于解决了。用了以下方法, 仅供有需要的人参考。
RichTextBox richTextBox = new RichTextBox();
richTextBox.Font = Spread_Excel.Cells[i, j].Font;
richTextBox.Text = dr["CellValue"].ToString();
for (int v = 0; v < richTextBox.Text.Length; v++)
{
    if (richTextBox.Text.ToString().Substring(v, 1) == "^" && richTextBox.Text.Length > 0)
    {                                                   
        richTextBox.SelectionStart = v - 1;
        richTextBox.SelectionLength = 1;
        richTextBox.SelectionCharOffset = 5;
        richTextBox.SelectionStart = v;
        richTextBox.SelectionLength = 1;
        richTextBox.SelectedText = "";
    }
    else if (richTextBox.Text.ToString().Substring(v, 1) == "&" && richTextBox.Text.Length > 0)
    {
        richTextBox.SelectionStart = v - 1;
        richTextBox.SelectionLength = 1;
        richTextBox.SelectionCharOffset = -5;
        richTextBox.SelectionStart = v;
        richTextBox.SelectionLength = 1;
        richTextBox.SelectedText = "";
    }
}
    RichTextCellType rtct = new RichTextCellType();
    Spread_Excel.Cells[i, j].CellType = rtct;
    Spread_Excel.Cells[i, j].Value = richTextBox.Rtf;


评分

参与人数 1金币 +500 收起 理由
Richard.Ma + 500

查看全部评分

回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-23 09:50:58
9#
我在 设置rtbsup.SelectedText 值的时候加了一个判断,但是所的有上下标又没有了。这个问题都把我整魔怔了。都不知道要怎么弄了。谢谢版主。
if (rtbsup.SelectionCharOffset != 0)
        rtbsup.SelectionCharOffset = 0;
rtbsup.SelectedText = rtbsup.SelectedText + dr["CellValue"].ToString().Substring(v, 1);

image.png586180724.png
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-22 20:31:17
8#
需要重置为原始的文字位置时应该设置为0
richTextBox1.SelectionCharOffset = 0;
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-22 16:53:51
7#
本帖最后由 bingdaoice 于 2020-6-22 17:03 编辑

通过设置两个专用字符来表示上下标,但有个问题就是在设置了上下标之后的全是上下标了。不能回来本来的格式的请问怎么处理。谢谢!
image.png820357055.png                                      if (dr["CellValue"].ToString().Contains("^")|| dr["CellValue"].ToString().Contains("&")){
RichTextBox rtbsup = new RichTextBox();
rtbsup.Font = Spread_Excel.Cells[i, j].Font;
for (int v = 0; v < dr["CellValue"].ToString().Length; v++)
{
    if (dr["CellValue"].ToString().Substring(v, 1) == "^" && rtbsup.Text.Length > 0)
    {
        rtbsup.SelectionStart = rtbsup.Text.Length - 1;
        rtbsup.SelectionLength = 1;
        rtbsup.SelectionCharOffset = 5;
        continue;
    }
    else if (dr["CellValue"].ToString().Substring(v, 1) == "&" && rtbsup.Text.Length > 0)
    {
        rtbsup.SelectionStart = rtbsup.Text.Length - 1;
        rtbsup.SelectionLength = 1;
        rtbsup.SelectionCharOffset = -5;
        continue;
    }
    rtbsup.SelectedText = rtbsup.SelectedText + dr["CellValue"].ToString().Substring(v, 1);
}
RichTextCellType rtct = new RichTextCellType();
Spread_Excel.Cells[i, j].CellType = rtct;
Spread_Excel.Cells[i, j].Value = rtbsup.Rtf;
}

回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-22 13:26:26
6#
从你提供的Demo发现字号不一样,1里面是9号宋体,但是2里面是11.5号宋体,字号不一样,展示的效果也就会有差异。
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-22 11:25:20
5#
你好,请问有验证结果了吗?
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部