• Excel数据操作


    **********************************************************************
    1.打开已存在的excel文件,并使其可见
    ***************************************************
    Set excelApp = CreateObject("excel.application")
    excelApp.Visible = True  '使excel程序可见
    excelApp.Workbooks.Open"d: est.xls"  '打开d: est.xls文件
    Set excelApp = Nothing
    ***************************************************
    2.指定某个sheet为活动工作
    ****************************************************
    Set excelApp = CreateObject("excel.application")
    excelApp.Visible = True
    Set WorkBook = excelApp.Workbooks.Open("d: est.xls")
    oWorkBook.Worksheets("name").activate '设置name表为活动工作表
    Set excelApp = Nothing
    ****************************************************
    3.添加、删除工作表
    ****************************************************
    Set excelApp = CreateObject("excel.application")
    excelApp.Visible = True
    '不显示特定的警告和消息,当出现警告和消息时选择默认应答
    excelApp.DisplayAlerts = False
    Set WorkBook = excelApp.Workbooks.Open("d: est.xls")
    Set Sheet = oWorkBook.Worksheets.Add '添加新的工作表
    oSheet.name = "newcname" 将刚添加的工作表改名为newname
    oWorkBook.Worksheets(“Sheet3”).delete '删除表Sheet3
    oWorkBook.Save '保存
    oWorkBook.SaveAs "d: est1.xls" '另存为
    oWorkBook.Close '关闭工作表
    excelApp.Quit '退出excel
    Set Sheet = nothing
    Set WorkBook = nothing
    Set excelApp = Nothing
    ***************************************************
    4.获取某个指定表中的某个单元格的值
    ***************************************************
    Set excelApp = CreateObject("excel.application")
    excelApp.Visible = True
    Set WorkBook = excelApp.Workbooks.Open("d: est.xls")
    oWorkBook.Sheets("login").activate '设置login表为活动工作表
    cellValue = excelApp.Cells(1,1).value '显示第一行第一列的单元格中数据
    oWorkBook.Close
    excelApp.Quit
    Set WorkBook = Nothing
    Set excelApp = Nothing
    ***************************************************
    5.修改某个单元格的值或赋值
    ***************************************************
    Set excelApp = CreateObject("excel.application")
    excelApp.Visible = True
    excelApp.DisplayAlerts = False
    Set WorkBook = excelApp.Workbooks.Open("d: est.xls")
    oWorkBook.Worksheets("a").cells(5,5).value="中国" '给a表中的5行5列单元格赋值
    cellvalue = oWorkBook.Worksheets("a").cells(5,5).value '获取a表中5行5列单元格的值
    oWorkBook.Worksheets("Sheet1").cells(2,1).value= cellvalue '将值赋给sheet1表单元格
    oWorkBook.Save
    oWorkBook.Close
    excelApp.Quit
    Set WorkBook = Nothing
    Set excelApp = Nothing
    ***************************************************
    6.清除单元格数据
    ***************************************************
    Set excelApp = CreateObject("excel.application")
    excelApp.Visible = True
    Set WorkBook = excelApp.Workbooks.Open("d: est.xls")
    oWorkBook.Worksheets("a").cells(1,1).clearContents '清除数据
    oWorkBook.Save
    oWorkBook.Close
    excelApp.Quit
    Set WorkBook = Nothing
    Set excelApp = Nothing
    ***************************************************
    **********************************************************************

  • 相关阅读:
    poj 3621(最优比率环)
    bzoj 1497(最大权闭合子图)
    Dinic(模板 再错是不可能的 这辈子都不可能了)
    BZOJ 2038
    zoj 3822(概率dp)
    poj 3683(2-sat+拓扑排序)
    poj 2186(tarjan+缩点)
    hdu 5782(kmp+hash)
    hdu 6035(树形dp)
    Python爬取房屋租售信息
  • 原文地址:https://www.cnblogs.com/hgfg331/p/3216680.html
Copyright © 2020-2023  润新知