?? test.bas
字號:
#COMPILE EXE
#INCLUDE "Excel.inc"
%XLSFALSE = 0
%XLSTRUE = NOT %XLSFALSE
FUNCTION PBMAIN() AS LONG
starttime! = TIMER
'enable the buffer. This is optional, but enabling a buffer will speed
'up file access.
stat& = xlsBuffer(%XLSTRUE, (512 * 1024)) 'a 512K buffer
'Create the new spreadsheet
mFileName$ = ".\vbtest.xls" 'create spreadsheet in the current directory
stat& = xlsCreateFile(mFileName$)
'stat& returns non-zero if an error occured.
'set a Password for the file. If set, the rest of the spreadsheet will
'be encrypted. If a password is used it must immediately follow the
'xlsCreateFile function call.
'This is different then protecting the spreadsheet (see below).
'NOTE: For some reason this function does not work. Excel will
'recognize that the file is password protected, but entering the password
'will not work. Also, the file is not encrypted. Therefore, do not use
'this function until I can figure out why it doesn't work. There is not
'much documentation on this function available.
'stat& = xlsSetFilePassword("PAUL_SQUIRES")
'specify whether to print the gridlines or not
'this should come before the setting of fonts and margins
stat& = xlsPrintGridLines(%XLSTRUE)
'it is a good idea to set margins, fonts and column widths
'prior to writing any text/numerics to the spreadsheet. These
'should come before setting the fonts.
stat& = xlsSetMargin(%xlsTopMargin, 1.5) 'set to 1.5 inches
stat& = xlsSetMargin(%xlsLeftMargin, 1.5)
stat& = xlsSetMargin(%xlsRightMargin, 1.5)
stat& = xlsSetMargin(%xlsBottomMargin, 1.5)
'Up to 4 fonts can be specified for the spreadsheet. This is a
'limitation of the Excel 2.1 format. For each value written to the
'spreadsheet you can specify which font to use.
stat& = xlsSetFont("Arial", 10, %xlsNoFormat) 'font0
stat& = xlsSetFont("Arial", 10, %xlsBold) 'font1
stat& = xlsSetFont("Arial", 10, %xlsBold + %xlsUnderline) 'font2
stat& = xlsSetFont("Courier", 18, %xlsItalic) 'font3
'Column widths are specified in Excel as 1/256th of a character.
stat& = xlsSetColumnWidth(1, 1, 50)
'set the global row height for the entire spreadsheet
stat& = xlsSetDefaultRowHeight(16)
'set the height of the first two rows a little bigger to allow for the
'title of the spreadsheet.
stat& = xlsSetRowHeight(1, 24)
stat& = xlsSetRowHeight(2, 24)
'set any header or footer that you want to print on
'every page. This text will be centered at the top and/or
'bottom of each page. The font will always be the font that
'is specified as font0, therefore you should only set the
'header/footer after specifying the fonts through SetFont.
stat& = xlsSetHeader("This is the header")
stat& = xlsSetFooter("This is the footer")
'write some data to the spreadsheet
stat& = xlsWriteInteger(20, 6, 1, %xlsFont0, %xlsLeftAlign, %xlsCellNormal, 0)
'write a cell with a shaded number with a bottom border
stat& = xlsWriteNumber(12123.456, 7, 1, %xlsFont1, %xlsrightAlign + %xlsBottomBorder + %xlsShaded, %xlsCellNormal, 0)
'write a normal left aligned string using font2 (bold & underline)
stat& = xlsWriteText("This is a test string", 8, 1, %xlsFont2, %xlsLeftAlign, %xlsCellNormal, 0)
'write a locked cell. The cell will not be able to be overwritten, BUT you
'must set the sheet PROTECTION to on before it will take effect!!!
stat& = xlsWriteText("This cell is locked.", 9, 1, %xlsFont3, %xlsLeftAlign, %xlsCellLocked, 0)
'fill the cell with "F"'s
stat& = xlsWriteText("F", 10, 1, %xlsFont3, %xlsFillCell, %xlsCellNormal, 0)
'write a hidden cell to the spreadsheet. This only works for cells
'that contain formulae. Text, Number, Integer value text can not be hidden
'using this feature. It is included here for the sake of completeness.
stat& = xlsWriteText("If this were a formula it would be hidden!", 11, 1, %xlsFont0, %xlsCentreAlign, %xlsCellHidden, 0)
'write a date to the file. Dates can be written as literal text strings but doing so will not allow
'the date to be formatted. The date will be eventually written as a number after conversion to a
'Julian date number.
'Write todays date..... Use format #12 mm/dd/yy (you can change the different formats by modifying
'the code in xlsWriteDefaultFormats. The date is expected to be in the YYYYMMDD format. You can convert
'from mm-dd-yyyy format to YYYYMMDD by calling the CTOD$ function.
mDate$ = CTOD$(DATE$)
stat& = xlsWriteDate(mDate$, 15, 1, %xlsFont1, %xlsLeftAlign, %xlsCellNormal, 12)
mDate$ = "19991128"
stat& = xlsWriteDate(mDate$, 16, 1, %xlsFont1, %xlsLeftAlign, %xlsCellNormal, 13)
mDate$ = "20000331"
stat& = xlsWriteDate(mDate$, 17, 1, %xlsFont1, %xlsLeftAlign, %xlsCellNormal, 14)
mDate$ = "20010630"
stat& = xlsWriteDate(mDate$, 18, 1, %xlsFont1, %xlsLeftAlign, %xlsCellNormal, 15)
mDate$ = "19991023"
stat& = xlsWriteDate(mDate$, 19, 1, %xlsFont1, %xlsLeftAlign, %xlsCellNormal, 20)
'insert a page break
stat& = xlsInsertHorizPageBreak(20)
'write consecutive numbers in the column
'This demonstrates that this Excel code can exceed the normal BIFF 2.1 and Excel 95 row limit
'of 32,767 rows.
NumRows& = 50000
FOR x# = 1 TO NumRows&
stat& = xlsWriteNumber(x#, 20+x#, 1, %xlsFont1, %xlsLeftAlign, %xlsCellNormal, 0)
NEXT
'PROTECT the spreadsheet so any cells specified as LOCKED will not be
'overwritten. Also, all cells with HIDDEN set will hide their formulae.
'PROTECT does not use a password.
stat& = xlsProtectSpreadsheet(%XLSTRUE)
'Finally, close the spreadsheet
stat& = xlsCloseFile
endtime! = TIMER
MSGBOX "Excel BIFF Spreadsheet created (" & TRIM$(FORMAT$(endtime!-starttime!, "###.####")) & " seconds)" & $CrLf & "Filename: " & mFileName$,, "Excel API"
END FUNCTION
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -