📄 excelfile.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "ExcelFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'Class file for writing Microsoft Excel BIFF 2.1 files.
'This class is intended for users who do not want to use the huge
'Jet or ADO providers if they only want to export their data to
'an Excel compatible file.
'Newer versions of Excel use the OLE Structure Storage methods
'which are quite complicated.
'Paul Squires, November 10, 2001 (Freeware)
'support@planetsquires.com
'
'Copyright (c) 2001 by Paul Squires.
'Although this code is available for free, the author retains the copyright, which means that you
'cannot do anything with it that is not expressly allowed by the author. In general terms, the author
'would allow the programmer to incorporate the code into their applications. Selling the code by
'itself is prohibited.
'
'Added default-cellformats: Dieter Hauk January 8, 2001 dieter.hauk@epost.de
'Added default row height: Matthew Brewster November 9, 2001
'the memory copy API is used in the MKI$ function which converts an integer
'value to a 2-byte string value to write to the file. (used by the Horizontal
'Page Break function).
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
'enum to handle the various types of values that can be written
'to the excel file.
Public Enum ValueTypes
xlsinteger = 0
xlsnumber = 1
xlsText = 2
End Enum
'enum to hold cell alignment
Public Enum CellAlignment
xlsGeneralAlign = 0
xlsLeftAlign = 1
xlsCentreAlign = 2
xlsrightAlign = 3
xlsFillCell = 4
xlsLeftBorder = 8
xlsRightBorder = 16
xlsTopBorder = 32
xlsBottomBorder = 64
xlsShaded = 128
End Enum
'enum to handle selecting the font for the cell
Public Enum CellFont
'used by rgbAttr2
'bits 0-5 handle the *picture* formatting, not bold/underline etc...
'bits 6-7 handle the font number
xlsFont0 = 0
xlsFont1 = 64
xlsFont2 = 128
xlsFont3 = 192
End Enum
Public Enum CellHiddenLocked
'used by rgbAttr1
'bits 0-5 must be zero
'bit 6 locked/unlocked
'bit 7 hidden/not hidden
xlsNormal = 0
xlsLocked = 64
xlsHidden = 128
End Enum
'set up variables to hold the spreadsheet's layout
Public Enum MarginTypes
xlsLeftMargin = 38
xlsRightMargin = 39
xlsTopMargin = 40
xlsBottomMargin = 41
End Enum
Public Enum FontFormatting
'add these enums together. For example: xlsBold + xlsUnderline
xlsNoFormat = 0
xlsBold = 1
xlsItalic = 2
xlsUnderline = 4
xlsStrikeout = 8
End Enum
Private Type FONT_RECORD
opcode As Integer '49
length As Integer '5+len(fontname)
FontHeight As Integer
'bit0 bold, bit1 italic, bit2 underline, bit3 strikeout, bit4-7 reserved
FontAttributes1 As Byte
FontAttributes2 As Byte 'reserved - always 0
FontNameLength As Byte
End Type
Private Type PASSWORD_RECORD
opcode As Integer '47
length As Integer 'len(password)
End Type
Private Type HEADER_FOOTER_RECORD
opcode As Integer '20 Header, 21 Footer
length As Integer '1+len(text)
TextLength As Byte
End Type
Private Type PROTECT_SPREADSHEET_RECORD
opcode As Integer '18
length As Integer '2
Protect As Integer
End Type
Private Type FORMAT_COUNT_RECORD
opcode As Integer '1f
length As Integer '2
Count As Integer
End Type
Private Type FORMAT_RECORD
opcode As Integer '1e
length As Integer '1+len(format)
FormatLenght As Byte 'len(format)
End Type '+ followed by the Format-Picture
Private Type COLWIDTH_RECORD
opcode As Integer '36
length As Integer '4
col1 As Byte 'first column
col2 As Byte 'last column
ColumnWidth As Integer 'at 1/256th of a character
End Type
'Beginning Of File record
Private Type BEG_FILE_RECORD
opcode As Integer
length As Integer
version As Integer
ftype As Integer
End Type
'End Of File record
Private Type END_FILE_RECORD
opcode As Integer
length As Integer
End Type
'true/false to print gridlines
Private Type PRINT_GRIDLINES_RECORD
opcode As Integer
length As Integer
PrintFlag As Integer
End Type
'Integer record
Private Type tInteger
opcode As Integer
length As Integer
Row As Integer 'unsigned integer
col As Integer
'rgbAttr1 handles whether cell is hidden and/or locked
rgbAttr1 As Byte
'rgbAttr2 handles the Font# and Formatting assigned to this cell
rgbAttr2 As Byte
'rgbAttr3 handles the Cell Alignment/borders/shading
rgbAttr3 As Byte
intValue As Integer 'the actual integer value
End Type
'Number record
Private Type tNumber
opcode As Integer
length As Integer
Row As Integer
col As Integer
rgbAttr1 As Byte
rgbAttr2 As Byte
rgbAttr3 As Byte
NumberValue As Double '8 Bytes
End Type
'Label (Text) record
Private Type tText
opcode As Integer
length As Integer
Row As Integer
col As Integer
rgbAttr1 As Byte
rgbAttr2 As Byte
rgbAttr3 As Byte
TextLength As Byte
End Type
Private Type MARGIN_RECORD_LAYOUT
opcode As Integer
length As Integer
MarginValue As Double '8 bytes
End Type
Private Type HPAGE_BREAK_RECORD
opcode As Integer
length As Integer
NumPageBreaks As Integer
End Type
Private Type DEF_ROWHEIGHT_RECORD
opcode As Integer
length As Integer
RowHeight As Integer
End Type
Private Type ROW_HEIGHT_RECORD
opcode As Integer '08
length As Integer 'should always be 16 bytes
RowNumber As Integer
FirstColumn As Integer
LastColumn As Integer
RowHeight As Integer 'written to file as 1/20ths of a point
internal As Integer
DefaultAttributes As Byte 'set to zero for no default attributes
FileOffset As Integer
rgbAttr1 As Byte
rgbAttr2 As Byte
rgbAttr3 As Byte
End Type
Private FileNumber As Integer
Private BEG_FILE_MARKER As BEG_FILE_RECORD
Private END_FILE_MARKER As END_FILE_RECORD
Private HORIZ_PAGE_BREAK As HPAGE_BREAK_RECORD
'create an array that will hold the rows where a horizontal page
'break will be inserted just before.
Private HorizPageBreakRows() As Integer
Private NumHorizPageBreaks As Integer
Public Function CreateFile(ByVal FileName As String) As Integer
On Error GoTo Write_Error
If Dir$(FileName) > "" Then
Kill FileName
End If
FileNumber = FreeFile
Open FileName For Binary As #FileNumber
Put #FileNumber, , BEG_FILE_MARKER 'must always be written first
Call WriteDefaultFormats
'create the Horizontal Page Break array
ReDim HorizPageBreakRows(0)
NumHorizPageBreaks = 0
OpenFile = 0 'return with no error
Exit Function
Write_Error:
OpenFile = Err.Number
Exit Function
End Function
Public Function CloseFile() As Integer
On Error GoTo Write_Error
If FileNumber = 0 Then Exit Function
'write the horizontal page breaks if necessary
If NumHorizPageBreaks > 0 Then
'the Horizontal Page Break array must be in sorted order.
'Use a simple Bubble sort because the size of this array would
'be pretty small most of the time. A QuickSort would probably
'be overkill.
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
For lLoop1 = UBound(HorizPageBreakRows) To LBound(HorizPageBreakRows) Step -1
For lLoop2 = LBound(HorizPageBreakRows) + 1 To lLoop1
If HorizPageBreakRows(lLoop2 - 1) > HorizPageBreakRows(lLoop2) Then
lTemp = HorizPageBreakRows(lLoop2 - 1)
HorizPageBreakRows(lLoop2 - 1) = HorizPageBreakRows(lLoop2)
HorizPageBreakRows(lLoop2) = lTemp
End If
Next lLoop2
Next lLoop1
'write the Horizontal Page Break Record
With HORIZ_PAGE_BREAK
.opcode = 27
.length = 2 + (NumHorizPageBreaks * 2)
.NumPageBreaks = NumHorizPageBreaks
End With
Put #FileNumber, , HORIZ_PAGE_BREAK
'now write the actual page break values
'the MKI$ function is standard in other versions of BASIC but
'VisualBasic does not have it. A KnowledgeBase article explains
'how to recreate it (albeit using 16-bit API, I switched it
'to 32-bit).
For x% = 1 To UBound(HorizPageBreakRows)
Put #FileNumber, , MKI$(HorizPageBreakRows(x%))
Next
End If
Put #FileNumber, , END_FILE_MARKER
Close #FileNumber
CloseFile = 0 'return with no error code
Exit Function
Write_Error:
CloseFile = Err.Number
Exit Function
End Function
Private Sub Class_Initialize()
'Set up default values for records
'These should be the values that are the same for every record of these types
With BEG_FILE_MARKER 'beginning of file
.opcode = 9
.length = 4
.version = 2
.ftype = 10
End With
With END_FILE_MARKER 'end of file marker
.opcode = 10
End With
End Sub
Public Function InsertHorizPageBreak(lrow As Long) As Integer
On Error GoTo Page_Break_Error
'the row and column values are written to the excel file as
'unsigned integers. Therefore, must convert the longs to integer.
If lrow > 32767 Then
Row% = CInt(lrow - 65536)
Else
Row% = CInt(lrow) - 1 'rows/cols in Excel binary file are zero based
End If
NumHorizPageBreaks = NumHorizPageBreaks + 1
ReDim Preserve HorizPageBreakRows(NumHorizPageBreaks)
HorizPageBreakRows(NumHorizPageBreaks) = Row%
Exit Function
Page_Break_Error:
InsertHorizPageBreak = Err.Number
Exit Function
End Function
Public Function WriteValue(ValueType As ValueTypes, CellFontUsed As CellFont, Alignment As CellAlignment, HiddenLocked As CellHiddenLocked, lrow As Long, lcol As Long, value As Variant, Optional CellFormat As Long = 0) As Integer
On Error GoTo Write_Error
'the row and column values are written to the excel file as
'unsigned integers. Therefore, must convert the longs to integer.
If lrow > 32767 Then
Row% = CInt(lrow - 65536)
Else
Row% = CInt(lrow) - 1 'rows/cols in Excel binary file are zero based
End If
If lcol > 32767 Then
col% = CInt(lcol - 65536)
Else
col% = CInt(lcol) - 1 'rows/cols in Excel binary file are zero based
End If
Select Case ValueType
Case ValueTypes.xlsinteger
Dim INTEGER_RECORD As tInteger
With INTEGER_RECORD
.opcode = 2
.length = 9
.Row = Row%
.col = col%
.rgbAttr1 = CByte(HiddenLocked)
.rgbAttr2 = CByte(CellFontUsed + CellFormat)
.rgbAttr3 = CByte(Alignment)
.intValue = CInt(value)
End With
Put #FileNumber, , INTEGER_RECORD
Case ValueTypes.xlsnumber
Dim NUMBER_RECORD As tNumber
With NUMBER_RECORD
.opcode = 3
.length = 15
.Row = Row%
.col = col%
.rgbAttr1 = CByte(HiddenLocked)
.rgbAttr2 = CByte(CellFontUsed + CellFormat)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -