1ãè°ç¨Excel对象æå¼EXCELæ件åï¼è¯»åæ件å
容
***************************************
å¨VBä¸è¦æ³è°ç¨Excelï¼éè¦æå¼VBç¼ç¨ç¯å¢âå·¥ç¨âèåä¸çâå¼ç¨â项ç®ï¼å¹¶éå项ç®ä¸çâMicrosoftExcel 11.0 object libraryâ项ãç±äºä½ çExcelçæ¬ä¸åï¼æ以è¿ä¸ªé项ç
çæ¬å·ä¹æ¯ä¸åçã
å 为EXCELæ¯ä»¥å±æ¬¡ç»æç»ç»å¯¹è±¡çï¼å
¶å¯¹è±¡æ¨¡åä¸å«æ许å¤ä¸åç对象å
ç´ ã
第ä¸å±ï¼Application对象ï¼å³Excelæ¬èº«ï¼
第äºå±ï¼workbooks对象éï¼æExcelçå·¥ä½ç°¿æ件
第ä¸å±ï¼worksheets对象éï¼è¡¨ç¤ºçæ¯Excelçä¸ä¸ªå·¥ä½è¡¨ï¼
第åå±ï¼CellsåRange对象ï¼æåExcelå·¥ä½è¡¨ä¸çåå
æ ¼ã
æ°å»ºç«ä¸ä¸ªVBçå·¥ç¨ï¼å
æ¾ä¸ä¸ªbuttonï¼å称为Excel_Outãå
å®ä¹å¥½åå±ï¼
Dim xlapp As Excel.Application 'Excel对象
Dim xlbook As Excel.Workbook 'å·¥ä½ç°¿
Dim xlsheet As Excel.Worksheet 'å·¥ä½è¡¨
æ们æç®åçæ¯ï¼æå¼/æ°å»ºä¸ä¸ªexcelï¼å¨å
¶ä¸å¯¹æå·¥ä½è¡¨çä¸äºåå
æ ¼ä¿®æ¹å
¶å¼ï¼ç¶åå¦å为test.xlsæ件ã
Private Sub Excel_Out_Click()
Dimi, j As Integer
Setxlapp = CreateObject("Excel.Application") 'å建EXCEL对象
'Set xlbook = xlapp.Workbooks.Open(App.Path& "\test.xls")'æå¼å·²ç»åå¨çtest.xls工件簿æ件
Setxlbook = xlapp.Workbooks.Add 'æ°å»ºEXCEL工件簿æ件
'xlbook.RunAutoMacros (xlAutoOpen) 'è¿è¡EXCELå¯å¨å®
'xlbook.RunAutoMacros (xlAutoClose) 'è¿è¡EXCELå
³éå®
xlapp.Visible = True '设置EXCEL对象å¯è§ï¼æä¸å¯è§ï¼
Setxlsheet = xlbook.Worksheets(1) '设置活å¨å·¥ä½è¡¨''
''~~~å½åå·¥ä½ç°¿ç第ä¸é¡µ,è¿éä¹å¯ä»¥æ¢æâ表åâ
'ä¸é¢å°±æ¯ç®åçå¨ä¸äºåå
æ ¼å
åå
¥æ°å
Fori = 7 To 15
For j = 1 To 10
xlsheet.Cells(i, j) = j 'å½åå·¥ä½ç°¿ç¬¬ä¸é¡µç第Iè¡ç¬¬Jå
Next j
Nexti
Withxlsheet '设置边æ¡ä¸ºæ¯å®çº¿
.Range(.Cells(7, 1), .Cells(28, 29)).Borders.LineStyle = xlContinuous
EndWith
'å¼ç¨å½åå·¥ä½ç°¿ç第äºé¡µ
Setxlsheet = xlapp.Application.Worksheets(2)
xlsheet.Cells(7, 2) = 2008 'å¨ç¬¬äºé¡µç第7è¡ç¬¬2ååå
¥2008
xlsheet.SaveAsApp.Path&"\test.xls" 'ææå®æ件ååç
'Setxlbook = xlapp.Application.Workbooks.Add 'æ°å»ºä¸ç©ºç½å·¥ä½ç°¿
xlapp.Quit 'ç»æEXCEL对象'xlapp.Workbooks.Close
Setxlapp = Nothing 'éæ¾xlApp对象
End Sub
è¿æ ·ï¼æ们就å¯ä»¥ç®åç对excelæ件è¿è¡æä½äºãä¸é¢åæ¾ä¸äºæä»ç½ä¸æ¥å°çèµæå§ï¼è¿æºæç¨çï¼
1ï¼å建Excel对象
eole=CREATEOBJECT(â²Excel.applicationâ²)
2ï¼æ·»å æ°å·¥ä½ç°¿
eole.Workbooks.add
3ï¼è®¾ç½®ç¬¬3个工ä½è¡¨ä¸ºæ¿æ´»å·¥ä½è¡¨
eole.Worksheets(ãsheet3ã).Activate
4ï¼æå¼æå®å·¥ä½ç°¿
eole.Workbooks.Open(ãc:\temp\ll.xlsã)
5ï¼æ¾ç¤ºExcelçªå£
eole.visible=.t.
6ï¼æ´æ¹Excelæ é¢æ
eole.Caption=ãVB
åºç¨ç¨åºè°ç¨Microsoft Excelã
7ï¼ç»åå
æ ¼èµå¼
eole.cells(1,4).value=XM(XM为æ°æ®åºå段å)
8ï¼è®¾ç½®æå®åç宽度(åä½ï¼å符个æ°)
eole.ActiveSheet.Columns(1).ColumnWidth=5
9ï¼è®¾ç½®æå®è¡çé«åº¦(åä½ï¼ç£
)
eole.ActiveSheet.Rows(1).RowHeight=1/0.035
(设å®è¡é«ä¸º1åç±³ï¼1ç£
=0.035åç±³)
10ï¼å¨ç¬¬18è¡ä¹åæå
¥
å页符eole.Worksheets(ãSheet1ã).Rows(18).PageBreak=1
11ï¼å¨ç¬¬4åä¹åå é¤å页符
eole.ActiveSheet.Columns(4).PageBreak=0
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ï¼åç»å®çº¿)
14ï¼è®¾ç½®é¡µç
eole.ActiveSheet.PageSetup.CenterHeader=ãæ¥è¡¨1ã
15ï¼è®¾ç½®é¡µè
eole.ActiveSheet.PageSetup.CenterFooter=ã第ï¼P页ã
16ï¼è®¾ç½®é¡µçå°é¡¶ç«¯è¾¹è·ä¸º2åç±³
eole.ActiveSheet.PageSetup.HeaderMargin=2/0.035
17ï¼è®¾ç½®é¡µèå°åºè¾¹è·ä¸º3åç±³
eole.ActiveSheet.PageSetup.FooterMargin=3/0.035
18ï¼è®¾ç½®é¡¶è¾¹è·ä¸º2åç±³
eole.ActiveSheet.PageSetup.TopMargin=2/0.035
19ï¼è®¾ç½®åºè¾¹è·ä¸º4åç±³
eole.ActiveSheet.PageSetup.BottomMargin=4/0.035
20ï¼è®¾ç½®å·¦è¾¹è·ä¸º2åç±³
veole.ActiveSheet.PageSetup.LeftMargin=2/0.035
21ï¼è®¾ç½®å³è¾¹è·ä¸º2åç±³
eole.ActiveSheet.PageSetup.RightMargin=2/0.035
22ï¼è®¾ç½®é¡µé¢æ°´å¹³å±
ä¸
eole.ActiveSheet.PageSetup.CenterHorizontally=.t.
23ï¼è®¾ç½®é¡µé¢åç´å±
ä¸
eole.ActiveSheet.PageSetup.CenterVertically=.t.
24ï¼è®¾ç½®é¡µé¢çº¸å¼
大å°(1ï¼çªè¡8511 39ï¼å®½è¡1411)
eole.ActiveSheet.PageSetup.PaperSize=1
25ï¼æå°åå
æ ¼ç½çº¿
eole.ActiveSheet.PageSetup.PrintGridlines=.t.
26ï¼æ·è´æ´ä¸ªå·¥ä½è¡¨
eole.ActiveSheet.UsedRange.Copy
27ï¼æ·è´æå®åºå
eole.ActiveSheet.Range(ãA1:E2ã).Copy
28ï¼ç²è´´
eole.WorkSheet(ãSheet2ã).Range(ãA1ã).PasteSpecial
29ï¼å¨ç¬¬2è¡ä¹åæå
¥ä¸è¡
eole.ActiveSheet.Rows(2).Insert
30ï¼å¨ç¬¬2åä¹åæå
¥ä¸å
eole.ActiveSheet.Columns(2).Insert
31ï¼è®¾ç½®åä½
eole.ActiveSheet.Cells(2,1).Font.Name=ã
é»ä½ã
32ï¼è®¾ç½®åä½å¤§å°
eole.ActiveSheet.Cells(1,1).Font.Size=25
33ï¼è®¾ç½®åä½ä¸ºæä½
eole.ActiveSheet.Cells(1,1).Font.Italic=.t.
34ï¼è®¾ç½®æ´ååä½ä¸ºç²ä½
eole.ActiveSheet.Columns(1).Font.Bold=.t.
35ï¼æ¸
é¤åå
æ ¼å
¬å¼
eole.ActiveSheet.Cells(1,4).ClearContents
36ï¼æå°é¢è§å·¥ä½è¡¨
eole.ActiveSheet.PrintPreview
37ï¼æå°è¾åºå·¥ä½è¡¨
eole.ActiveSheet.PrintOut
38ï¼å·¥ä½è¡¨å¦å为
eole.ActiveWorkbook.SaveAs(ãc:\temp\22.xlsã)
39ï¼æ¾å¼åç
eole.ActiveWorkbook.saved=.t.
40ï¼å
³éå·¥ä½ç°¿
eole.Workbooks.close
41ï¼éåºExcel
eole.quit
***************************************
2ãè°ç¨ADO对象以æ°æ®åºæ¹å¼æå¼EXECLæ件åï¼ä»¥SQLè¯å¥æ¹å¼è¯»åæ件
***************************************
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};" DBQ=" & App.Path & ";", "", ""
rs.Open "select * from [test$]", conn, adOpenStatic, adLockReadOnly, adCmdText
Set Read_Text_File = rs
Set rs = Nothing
Set conn = Nothing
***************************************