时间烙下的记忆 交给时间

VBS读Excel文件

上一篇 / 下一篇  2009-08-31 16:50:00 / 个人分类:VBS

Option Explicit

Dim xlApp,xlFile,xlsheet,xlsheet2 'Application,ExcelFile,Sheet 因此需要创建三个对象实现对Excel表格的操作
Dim iRowCount,iLoop,jLoop,numAdd(4),iColumnCount,record

Set xlApp=CreateObject("Excel.Application")

Set xlFile=xlApp.Workbooks.Open("F:\VBS\readxl.xlsx")'通过xlApp对象打开一个Excel应用程序,并且返回一个对象给xlFile

Set xlsheet=xlFile.Sheets("Sheet1") '通过xlFile对象的Sheets属性来操作一个sheet
Set xlsheet2=xlFile.Sheets("Sheet2")


iRowCount=xlSheet.usedRange.Rows.Count'通过sheet对象的属性usedRange.Rows属性来获取已经使用的行,而Count方法得到具体的值
iColumnCount=xlSheet.usedRange.Columns.Count

For iLoop=1 to iRowCount
    For jLoop=1 to iColumnCount

    numAdd(jLoop)=xlSheet.Cells(iLoop,jLoop)'通过sheet对象的Cells属性来操作sheet的行列内容
    xlSheet2.Cells(iLoop,jLoop)=numAdd(jLoop)
    Next
    record=Join(numAdd," ")'Join 函数用来数组的内容 Join(Myarray,"") Filter函数过滤字符串数组 Filter(Myarray,"n")

    MsgBox record
Next

xlFile.Save
xlFile.Close
xlApp.Quit

Set xlSheet=Nothing
Set xlSheet2=Nothing
Set xlFile=Nothing
Set xlApp=Nothing


TAG:

 

评分:0

我来说两句

Open Toolbar