120316 VBS excel:创建 写入 读取 数组:赋值 读取

'=======================================================================
'excel:创建 写入 读取 数组:赋值 读取
'=======================================================================
'创建excel的对象
Set ExcelObj = CreateObject("Excel.Application")

'指定一个execl
aatype="E:\templet.xlsx"

'打开excel
ExcelObj.Workbooks.Open(aatype)

'设置操作的工作表
Set NewSheet = ExcelObj.Sheets.Item(1)

values1 =0
values2 =0

'写入excel sheet
For  i = 0 TO 9
                     NewSheet.Cells(i+1,1).Value =values1
                     NewSheet.Cells(i+1,2).Value =values2
                     values1 = values1 + 1
                     values2 = values2 + 1
Next

'excel赋值到数组
Dim arrayDemo(10,10)
For j = 0 to 9
    For k = 0 to 9
        arrayDemo(j,k)=NewSheet.Cells(j+1,k+1).value
    Next
Next

'数组赋值到excel
Set NewSheet2= ExcelObj.Sheets.Item(2)
For j = 0 to 9
    For k = 0 to 9
        NewSheet2.Cells(j+1,k+1).value =arrayDemo(j,k)
    Next
Next

'保存excel数据
ExcelObj.ActiveWorkbook.Saveas "E:\templet.xlsx"
ExcelObj.Quit
Set ExcelObj = Nothing

Dim filePath
filePath  = "E:\templet3.xlsx"
createExcel filePath

'**************************************
'   根据路径和文件名称创建excel
'   filePath 路径&文件名 如 E:\temp.xlsx
'**************************************
sub createExcel(filePath)  
   dim excel
   set excel = createobject("excel.application")
   excel.Workbooks.Add
   msgbox filePath
   excel.Save filePath
end Sub

你可能感兴趣的:(每日记录)