如何绘制表格边框和分割线以模拟数据报表格式?

解读

国内大多数 VB 岗位集中在维护老旧 ERP、MES、财务小工具或快速给 Excel 输出“中国式报表”。面试官问“绘制表格边框和分割线”并不是考你拖一个 DataGridView 就完事,而是看:

  1. 你是否知道老项目里没 .NET 时怎么“手绘”;
  2. 能否在打印或打印预览场景下精确对齐;
  3. 是否理解 GDI+ 与 PrintDocument 事件模型;
  4. 有没有性能意识(别在 Paint 里反复 CreatePen);
  5. 遇到“最后一页底线要不要封口”这类细节需求能否快速调参。

一句话:考察“用 VB 生成可打印、可导出、像素级对齐的报表”基本功。

知识点

  1. GDI+ 绘图三件套:Graphics、Pen、Brush。
  2. 坐标系:DpiX/DpiY、PageUnit、Margins、HardMarginX/Y。
  3. 打印模型:PrintDocument、QueryPageSettings、PrintPage、HasMorePages。
  4. 老版 VB6 与 VBA 的 Line 方法、Printer 对象、Twips→Pixel 换算。
  5. 常用优化:缓存 Pen、测量字符串宽度(Graphics.MeasureString)、抗锯齿开关。
  6. 扩展:把同一绘图逻辑封装为 DrawReport(g As Graphics),既能 PrintDocument,也能 Save 为 EMF/PDF(通过 PdfSharp 或 Office Interop)。

答案

下面给出 VB.NET 版“纯手绘”示例,可直接放进 Visual Studio 2019+ 运行,模拟一张带分组小计的库存报表。核心思路:先算列宽,再画横线、竖线,最后填字。所有尺寸用毫米换算,打印到任何打印机都不变形。

'==== 1. 定义报表元数据 ====
Private ReadOnly Columns As New List(Of String) From {"物料编码", "名称", "库存", "单价", "金额"}
Private ReadOnly ColWidthMM As New List(Of Single) From {25, 45, 15, 15, 20} '毫米
Private Rows As List(Of Object()) '实际数据,面试时可写死 30 条

'==== 2. 打印入口 ====
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    Dim pd As New PrintDocument With {.DocumentName = "库存报表"}
    AddHandler pd.PrintPage, AddressOf PrintPageHandler
    PrintPreviewDialog1.Document = pd
    PrintPreviewDialog1.ShowDialog()
End Sub

'==== 3. 真正绘制 ====
Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)
    Dim g As Graphics = e.Graphics
    g.PageUnit = GraphicsUnit.Millimeter '关键:统一单位
    Dim pen As New Pen(Color.Black, 0.1F) '0.1 mm 细线
    Dim font As New Font("宋体", 9)

    Dim left As Single = e.MarginBounds.Left / 100 * 25.4F '把像素转毫米
    Dim top As Single = e.MarginBounds.Top / 100 * 25.4F
    Dim rowH As Single = 6 '毫米行高
    Dim y As Single = top

    '画表头
    Dim x As Single = left
    For i = 0 To Columns.Count - 1
        g.DrawRectangle(pen, x, y, ColWidthMM(i), rowH)
        g.DrawString(Columns(i), font, Brushes.Black, x + 1, y + 1)
        x += ColWidthMM(i)
    Next
    y += rowH

    '画数据行
    Static currentRow As Integer = 0
    Dim rowsPerPage As Integer = CInt((e.MarginBounds.Bottom / 100 * 25.4F - y) / rowH)
    Dim printed As Integer = 0
    While currentRow < Rows.Count AndAlso printed < rowsPerPage
        x = left
        Dim data() As Object = Rows(currentRow)
        For i = 0 To Columns.Count - 1
            g.DrawRectangle(pen, x, y, ColWidthMM(i), rowH)
            g.DrawString(data(i).ToString, font, Brushes.Black, x + 1, y + 1)
            x += ColWidthMM(i)
        Next
        y += rowH : currentRow += 1 : printed += 1
    End While

    '底线封口
    g.DrawLine(pen, left, y, left + ColWidthMM.Sum, y)

    e.HasMorePages = (currentRow < Rows.Count)
    If Not e.HasMorePages Then currentRow = 0 '复位
End Sub

面试时口头补充要点:

  • 用 GraphicsUnit.Millimeter 可保证换打印机不走样;
  • 0.1 mm 的 Pen 宽度在 300 dpi 以上打印机刚好 1 像素;
  • 如果列很多,先把列宽数组按比例缩放到可打印宽度;
  • 画完后可同一函数 DrawReport(g) 重用到 EMF 文件,供 Word 插图或转 PDF。

拓展思考

  1. 如果需求改为“奇偶行底色不同”,你准备在画横线前还是画横线后填充 Rectangle?为什么?
  2. 当最后一页只有两行,客户要求“空白处也要把竖线画到底”,你如何计算底线位置?
  3. 老系统用 VB6,Printer 对象以 Twips 为单位,如何写通用函数把毫米转 Twips,并兼容美式 Letter 与 A4?
  4. 若报表需要多级分组(仓库→品类→物料),你会在 PrintPage 事件里维护一个“组堆栈”状态机,还是提前把数据拍平成带缩进的行对象?两种方案在内存与速度上如何取舍?
  5. 打印预览没问题,但客户导出 PDF 后发现中文粗体发虚,你会如何排查:是字体嵌入、抗锯齿,还是 PdfSharp 的编码问题?

把以上五点想清,面试官基本会认定你“不仅能画线,还能交付一套可维护的打印引擎”。