怎样给Excel划格子?我们使用Excel做报表,由于行数是动态的,所以要动态划格子,不知有哪位大侠会?
给一段程序代码了,谢谢!

解决方案 »

  1.   

    Private Sub SetLine(ByVal strStart As String, ByVal strEnd As String, ByVal xlsWS As Excel.Worksheet)
        Dim i As Integer
        
        With xlsWS.Range(strStart & ":" & strEnd)
            On Error Resume Next
            For i = 7 To 12
                .Borders(i).LineStyle = xlContinuous
            Next
    '        .Borders(xlEdgeLeft).LineStyle = xlContinuous
    '        .Borders(xlEdgeTop).LineStyle = xlContinuous
    '        .Borders(xlEdgeBottom).LineStyle = xlContinuous
    '        .Borders(xlEdgeRight).LineStyle = xlContinuous
    '        .Borders(xlInsideVertical).LineStyle = xlContinuous
    '        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        End With
        End Sub
      

  2.   

    Dim appExcel As New Excel.Application
    dim bsRange as range
    ... ... ...
    set bsRange =appExcel.ActiveSheet.Range("XXX") 'XXX为第一个有内容的单元格
    with bsRange.CurrentRegion
          .Borders(xlEdgeBottom).Weight = xlContinuous
          .Borders(xlEdgeTop).Weight = xlContinuous
          .Borders(xlEdgeLeft).Weight = xlContinuous
          .Borders(xlEdgeRight).Weight = xlContinuous
          '内部水平划线
          If .Rows.Count > 1 Then .Borders(xlInsideHorizontal).Weight = xlContinuous 
          If .Columns.Count > 1 Then .Borders(xlInsideVertical).Weight = xlContinuous 
    End With