Foxtable(狐表)用户栏目专家坐堂 → 莫名其妙的空格


  共有4879人关注过本帖树形打印复制链接

主题:莫名其妙的空格

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2013/10/17 21:05:00 [显示全部帖子]

 看了下,如下代码,测试有效。

Dim fl As String = ProjectPath & "结果.xls"
Dim s0,s1,s2,s3 As String
Dim chr As MSExcel.Characters
s0 = " "
s1 = "品牌:中国牌"
s2 = "品名:"
s3 = "一二三四五六七八九十一二三四五快快快快六七八九十"

Dim App As New MSExcel.Application
App.displayAlerts = False
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim Rg As MSExcel.Range = Ws.Range("A3")

Rg.value = s1 & s2 & s3.trim()
chr= Rg.Characters(s1.length + s2.length + 1,s3.length)

Dim i As Integer
Dim defFontSize = 16
Dim Range = ws.Range("A3")
Dim width = Rg.width * 4 / 3
Dim graphics As Graphics = Forms("窗口2").baseform.CreateGraphics
Dim sizeF  = graphics.MeasureString(s1 & s2, new Font("宋体", defFontSize))
width = width - sizeF.width

For i  = defFontSize*2 To 1 Step -1
    sizeF = graphics.MeasureString(s3, new Font("宋体", i/2))
    If sizeF.width <= width Then
        chr= Rg.Characters(s1.length + s2.length + 1,s3.length)
        chr.font.size = i/2
        Dim sizeF0 = graphics.MeasureString(s0, new Font("宋体", defFontSize))
        Dim sizeF1 = graphics.MeasureString(s1, new Font("宋体", defFontSize))
        Dim sizeF2 = graphics.MeasureString(s2, new Font("宋体", defFontSize))
        Dim j As Integer = 1
        Do While sizeF0.width*(j-3) + sizeF1.width + sizeF2.width + sizeF.width <= rg.width * 4 / 3
            j += 1
        Loop
        j -= 1
        rg.value = s1.PadRight(s1.Length+j) & s2 & s3
        chr= Rg.Characters(s1.length + s2.length + j + 1,s3.length)
        chr.font.size = i/2
        Exit For
    End If
Next
wb.save
App.visible = True

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2013/10/17 22:14:00 [显示全部帖子]

 没有完美的,特别字符串长度,更是没谱。

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2013/10/17 22:44:00 [显示全部帖子]

  再试了一下,是空格宽度的问题,应该是11的。

Dim fl As String = ProjectPath & "结果.xls"
Dim s0,s1,s2,s3 As String
Dim chr As MSExcel.Characters
s0 = " "
s1 = "品牌:中国牌"
s2 = "品名:"
s3 = "一七八九八九八九八九八九八九八九八九八九八九八九八九八九八九八九八九八九八九十"

Dim App As New MSExcel.Application
App.displayAlerts = False
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim Rg As MSExcel.Range = Ws.Range("A4")

Rg.value = s1 & s2 & s3.trim()
chr= Rg.Characters(s1.length + s2.length + 1,s3.length)

Dim i As Integer
Dim defFontSize = 16
Dim Range = ws.Range("A3")
Dim width = Rg.width * 4 / 3
Dim graphics As Graphics = Forms("窗口2").baseform.CreateGraphics
Dim sizeF  = graphics.MeasureString(s1 & s2, new Font("宋体", defFontSize))
width = width - sizeF.width

For i  = defFontSize*2 To 1 Step -1
    sizeF = graphics.MeasureString(s3, new Font("宋体", i/2))
    If sizeF.width <= width Then
        chr= Rg.Characters(s1.length + s2.length + 1,s3.length)
        chr.font.size = i/2
        Dim sizeF0 = graphics.MeasureString(s0, new Font("宋体", defFontSize))
        Dim sizeF1 = graphics.MeasureString(s1, new Font("宋体", defFontSize))
        Dim sizeF2 = graphics.MeasureString(s2, new Font("宋体", defFontSize))
        Dim j As Integer = 1
        Do While 11*j-3*sizeF0.width + sizeF1.width + sizeF2.width + sizeF.width <= rg.width * 4 / 3
            j += 1
        Loop
        rg.value = s1.PadRight(s1.Length+j) & s2 & s3
        chr= Rg.Characters(s1.length + s2.length + j + 1,s3.length)
        chr.font.size = i/2
        Exit For
    End If
Next
wb.save
App.visible = True

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2013/10/18 19:42:00 [显示全部帖子]

 无解,别期待了,空格的宽度,是不精确的,而且16像素的空格有1个的误差是正常的。自己想办法精确。

 回到顶部