如何用vb操作excel的表格?谢谢!!
解决方案 »
- 郁闷了很久的一个问题,关于VB.Net中的TreeView
- 晕,网络编程版居然没人解决这个问题
- AcitveX 重新编译以后CLSID会变化吗??
- MSFlexGrid.TextMatrix
- 请问各位高手,如何在ocx控件中实现exe中的form_load方法
- 如何判断一个汉字所属的字符集!如属 如BIG5码还是属于GB码!
- 一个hwnd是不是只能RegisterHotKey一个热键?
- 我生成的exe文件在有的机器上不能用,请问为什么??
- 一个VB初学者..我很迷茫请大家指教.不胜感激!!
- 请问在vb中如何使一个access数据库实现多用户访问??请高手指点!!!!!!
- Flexgrid问题,谢谢!
- 我打开一幅图片,如何让图片框的大小与图片的大小一样,
http://support.microsoft.com/default.aspx?scid=kb;EN-US;278973
Dim xlsApp As Excel.Application
Dim wrdApp As Word.Application 只要相关的对象库已经被选择,在应用程序中进行对象变量的赋值是可能的。Microsoft Excel 8.0对象库是相对于Excel的,而 Microsoft Word 8.0 对象库是为Word服务的。 在VB的IDE环境中,从“工程”菜单中选择“引用”,可以看到系统可用的所有库列表。Private Sub Command1_Click()
Set xlsApp = Excel.Application
With xlsApp
'Show Excel
.Visible = True
'Create a new workbook
.Workbooks.Add
'Put text in to the cell that is selected
.ActiveCell.Value = "Hi"
'Put text into A3 regardless of the selected cell
.Range("A3").Value = "This is an example of connecting to Excel"
End With
End Sub
在上面的程序段中,我们在变量xlsApp中建立了一个对象,这样Excel就对用户可见了。当Excel象这样启动后,并不包含一个工作簿,所以必须创建或者执行打开操作。这里,我们建立了一个新的工作簿,然后,就可以操作其中的信息,或者打印,或者保存,或者你任意想做的事情。Private Sub Command2_Click()
'close the workbook
xlsApp.Workbooks.Close
'Close Excel
xlsApp.Quit
End Sub 上面这段代码执行关闭程序的功能。首先,关闭工作簿,这将出现一个提示对话框,询问用户是否想保存修改;然后,退出应用程序。Private Sub Command3_Click()
Set wrdApp = New Word.Application
With wrdApp
'Show Word
.Visible = True
'Create New Document
.Documents.Add
'Add text to the document
.ActiveDocument.Content.Text = "Hi"
.ActiveDocument.Content.Text = "This is a test example"
End With
End Sub 上面这段代码中,在变量wrdApp中设置引用Word程序的对象。同样,当Word按照这种方式启动后,不会包含一个文档,所以,必须执行建立或者打开操作。这里是建立了一个新文档,然后可以操作其中的信息了,打印、保存、发送邮件,等等... 但是,在Word文档中放置文本并非容易!特别是与Excel一起工作时。为了简单地在特定的地方放置文本,需要有一个book标记。这意味着,需要事先建立一个模板。Private Sub Command4_Click()
'Close the current document
wrdApp.ActiveDocument.Close
'Close Word
wrdApp.Quit
End Sub 上面这段代码的功能是关闭应用程序。首先,关闭当前文档,这时可能需要用户保存修改。然后,退出程序。Private Sub Form_Unload(Cancel As Integer)
'Clear the memory
Set xlsApp = Nothing
Set wrdApp = Nothing
End Sub 最后一段代码就是关闭VB应用程序。这是优秀程序员编程的好习惯。 Well I hope this brief tutorial is helpful. It does not touch on much of what you can do to the office applications once they're open, but should give you an idea of how to get started. 好了,简单的介绍到此结束。我希望能抛砖引玉,让你更加随意地操作Office应用程序!
Public Book As Excel.Workbook
Public Sheet As Excel.Worksheet
这是我程序代码的一部分。
Public Sub PrintExcel_query(Grid1 As MSFlexGrid, Grid2 As MSFlexGrid, startpageid As Integer, CopiesNum As Integer, printid As Integer, weekorbargain As Integer)
' On Error Resume Next
Dim i As Integer
Dim TotalRow As Integer
Dim Rows As Integer
Set Ap = New Excel.Application Set Book = Ap.Workbooks.Open(App.Path & "\ftgl.xls")
Rows = 0
Select Case printid
Case 0
'****************************************
'打印部件
'**************************************** Set Sheet = Book.Sheets("parts")
Sheet.Activate
Sheet.PageSetup.Orientation = xlLandscape
'打印周次
rs.Open "select F_bargain,F_otisweek from T_copyrount where F_otisweek='" & Trim(Frm_plan.Txt_otisweek.Text) & "'", cn
If Not rs.EOF Then Sheet.Cells(1, 1) = GB_OKText(rs("F_otisweek")) & "周"
rs.Close
'*********************************************************************************
'打印单台合同号
If weekorbargain = 1 Then
rs.Open "select F_bargain,F_otisweek from T_copyrount where F_otisweek='" & Trim(Frm_plan.Txt_otisweek.Text) & "'and F_bargain='" & Trim(Frm_plan.Grid1.TextMatrix(Frm_plan.Grid1.Row, 1)) & "'", cn
If Not rs.EOF Then Sheet.Cells(1, 8) = GB_OKText(rs("F_bargain"))
rs.Close
End If
'**********************************************************************************
'打开查询语句
For i = 1 To Grid2.Rows - 1
Rows = Rows + 1
Sheet.Cells(3 + i, 1) = i '顺序号
Sheet.Cells(3 + i, 2) = Grid2.TextMatrix(i, 2) '图号
Sheet.Cells(3 + i, 3) = Grid2.TextMatrix(i, 3) '名称
Sheet.Cells(3 + i, 4) = Grid2.TextMatrix(i, 4) '材料
Sheet.Cells(3 + i, 5) = Grid2.TextMatrix(i, 5) '单台量
Sheet.Cells(3 + i, 6) = Grid2.TextMatrix(i, 15) '工序
Sheet.Cells(3 + i, 7) = Grid2.TextMatrix(i, 19) '尺寸
Sheet.Cells(3 + i, 8) = Grid2.TextMatrix(i, 17) '备注
Next i
TotalRow = (Int((Rows - 2) / 14) + 1) * 14 + 3
Sheet.Range("A3:H" & TotalRow).Select
Ap.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Ap.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Ap.Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Ap.Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Ap.Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Ap.Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Ap.Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Ap.Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
' Sheet.PageSetup.CenterFooter = "第 &P+" & startpageid - 1 & " 页"
'打印预览
' Ap.Visible = True
' Sheet.PrintPreview
Sheet.PrintOut Copies:=CopiesNum, Collate:=True
Set Sheet = Nothing
Book.Close False
Set Book = Nothing
Ap.Quit
Set Ap = Nothing
MsgBox "数据已经送交打印", vbOKOnly + vbInformation, "打印"
别忘了引用,excel9.0,在工程-引用中。
Dim xlsApp As Excel.Application
Dim wrdApp As Word.Application 只要相关的对象库已经被选择,在应用程序中进行对象变量的赋值是可能的。Microsoft Excel 8.0对象库是相对于Excel的,而 Microsoft Word 8.0 对象库是为Word服务的。 在VB的IDE环境中,从“工程”菜单中选择“引用”,可以看到系统可用的所有库列表。Private Sub Command1_Click()
Set xlsApp = Excel.Application
With xlsApp
'Show Excel
.Visible = True
'Create a new workbook
.Workbooks.Add
'Put text in to the cell that is selected
.ActiveCell.Value = "Hi"
'Put text into A3 regardless of the selected cell
.Range("A3").Value = "This is an example of connecting to Excel"
End With
End Sub
在上面的程序段中,我们在变量xlsApp中建立了一个对象,这样Excel就对用户可见了。当Excel象这样启动后,并不包含一个工作簿,所以必须创建或者执行打开操作。这里,我们建立了一个新的工作簿,然后,就可以操作其中的信息,或者打印,或者保存,或者你任意想做的事情。Private Sub Command2_Click()
'close the workbook
xlsApp.Workbooks.Close
'Close Excel
xlsApp.Quit
End Sub 上面这段代码执行关闭程序的功能。首先,关闭工作簿,这将出现一个提示对话框,询问用户是否想保存修改;然后,退出应用程序。Private Sub Command3_Click()
Set wrdApp = New Word.Application
With wrdApp
'Show Word
.Visible = True
'Create New Document
.Documents.Add
'Add text to the document
.ActiveDocument.Content.Text = "Hi"
.ActiveDocument.Content.Text = "This is a test example"
End With
End Sub 上面这段代码中,在变量wrdApp中设置引用Word程序的对象。同样,当Word按照这种方式启动后,不会包含一个文档,所以,必须执行建立或者打开操作。这里是建立了一个新文档,然后可以操作其中的信息了,打印、保存、发送邮件,等等... 但是,在Word文档中放置文本并非容易!特别是与Excel一起工作时。为了简单地在特定的地方放置文本,需要有一个book标记。这意味着,需要事先建立一个模板。Private Sub Command4_Click()
'Close the current document
wrdApp.ActiveDocument.Close
'Close Word
wrdApp.Quit
End Sub 上面这段代码的功能是关闭应用程序。首先,关闭当前文档,这时可能需要用户保存修改。然后,退出程序。Private Sub Form_Unload(Cancel As Integer)
'Clear the memory
Set xlsApp = Nothing
Set wrdApp = Nothing
End Sub 最后一段代码就是关闭VB应用程序。这是优秀程序员编程的好习惯。 Well I hope this brief tutorial is helpful. It does not touch on much of what you can do to the office applications once they're open, but should give you an idea of how to get started. 好了,简单的介绍到此结束。我希望能抛砖引玉,让你更加随意地操作Office应用程序!
一人WORKBOOK
用第一个对像打开,然后把返回的值给第地个对像
操作第二个,就像是操作EXCEL文件一件,取值,有SHEET,RANG,CELL这些在帮助里面都有
set exbook=createobject("excel.workbook")
救急,大虾们!谢了!