找回密码
 立即注册

QQ登录

只需一步,快速开始

bingdaoice

中级会员

44

主题

208

帖子

608

积分

中级会员

积分
608
bingdaoice
中级会员   /  发表于:2020-6-19 12:44  /   查看:6558  /  回复: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-19 12:44:25
来自 11#
本帖最后由 Richard.Ma 于 2020-9-15 10:25 编辑

好的,解决了就行,也感谢你分享代码,此代码我贴出设置为最佳答案,供有需要的人参考。
  1. RichTextBox richTextBox = new RichTextBox();
  2. richTextBox.Font = Spread_Excel.Cells[i, j].Font;
  3. richTextBox.Text = dr["CellValue"].ToString();
  4. for (int v = 0; v < richTextBox.Text.Length; v++)
  5. {
  6. if (richTextBox.Text.ToString().Substring(v, 1) == "^" && richTextBox.Text.Length > 0)
  7. {
  8. richTextBox.SelectionStart = v - 1;
  9. richTextBox.SelectionLength = 1;
  10. richTextBox.SelectionCharOffset = 5;
  11. richTextBox.SelectionStart = v;
  12. richTextBox.SelectionLength = 1;
  13. richTextBox.SelectedText = "";
  14. }
  15. else if (richTextBox.Text.ToString().Substring(v, 1) == "&" && richTextBox.Text.Length > 0)
  16. {
  17. richTextBox.SelectionStart = v - 1;
  18. richTextBox.SelectionLength = 1;
  19. richTextBox.SelectionCharOffset = -5;
  20. richTextBox.SelectionStart = v;
  21. richTextBox.SelectionLength = 1;
  22. richTextBox.SelectedText = "";
  23. }
  24. }
  25. RichTextCellType rtct = new RichTextCellType();
  26. Spread_Excel.Cells[i, j].CellType = rtct;
  27. Spread_Excel.Cells[i, j].Value = richTextBox.Rtf;
复制代码


回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-19 14:44:17
2#
从你两张截图上来看其实上标的效果都没有问题,只是第一个字体小,第二个字体大 ,导致了看起来不一样,你可以测试一下

第一个代码我这边可以测试,但是第二个代码我看可能很多的变量对象和你项目中有关,我这里暂时没测试,这个里面你是不是对字体或者字体大小做了设置导致结果不一样
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-19 16:26:16
3#
我做了一个DEMO,通过DEMO,测试可以表现出上面所说的问题。不知道要怎么解决。

image.png590833434.png

spread_上下标.rar

12.03 MB, 下载次数: 141

回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-19 19:01:42
4#
好的,我这边验证后给你回复
回复 使用道具 举报
bingdaoice
中级会员   /  发表于:2020-6-22 11:25:20
5#
你好,请问有验证结果了吗?
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-22 13:26:26
6#
从你提供的Demo发现字号不一样,1里面是9号宋体,但是2里面是11.5号宋体,字号不一样,展示的效果也就会有差异。
回复 使用道具 举报
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;
}

回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-6-22 20:31:17
8#
需要重置为原始的文字位置时应该设置为0
richTextBox1.SelectionCharOffset = 0;
回复 使用道具 举报
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
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部