當前位置:編程學習大全網 - 編程語言 - VB要輸出壹個完整的表格(WORD或EXCEL),並在表格內輸出相應數據

VB要輸出壹個完整的表格(WORD或EXCEL),並在表格內輸出相應數據

給妳壹個例子

Option Explicit

'Private xlApp As Excel.Application

'Private xlBook As Excel.Workbook

'Private xlSheet As Excel.Worksheet

Private xlApp As Object

Private xlBook As Object

Private xlSheet As Object

Private cellValue As String

Public strError As String

Public ExportOK As Boolean

Private Sub Class_Initialize()

ExportOK = False

On Error GoTo errHandle:

' Set xlApp = CreateObject("Excel.Applaction")

Set xlApp = New Excel.Application

xlApp.Visible = False

On Error GoTo errHandle:

Set xlBook = xlApp.Workbooks.Add

Set xlSheet = xlBook.Worksheets(1)

If Val(xlApp.Application.Version) >= 8 Then

Set xlSheet = xlApp.ActiveSheet

Else

Set xlSheet = xlApp

End If

Exit Sub

errHandle:

Err.Raise 100001, , "建立Excel對象時發生錯誤:" & Err.Description & vbCr & _

"請確保您正確了安裝了Excel軟件!"

End Sub

Public Property Get TextMatrix(Row As Integer, Col As Integer) As Variant

TextMatrix = xlSheet.Cells(Row, Col)

End Property

Public Property Let TextMatrix(Row As Integer, Col As Integer, Value As Variant)

xlSheet.Cells(Row, Col) = Value

End Property

'合並單元格

Public Sub MergeCell(bRow As Integer, bCol As Integer, eRow As Integer, eCol As Integer)

xlSheet.Range(GetExcelCell(bRow, bCol) & ":" & GetExcelCell(eRow, eCol)).Select

With xlApp.Selection

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

.WrapText = True

.Orientation = 0

.AddIndent = False

.ShrinkToFit = False

.MergeCells = True

End With

End Sub

'打印預覽

Public Function PrintPreview() As Boolean

On Error GoTo errHandle:

xlApp.Visible = True

xlBook.PrintPreview True

Exit Function

errHandle:

If Err.Number = 1004 Then

MsgBox "尚未安裝打印機,不能預覽!", vbOKOnly + vbCritical, "錯誤"

End If

End Function

'導出

Public Function ExportExcel() As Boolean

xlApp.Visible = True

End Function

'畫線

Public Sub DrawLine(bRow As Integer, bCol As Integer, eRow As Integer, eCol As Integer)

On Error Resume Next

xlSheet.Range(GetExcelCell(bRow, bCol) & ":" & GetExcelCell(eRow, eCol)).Select

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 With

End Sub

'導出記錄集到Excel

Public Sub RstExport(Rst As ADODB.Recordset, bRow As Integer, bCol As Integer, GridHead() As String)

Dim i As Integer, j As Integer

For i = bCol To UBound(GridHead) + bCol

With Me

.TextMatrix(bRow, i) = GridHead(i - bCol)

End With

Next

i = 1 + bRow

Do While Not Rst.EOF

For j = 1 To Rst.Fields.Count

If Rst.Fields(j - 1).Type = adChar Or Rst.Fields(j - 1).Type = adVarChar Then

xlSheet.Range(GetExcelCell(i, j) & ":" & GetExcelCell(i, j)).Select

xlApp.Selection.NumberFormatLocal = "@" '已文本方式格式化

End If

Me.TextMatrix(i, j) = checkNull(Rst.Fields(j - 1).Value)

Next

i = i + 1

Rst.MoveNext

Loop

End Sub

'或者指定行,列號的Excel編碼

Private Function GetExcelCell(Row As Integer, Col As Integer) As String

Dim nTmp1 As Integer

Dim nTmp2 As Integer

Dim sTmp As String

If Col <= 26 Then

sTmp = Chr(Asc("A") + Col - 1)

Else

nTmp1 = Col \ 26

If nTmp1 > 26 Then

Err.Raise 100000, , "列數過大,發生錯誤"

Exit Function

Else

sTmp = Chr(Asc("A") + nTmp1 - 1)

nTmp1 = Col Mod 26

sTmp = sTmp & Chr(Asc("A") + nTmp1 - 1)

End If

End If

GetExcelCell = sTmp & Row

End Function

'將Null返回為空串

Private Function checkNull(s As Variant) As String

checkNull = IIf(IsNull(s), "", s)

End Function

Private Sub Class_Terminate()

Set xlApp = Nothing

Set xlBook = Nothing

Set xlSheet = Nothing

End Sub

  • 上一篇:如何為Linux安裝Go語言
  • 下一篇:北大青鳥設計培訓:Java編程工具有哪些比較好用?
  • copyright 2024編程學習大全網