枫叶棋语 发表于 2022-4-21 21:20:27

pycad 读写EXCEL

本帖最后由 枫叶棋语 于 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)



烟盒迷唇 发表于 2022-4-22 06:52:20

不错,很有参考价值
页: [1]
查看完整版本: pycad 读写EXCEL