明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 779|回复: 1

[【PyCAD】] pycad 读写EXCEL

  [复制链接]
发表于 2022-4-21 21:20 | 显示全部楼层 |阅读模式
本帖最后由 枫叶棋语 于 2022-4-21 21:26 编辑

具体参考Microsoft.Office.Interop.Excel用法,与查到的资料不一致的地方,读单元格数据用的是Value2.ToString()在这边卡了很久。网上查到的都是Value.ToString(),返回是<bound dispmethod Value>

from pycad.system import *
from pycad.runtime import *
efilename = None
@command()
def excel1(doc):
    import clr
    clr.AddReference('Microsoft.Office.Interop.Excel')

    from Microsoft.Office.Interop import Excel
    from System.Runtime.InteropServices import Marshal
    pfo = aced.PromptOpenFileOptions("选择excel文件")
    pfo.Filter = "Excel文件(*.xlsx)|*.xlsx"
    pfo.PreferCommandLine = acapp.GetSystemVariable("FILEDIA") == 0
    pfo.InitialDirectory = pye.extensionspath + "\\.scripts\\"
    global efilename
    pfo.InitialFileName = efilename
    pr = doc.Editor.GetFileNameForOpen(pfo)
    if pr.Status == aced.PromptStatus.OK:
        file= pr.StringResult
        print(file)
        ex=Excel.ApplicationClass()
        ex.Visible = True
        ex.DisplayAlerts = False
        wb=ex.Workbooks.Open(file)
        wsh=wb.Worksheets['Sheet3']
        wsh.Cells[1,4].Value="asdsada"
        str = wsh.Cells[1,4].Value2.ToString()
        print(str)



评分

参与人数 1金钱 +15 收起 理由
e2002 + 15 赞一个!

查看全部评分

发表于 2022-4-22 06:52 | 显示全部楼层
不错,很有参考价值
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-4 16:00 , Processed in 0.220814 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表