ç»ä½ ç¹VBæä½Excelçèµæ
å¸æå¯¹ä½ æ帮å©~
VBæä½EXCEL
å
¨é¢æ§å¶ Excel
é¦å
å建 Excel 对象ï¼ä½¿ç¨ComObj:
Dim ExcelID as Excel.Application
Set ExcelID as new Excel.Application
1) æ¾ç¤ºå½åçªå£ï¼
ExcelID.Visible := True;
2) æ´æ¹ Excel æ é¢æ ï¼
ExcelID.Caption := \'åºç¨ç¨åºè°ç¨ Microsoft Excel\';
3) æ·»å æ°å·¥ä½ç°¿ï¼
ExcelID.WorkBooks.Add;
4) æå¼å·²åå¨çå·¥ä½ç°¿ï¼
ExcelID.WorkBooks.Open( \'C:\\Excel\\Demo.xls\' );
5) 设置第2个工ä½è¡¨ä¸ºæ´»å¨å·¥ä½è¡¨ï¼
ExcelID.WorkSheets[2].Activate;
æ ExcelID.WorkSheets[ \'Sheet2\' ].Activate;
6) ç»åå
æ ¼èµå¼ï¼
ExcelID.Cells[1,4].Value := \'第ä¸è¡ç¬¬åå\';
7) 设置æå®åç宽度ï¼åä½ï¼å符个æ°ï¼ï¼ä»¥ç¬¬ä¸å为ä¾ï¼
ExcelID.ActiveSheet.Columns[1].ColumnsWidth := 5;
8) 设置æå®è¡çé«åº¦ï¼åä½ï¼ç£
ï¼ï¼1ç£
ï¼0.035åç±³ï¼ï¼ä»¥ç¬¬äºè¡ä¸ºä¾ï¼
ExcelID.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1åç±³
9) å¨ç¬¬8è¡ä¹åæå
¥å页符ï¼
ExcelID.WorkSheets[1].Rows[8].PageBreak := 1;
åè代ç ï¼ActiveSheet.HPageBreaks(1).Location = Range("A22")
10) å¨ç¬¬8åä¹åå é¤å页符ï¼
ExcelID.ActiveSheet.Columns[4].PageBreak := 0;
11) æå®è¾¹æ¡çº¿å®½åº¦ï¼
ExcelID.ActiveSheet.Range[ \'B3:D4\' ].Borders[2].Weight := 3;
1-å·¦ 2-å³ 3-顶 4-åº 5-æ( \\ ) 6-æ( / )
12) æ¸
é¤ç¬¬ä¸è¡ç¬¬åååå
æ ¼å
¬å¼ï¼
ExcelID.ActiveSheet.Cells[1,4].ClearContents;
13) 设置第ä¸è¡åä½å±æ§ï¼
ExcelID.ActiveSheet.Rows[1].Font.Name := \'é¶ä¹¦\';
ExcelID.ActiveSheet.Rows[1].Font.Color := clBlue;
ExcelID.ActiveSheet.Rows[1].Font.Bold := True;
ExcelID.ActiveSheet.Rows[1].Font.UnderLine := True;
14) è¿è¡é¡µé¢è®¾ç½®ï¼
a.页çï¼
ExcelID.ActiveSheet.PageSetup.CenterHeader := \'æ¥è¡¨æ¼ç¤º\';
b.页èï¼
ExcelID.ActiveSheet.PageSetup.CenterFooter := \'第&P页\';
c.页çå°é¡¶ç«¯è¾¹è·2cmï¼
ExcelID.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;
d.页èå°åºç«¯è¾¹è·3cmï¼
ExcelID.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;
e.顶边è·2cmï¼
ExcelID.ActiveSheet.PageSetup.TopMargin := 2/0.035;
f.åºè¾¹è·2cmï¼
ExcelID.ActiveSheet.PageSetup.BottomMargin := 2/0.035;
g.左边è·2cmï¼
ExcelID.ActiveSheet.PageSetup.LeftMargin := 2/0.035;
h.å³è¾¹è·2cmï¼
ExcelID.ActiveSheet.PageSetup.RightMargin := 2/0.035;
i.页é¢æ°´å¹³å±
ä¸ï¼
ExcelID.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
j.页é¢åç´å±
ä¸ï¼
ExcelID.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
k.æå°åå
æ ¼ç½çº¿ï¼
ExcelID.ActiveSheet.PageSetup.PrintGridLines := True;
15) æ·è´æä½ï¼
a.æ·è´æ´ä¸ªå·¥ä½è¡¨ï¼
ExcelID.ActiveSheet.Used.Range.Copy;
b.æ·è´æå®åºåï¼
ExcelID.ActiveSheet.Range[ \'A1:E2\' ].Copy;
c.ä»A1ä½ç½®å¼å§ç²è´´ï¼
ExcelID.ActiveSheet.Range.[ \'A1\' ].PasteSpecial;
d.ä»æ件尾é¨å¼å§ç²è´´ï¼
ExcelID.ActiveSheet.Range.PasteSpecial;
16) æå
¥ä¸è¡æä¸åï¼
a. ExcelID.ActiveSheet.Rows[2].Insert;
b. ExcelID.ActiveSheet.Columns[1].Insert;
17) å é¤ä¸è¡æä¸åï¼
a. ExcelID.ActiveSheet.Rows[2].Delete;
b. ExcelID.ActiveSheet.Columns[1].Delete;
18) æå°é¢è§å·¥ä½è¡¨ï¼
ExcelID.ActiveSheet.PrintPreview;
19) æå°è¾åºå·¥ä½è¡¨ï¼
ExcelID.ActiveSheet.PrintOut;
20) å·¥ä½è¡¨ä¿åï¼
If not ExcelID.ActiveWorkBook.Saved then
ExcelID.ActiveSheet.PrintPreview
End if
21) å·¥ä½è¡¨å¦å为ï¼
ExcelID.SaveAs( \'C:\\Excel\\Demo1.xls\' );
22) æ¾å¼åçï¼
ExcelID.ActiveWorkBook.Saved := True;
23) å
³éå·¥ä½ç°¿ï¼
ExcelID.WorkBooks.Close;
24) éåº Excelï¼
ExcelID.Quit;
25) 设置工ä½è¡¨å¯ç :
ExcelID.ActiveSheet.Protect "123", DrawingObjects:=True, Contents:=True, Scenarios:=True
26) EXCELçæ¾ç¤ºæ¹å¼ä¸ºæ大å
ExcelID.Application.WindowState = xlMaximized
27) å·¥ä½èæ¾ç¤ºæ¹å¼ä¸ºæ大å
ExcelID.ActiveWindow.WindowState = xlMaximized
28) 设置æå¼é»è®¤å·¥ä½èæ°é
ExcelID.SheetsInNewWorkbook = 3
29) \'å
³éæ¶æ¯å¦æ示ä¿å(true ä¿åï¼false ä¸ä¿å)
ExcelID.DisplayAlerts = False
30) 设置æåçªå£,ååºå®è¡ä½ç½®
ExcelID.ActiveWindow.SplitRow = 1
ExcelID.ActiveWindow.FreezePanes = True
31) 设置æå°æ¶åºå®æå°å
容
ExcelID.ActiveSheet.PageSetup.PrintTitleRows = "$1:$1"
32) 设置æå°æ é¢
ExcelID.ActiveSheet.PageSetup.PrintTitleColumns = ""
33) 设置æ¾ç¤ºæ¹å¼(å页æ¹å¼æ¾ç¤º)
ExcelID.ActiveWindow.View = xlPageBreakPreview
34) 设置æ¾ç¤ºæ¯ä¾
ExcelID.ActiveWindow.Zoom = 100
35) 让Excel ååº DDE 请æ±
Ex.Application.IgnoreRemoteRequests = False
ç¨VBæä½EXCEL
Private Sub Command3_Click()
On Error GoTo err1
Dim i As Long
Dim j As Long
Dim objExl As Excel.Application \'声æ对象åé
Me.MousePointer = 11 \'æ¹åé¼ æ æ ·å¼
Set objExl = New Excel.Application \'åå§å对象åé
objExl.SheetsInNewWorkbook = 1 \'å°æ°å»ºçå·¥ä½èæ°é设为1
objExl.Workbooks.Add \'å¢å ä¸ä¸ªå·¥ä½è
objExl.Sheets(objExl.Sheets.Count).Name = "book1" \'ä¿®æ¹å·¥ä½èå称
objExl.Sheets.Add , objExl.Sheets("book1") âå¢å 第äºä¸ªå·¥ä½èå¨ç¬¬ä¸ä¸ªä¹å
objExl.Sheets(objExl.Sheets.Count).Name = "book2"
objExl.Sheets.Add , objExl.Sheets("book2") âå¢å 第ä¸ä¸ªå·¥ä½èå¨ç¬¬äºä¸ªä¹å
objExl.Sheets(objExl.Sheets.Count).Name = "book3"
objExl.Sheets("book1").Select \'éä¸å·¥ä½è<book1>
For i = 1 To 50 \'循ç¯åå
¥æ°æ®
For j = 1 To 5
If i = 1 Then
objExl.Selection.NumberFormatLocal = "@" \'è®¾ç½®æ ¼å¼ä¸ºææ¬
objExl.Cells(i, j) = " E " & i & j
Else
objExl.Cells(i, j) = i & j
End If
Next
Next
objExl.Rows("1:1").Select \'éä¸ç¬¬ä¸è¡
objExl.Selection.Font.Bold = True \'设为ç²ä½
objExl.Selection.Font.Size = 24 \'设置åä½å¤§å°
objExl.Cells.EntireColumn.AutoFit \'èªå¨è°æ´å宽
objExl.ActiveWindow.SplitRow = 1 \'æå第ä¸è¡
objExl.ActiveWindow. SplitColumn = 0 \'æåå
objExl.ActiveWindow.FreezePanes = True \'åºå®æå objExl.ActiveSheet.PageSetup.PrintTitleRows = "$1:$1" \'设置æå°åºå®è¡
objExl.ActiveSheet.PageSetup.PrintTitleColumns = "" \'æå°æ é¢ objExl.ActiveSheet.PageSetup.RightFooter = "æå°æ¶é´: " & _
Format(Now, "yyyyå¹´mmæddæ¥ hh:MM:ss")
objExl.ActiveWindow.View = xlPageBreakPreview \'设置æ¾ç¤ºæ¹å¼
objExl.ActiveWindow.Zoom = 100 \'设置æ¾ç¤ºå¤§å°
objExl.ActiveSheet.PageSetup.Orientation = xlLandscape â设置æå°æ¹å(横å)
\'ç»å·¥ä½è¡¨å å¯ç
objExl.ActiveSheet.Protect "123", DrawingObjects:=True, _
Contents:=True, Scenarios:=True
objExl.Application.IgnoreRemoteRequests = False
objExl.Visible = True \'使EXCELå¯è§
objExl.Application.WindowState = xlMaximized \'EXCELçæ¾ç¤ºæ¹å¼ä¸ºæ大å
objExl.ActiveWindow.WindowState = xlMaximized \'å·¥ä½èæ¾ç¤ºæ¹å¼ä¸ºæ大å
objExl.SheetsInNewWorkbook = 3 \'å°é»è®¤æ°å·¥ä½èæ°éæ¹å3个
Set objExl = Nothing \'æ¸
é¤å¯¹è±¡
Me.MousePointer = 0 \'ä¿®æ¹é¼ æ
Exit Sub
err1:
objExl.SheetsInNewWorkbook = 3
objExl.DisplayAlerts = False \'å
³éæ¶ä¸æ示ä¿å
objExl.Quit \'å
³éEXCEL
objExl.DisplayAlerts = True \'å
³éæ¶æ示ä¿å
Set objExl = Nothing
Me.MousePointer = 0
End Sub
温馨提示:内容为网友见解,仅供参考
在VB6中如何将access格式导出成EXCEL格式
Private Function ExportDetail(strFileName As String) As Boolean Dim iRow As Long Dim myExcel As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet On Error GoTo ErrAction ExportDetail = False Set myExcel = New Excel.Application myExcel.Visible = False my...
...Access数据库后 如何编写代码使数据库导出到excel、word或html上...
Private Sub Command1_Click() Dim xlapp As Variant Dim xlBook As Variant Dim xlSheet As Variant Dim sum As Long Set xlapp = CreateObject("excel.application") Set xlBook = xlapp.Workbooks.Open(App.Path & "\\data\\报表.xlt") '打开EXCEL模板 'Set xlBook =...
如何用vb将Access超过10万条数据快速导入EXCEL?知道可以分多个sheet存...
For i = 1 To ps myOpen1 = "select * into [Excel 8.0;database=" & ff & "].Sheet" & i & " from (" & myOpen & ") where ID >= " & arr(i)If i < ps Then myOpen1 = myOpen1 & " and ID<" & arr(i + 1)rs1.Open myOpen1 & " order by ID"Next Back...
怎样用VB6.0把Listview中查询到的数据导入到EXCEL
1,打开access,点击工具栏的外部数据,点击excel。2,点击浏览,选择要导入的excel表格,选好后点击确定,在获取外部数据中点击确定。3,点击确定后会弹出导入数据包向导框,点击下一步,然后到另一个步骤。4,点击下一步后,再点击下一步,然后在框里输入要导入的数据表的名称,最后点击完成就可以了。
VB6.0 怎么连接access已经建立好的表格?请在每条语句后面加上注解,小弟...
Private Sub Form_Load()'打开数据库 Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\实验平台\\数据库\\留言板.mdb"End Sub '增加记录 Private Sub Command1_Click()Dim sql As String Set Recordset = Nothing sql = "select * from liuyan" 'SQL查询语句 Recordset.Open sql, ...
VB6.0的DATAGRID数据导出EXCEL
Conn.Execute "insert into [excel 8.0;database=" & Filepath & "].[S_Line_GE$] select 回路编号,顶点序号,东坐标,北坐标,高程,回路电压,回路导线,里程,转角,方位角,控制区段,杆塔编号,杆塔型号 from S_Line_GE order by 回路编号,里程"'将库中的数据导出到刚刚建立的表中。看上去多...
vb6.0中如何把EXCEL表格中的数据导入到access库中
Public Sub 将数据库记录数据全部导入到excel工作表ADO之二() Dim myData As String, myTable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer ActiveSheet.Cells.Clear '清除工作表的全部数据 myData = ThisWorkbook.Path & "\\学生成绩管理.mdb" '指定数...
VB6.0编程,想把ACCESS07数据库【ABC】中表1中的所有内容都复制到同一数...
Private Sub Form_Load()cn.Open "provider=microsoft.jet.oledb.4.0;data source=" + App.Path + "\\数据库名.mdb" + ""sql = "select * from 表名"rs1.Open sql, cn rs1.MoveFirst Do While Not rs1.EOF Combo1.AddItem rs1("字段名")rs1.MoveNext Loop rs1.Close End Sub 如...
怎样才能在VB6.0 中连接Access 2003数据库
一:建立一个Access型数据库,并在数据库中创建新表—即为附加表。通过设置 附加表的各属性值,使其与你所要访问的数据库建立链接关系。程序代码如下:DimDbasDatabase DimTdasNewTabledef SetDb=OpenDatabase("MYDB.MDB")Td.Connect="dBASE:DATABASE=c:\\DATADIR"Td.SourceTableName="AU THOR"Td....
如何在VB6.0里使用MSFlexGrid将Access中的数据导进来
向工程添加部件:microsoft hierarchical flexgrid control 然后用下面的代码同样能达到要求:Set Conn = CreateObject("ADODB.Connection")Set Reco = CreateObject("ADODB.Recordset")Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\\db1.mdb"Reco.CursorLocation = ...