以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 莫名其妙的空格 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=41360) |
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/17 16:38:00 -- 莫名其妙的空格
[此贴子已经被作者于2013-10-17 17:38:13编辑过]
|
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/17 20:24:00 -- 有没有高手帮忙啊! |
||||||
-- 作者:有点甜 -- 发布时间: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 |
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/17 22:02:00 -- 甜老师:s3 的字数多于7时代码是有效的,少于7则空格逐渐加大。上面的代码也是无效的。 我折腾了一天,直到刚才,应该是发现了问题的要害所在:4/3这个系数是一个精度不够的近似数造成的,只要换成一个精度更高的近似数应该就可以解决问题! [此贴子已经被作者于2013-10-17 22:10:55编辑过]
|
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/17 22:09:00 -- 其实字数10是一个临界点,10以上,字号开始变化插入空格的代码不起作用,因此不会出错,7以上,误差尚未累积到肉眼可以观察的程度,因此看起来也是正常的。 我的代码如下,测试结果和你的一样: Dim fl As String = ProjectPath & "结果.xls" Dim App As New MSExcel.Application Dim i As Integer For i = defFontSize*2 To 1 Step -1 i = 0
|
||||||
-- 作者:有点甜 -- 发布时间:2013/10/17 22:14:00 -- 没有完美的,特别字符串长度,更是没谱。 |
||||||
-- 作者:有点甜 -- 发布时间: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 |
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/17 23:10:00 -- 我刚才测试7楼的代码,好像还是不行,问题到底出在哪儿呢? |
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/17 23:14:00 -- 分享个小技巧 像这种不断变更值得长度进行测试我把" \'做成一个整体考虑,每次剪切它贴到相应的位置就可以,很快。 |
||||||
-- 作者:东坡一剑 -- 发布时间:2013/10/18 0:28:00 -- 神啊,难道当真无解!!!! 难道是graphics.MeasureString测量不准造成的??? 关键是搞不定居然还没人知道是什么原因,狐坛无人了??? 我不信,继续期待中…………………… |