以下函数是Excel生成的vb宏,用来给表格的边框划线
Sub Macro1()    Range("A2:M8").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End Sub-------------------------------------我在vb中调用先导出Excel在进行格式设置
程序如下:
Dim i As Integer
Dim j As IntegerDim xlApp As Excel.Application
'Set xlApp = New Excel.ApplicationDim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim r As Excel.Range
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Select
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).RowHeight = 40
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Merge (missing)
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Value = "档案资料报表"
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).HorizontalAlignment = xlCenter
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).VerticalAlignment = xlCenter
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Font.Name = "黑体"
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Font.Size = 16For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(2, j + 1) = DataGrid1.Columns.Item(j).Caption
xlSheet.Cells(2, j + 1).HorizontalAlignment = xlCenter
Next j'xlSheet.Cells(10, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 1 To adoPrimaryRS.RecordCount - 1
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text
If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = "'" + DataGrid1.Text
End If
Next j
Next ixlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Select
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).HorizontalAlignment = xlHAlignLeft
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).VerticalAlignment = xlCenter
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).WrapText = True
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Font.Name = "宋体"
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Font.Size = 9    xlApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    xlApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With xlApp.Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End WithWith xlApp.ActiveSheet.PageSetup
    .CenterFooter = "第 &P 页,共 &N 页"
    .Orientation = xlLandscape
    .PrintGridlines = False
End With
结果别的都没问题,就是设置表格线的代码不起作用,请高手帮忙看看~

解决方案 »

  1.   

    我测试了一下,没问题,可以画线你把程序中On Error Resume Next 这句注释掉,然后运行看有没有错误
      

  2.   

    12.指定边框线宽度(Borders参数如下)  ole.ActiveSheet.Range(″b3:d3″).Borders(2).Weight=3  13.设置四个边框线条的类型  eole.ActiveSheet.Range(″b3:d3″).Borders(2).LineStyle=1  (其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle值:1与7-细实、2-细虚、4-点虚、9-双细实线)
      

  3.   

    照cuilei197979(风)的方法改成了数字,结果没问题了(估计是我笔误),高兴~