您的位置:首页 > 编程语言 > ASP

asp中从数据库导出EXCEL时,EXCEL宏应用的边框设置

2009-09-15 16:44 615 查看
Set xlApp = CreateObject("Excel.Application")

xlApp.DisplayAlerts = false               '不显示警告
   'xlApp.Application   = false               '不显示界面
   xlapp.Workbooks.add
   set objExcelBook   = xlapp.ActiveWorkBook
   set objExcelSheets = objExcelBook.Worksheets
   set xlSheet = objExcelBook.Sheets(1)

 

 

EXCEL中设置边框,录制的宏

Sub Macro1()
'
' Macro1 Macro
' 宏由 YlmF 录制,时间: 2009-9-15 test
'
' 快捷键: Ctrl+q
'
    Range("A3:G32").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

运行会出现“类型不匹配: 'range'”或者“selection错误”之类的错误,所以在vb中用msgbox查看了下述的值

'xlDiagonalDown=5
   'xlDiagonalUp=6
   'xlEdgeLeft=7
   'xlEdgeTop=8
   'xlEdgeBottom=9
   'xlEdgeRight=10
   'xlInsideVertical=11
   'xlInsideHorizontal=12

   'xlNone=-4142

  改为

xlsheet.Range("A3","G"&startrow-1&"").Borders(xlDiagonalDown).LineStyle = xlNone

 运行出现“未知的运行时错误”的错误

改为

xlsheet.Range("A3","G"&startrow-1&"").Borders("5").LineStyle = "-4142"

运行出现“不能设置类 Border 的 LineStyle 属性”的错误

 

最后正确运行代码如下:
    xlsheet.Range("A3","G"&startrow-1&"").Borders("5").LineStyle = xlNone
      xlsheet.Range("A3","G"&startrow-1&"").Borders("6").LineStyle = xlNone
    With xlsheet.Range("A3","G"&startrow-1&"").Borders("7")
        .LineStyle = xlContinuous
        .Weight = "2" 'xlThin
        .ColorIndex = xlAutomatic
    End With
    With xlsheet.Range("A3","G"&startrow-1&"").Borders("8")
        .LineStyle = xlContinuous
        .Weight = "2"  'xlThin
        .ColorIndex = xlAutomatic
    End With
    With xlsheet.Range("A3","G"&startrow-1&"").Borders("9")
        .LineStyle = xlContinuous
        .Weight = "2" 'xlThin
        .ColorIndex = xlAutomatic
    End With
    With xlsheet.Range("A3","G"&startrow-1&"").Borders("10")
        .LineStyle = xlContinuous
        .Weight = "2"  'xlThin
        .ColorIndex = xlAutomatic
    End With
    With xlsheet.Range("A3","G"&startrow-1&"").Borders("11")
        .LineStyle = xlContinuous
        .Weight = "2"  'xlThin
        .ColorIndex = xlAutomatic
    End With
    With xlsheet.Range("A3","G"&startrow-1&"").Borders("12")
        .LineStyle = xlContinuous
        .Weight = "2"  'xlThin
        .ColorIndex = xlAutomatic
    End With

 

 

代码正确了,但是关于录制的宏在asp中应用还是有很多疑问,比如上例中 .Weight = "2" ,如果是.Weight = xlThin就出现“不能设置类 Border 的 Weight属性”的错误,这个和

xlsheet.Range("A3","G"&startrow-1&"").Borders("5").LineStyle = "-4142" 不一致

贴出来,共勉共勉
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel asp 数据库 border vb