【问题标题】:Windows explorer force excel to importWindows 资源管理器强制 excel 导入
【发布时间】:2014-03-24 10:01:06
【问题描述】:

我想在我的 Windows 资源管理器 (Windows 7) 中添加一个上下文菜单项,以便将所选文件导入 excel 并启动导入对话框。

我设法在本指南之后添加了条目: http://www.howtogeek.com/107965/how-to-add-any-application-shortcut-to-windows-explorers-context-menu/

但是当它打开文件时,会跳过导入对话框并在电子表格中打开文件。

编辑:我搜索了 exe 开关,但我发现的那些对此毫无用处! EDIT2:ElectricLlama 询问的更多背景

额外背景: 需要将大量文本文件导入excel。不是 CSV 目前它们的宽度是固定的,但它们肯定会在不久的将来发生变化。 我打开它们来转换它们并添加一些额外的信息作为列。 程序 X 生成一个带有空格的表格 ----> Excel 格式化此表格并添加信息 ----> 程序 Y 直接打开 excel 文件

【问题讨论】:

  • 您可以构建一个使用 Excel 对象模型的 VBScript 来完成所有这些工作。如果您有兴趣,请回帖。
  • 是的,我会对 VB 脚本感兴趣!是通过windows调用还是在excel里面调用?
  • 它会从 Windows 调用 - 让我做一些调查并回复你。
  • 似乎没有一种编程方式可以使导入对话框出现。所有文件都一样吗?它们是固定宽度吗?您是打开它们进行转换、编辑它们并以不同的格式保存,还是只是查看它们?
  • 编辑回复,ElectricLlama!

标签: windows excel


【解决方案1】:

所以我在 Windows 注册表中选择了一个选项(使用 regedit),让它启动处理解析和调用 Excel.exe 的 python 脚本。文件类型是没有扩展名的文件,python 在 PATH windows 变量中。 regedit 导出也会转义引号(如果您想知道的话):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\import with excel]
@="Convert and Open with Excel"

[HKEY_CLASSES_ROOT\*\shell\import with excel\command]
@="python \"path to python script\" \"%1\""

python 脚本非常杂乱,非常适合我的需求,但这里是:

import csv, os, sys

#If there are no arguments stop everything
if len(sys.argv) > 0:
    file = sys.argv[1]
else: sys.exit()

fileout = file + ".csv"

data = open(file, "r")
open(fileout, 'w').close() #cleans the file

dataout = open(fileout, "a")

# using the python csv lib, please look for info on it for further explanation
with open(fileout, 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, dialect='excel')

# I needed a special handling for the first line, that's why I used the i < 2
    i = 1
    for line in data:
        line = line.split()
        lineout = [] # clean the list after each line is parsed
        if i < 2: lineout.append("")
        for item in line:
             if item != "\"": lineout.append(item.replace("\"", ""))
        spamwriter.writerow(lineout)

        i += 1
data.close()
dataout.close()

# launches excel.exe with the fileout argument
os.system("start Excel.exe \""+ fileout + "\"")

然后,我可以根据需要在 excel 中打开 csv 文件。这种方式实际上比在 Excel 导入窗口中手动选择每个选项要快。

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    相关资源
    最近更新 更多