没有等宽字体的字符串对齐

时间:2017-06-09 15:53:39

标签: c# excel string vsto

我希望在Excel中的右键单击菜单中跨多行的对齐字符串。右键单击菜单添加如下:

 CommandBar contextMenu = Globals.ThisWorkbook.Application.CommandBars["List Range Popup"];
 CommandBarPopup subMenu = (CommandBarPopup)contextMenu.Controls.Add(Type: MsoControlType.msoControlPopup, Before: 1, Temporary: true);
 subMenu.Caption = "Assumptions Drill Down";

然后我迭代数据库结果并添加到前一行中的subMenu 当前结果如下(使用字符串填充):enter image description here

由于字符空间不均匀(非等宽字体),因此列不对齐。我试图做类似下面的事情来获取字符串的像素大小,然后转换回所需的字符串填充整数,但我不能让它工作:

    private static double GetStringSize(string stringText)
    {
        Bitmap b = new Bitmap(1, 1);
        Graphics g = Graphics.FromImage(b);
        SizeF size = g.MeasureString(stringText,
            new System.Drawing.Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point));
        return size.Width;
    }
  1. 有没有办法将右键单击字体设置为等宽字体(我不相信)
  2. 如果我不能做(1.)我怎样才能确保每列中的间距是均匀的 - 例如,均匀对齐'iiiii'与'QQQQQ'的用户 ?

0 个答案:

没有答案