VB匯出EXCEL表

來源:互聯網
上載者:User

            從"工程"菜單中選擇"引用"欄;選擇Microsoft
Excel  
14 .0 Object Library,然後選擇"確定"。表示在工程中要引用EXCEL類型庫。

   
然後就是實現從MSHFlexGrid控制項中將資料匯出到EXCEL表中(注意:MSHFlexGrid控制項是從第0行,第0列開始的,而EXCEL卻是從第1行,第1列開始的)。代碼如下: 

Dim i As Long    Dim j As Long    If myFlexGrid.TextMatrix(1, 0) = "" Then        MsgBox "沒有資料匯出", vbInformation, "提示"        Exit Sub    End If'建立一個Application對象    Dim excelApp As Excel.Application    Set excelApp = New Excel.Application'綁定    Set excelApp = CreateObject("Excel.Application")'向Excel中寫入資料    Dim exbook  As Excel.Workbook    Dim exsheet  As Excel.Worksheet    Set exbook = excelApp.Workbooks.Add    excelApp.SheetsInNewWorkbook = 1'設定為可見    excelApp.Visible = True'將控制項MSHFlexGrid顯示的內容寫入Excel中    With excelApp.ActiveSheet        For i = 1 To myFlexGrid.Rows            For j = 1 To myFlexGrid.Cols                .Cells(i, j).Value = "" & Format$(myFlexGrid.TextMatrix(i - 1, j - 1))            Next j        Next i    End With    '清空並關閉    Set exsheet = Nothing    Set exbook = Nothing    Set excelApp = Nothing

以上只是用到EXCEL與VB互動的最基本的內容,下面我們來做一些擴充:

首先建立 Excel
對象,使用ComObj:

DimexcelApp as Excel.Application

SetexcelApp as new Excel.Application

1)顯示當前視窗:

excelApp.Visible= True

2)更改 Excel
標題列:

excelApp.Caption='學生上機記錄';

3)添加新活頁簿:

excelApp.WorkBooks.Add;

4)開啟已存在的活頁簿:

EexcelApp.WorkBooks.Open('CExcelDemo.xls' );

5)設定第2個工作表為使用中工作表:

excelApp.WorkSheets(2).Activate;

或 excelApp.WorkSheets( 'Sheet2').Activate;

6)給儲存格賦值:

excelApp.Cells(1,4).Value= '第一行,第四列';

12)清除第一行第四列儲存格公式:

excelApp.ActiveSheet.Cells(1,4).ClearContents;

13)設定第一行字型屬性:

excelApp.ActiveSheet.Rows(1).Font.Name= '隸書';

excelApp.ActiveSheet.Rows(1).Font.Color= clBlue;

excelApp.ActiveSheet.Rows(1).Font.Bold= True;

excelApp.ActiveSheet.Rows(1).Font.UnderLine= True;

 

 

k.列印儲存格網線:

excelApp.ActiveSheet.PageSetup.PrintGridLines= True;

15)拷貝操作:

a.拷貝整個工作表:

excelApp.ActiveSheet.Used.Range.Copy;

b.拷貝指定地區:

excelApp.ActiveSheet.Range('A1E2' ).Copy;

c.從A1位置開始粘貼:

excelApp.ActiveSheet.Range.('A1' ).PasteSpecial;

d.從檔案尾部開始粘貼:

excelApp.ActiveSheet.Range.PasteSpecial;

16)插入一行或一列:

a.excelApp.ActiveSheet.Rows(2).Insert;

b.excelApp.ActiveSheet.Columns(1).Insert;

17)刪除一行或一列:

a.excelApp.ActiveSheet.Rows(2).Delete;

b.excelApp.ActiveSheet.Columns(1).Delete;

18)預覽列印工作表:

excelApp.ActiveSheet.PrintPreview;

19)列印輸出工作表:

excelApp.ActiveSheet.PrintOut;

20)工作表儲存:

If notexcelApp.ActiveWorkBook.Saved then

excelApp.ActiveSheet.PrintPreview

End if

21)工作表另存新檔:

excelApp.SaveAs('CExcelDemo1.xls' );

22)放棄存檔:

excelApp.ActiveWorkBook.Saved= True;

23)關閉活頁簿:

excelApp.WorkBooks.Close;

24)退出 Excel:

excelApp.Quit;

25)設定工作表密碼

excelApp.ActiveSheet.Protect"123", DrawingObjects=True, Contents=True, Scenarios=True

26)EXCEL的顯示方式為最大化

excelApp.Application.WindowState= xlMaximized

27)工作薄顯示方式為最大化

excelApp.ActiveWindow.WindowState= xlMaximized

28)設定開啟預設工作薄數量

excelApp.SheetsInNewWorkbook= 3

29)'關閉時是否提示儲存(true
儲存;false
不儲存)

excelApp.DisplayAlerts= False

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.