在quicker的c#代码中无法获取到 COM ,但是在vs中可以正常运行,是什么原因导致。quicker和wps安装的都是64位程序。
测试动作https://getquicker.net/Sharedaction?code=db3c3bae-aec1-4b9b-6204-08de3f869ab9
在quicker中运行动作得到的结果是:
visual studio的代码如下:
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ConsoleApp13
{
internal class Program
{
static void Main(string[] args)
{
Type appType = null;
object excelApp = null;
object workbooks = null;
object workbook = null;
object worksheet = null;
object range = null;
string usedProgId = null;
try
{
appType = Type.GetTypeFromProgID("ET.Application");
if (appType != null)
{
usedProgId = "ET.Application";
}
else
{
appType = Type.GetTypeFromProgID("Ket.Application");
if (appType != null)
{
usedProgId = "Ket.Application";
}
else
{
appType = Type.GetTypeFromProgID("Excel.Application");
if (appType != null)
{
usedProgId = "Excel.Application";
}
}
}
if (appType == null)
{
Console.WriteLine("❌ 未找到 Excel 或 WPS 应用程序");
return;
}
string appName;
if (usedProgId == "ET.Application" || usedProgId == "Ket.Application")
{
appName = "WPS表格";
}
else if (usedProgId == "Excel.Application")
{
appName = "Excel";
}
else
{
appName = "未知应用程序";
}
Console.WriteLine("✅ 使用应用程序: " + appName + " (ProgID: " + usedProgId + ")");
excelApp = Activator.CreateInstance(appType);
excelApp.GetType().InvokeMember("Visible",
BindingFlags.SetProperty, null, excelApp, new object[] { true });
excelApp.GetType().InvokeMember("DisplayAlerts",
BindingFlags.SetProperty, null, excelApp, new object[] { false });
string filePath = @"C:\Users\Desktop\a.xlsx";
if (!File.Exists(filePath))
{
Console.WriteLine("❌ 文件不存在: " + filePath);
Console.WriteLine("将创建新工作簿...");
workbooks = excelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, excelApp, null);
workbook = workbooks.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod, null, workbooks, null);
}
else
{
Console.WriteLine("📂 正在打开受保护文件: " + filePath);
workbooks = excelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, excelApp, null);
workbook = workbooks.GetType().InvokeMember("Open",
BindingFlags.InvokeMethod, null, workbooks,
new object[] {
filePath, // Filename
Type.Missing, // UpdateLinks
false, // ReadOnly
Type.Missing, // Format
"123", // Password
Type.Missing, // WriteResPassword
Type.Missing, // IgnoreReadOnlyRecommended
Type.Missing, // Origin
Type.Missing, // Delimiter
Type.Missing, // Editable
Type.Missing, // Notify
Type.Missing, // Converter
false, // AddToMru
Type.Missing, // Local
Type.Missing // CorruptLoad
});
Console.WriteLine("✅ 文件已成功用密码打开");
}
// 获取活动工作表
worksheet = workbook.GetType().InvokeMember("ActiveSheet",
BindingFlags.GetProperty, null, workbook, null);
object sheetName = worksheet.GetType().InvokeMember("Name",
BindingFlags.GetProperty, null, worksheet, null);
Console.WriteLine("📄 当前工作表: " + sheetName);
Console.WriteLine("🔄 正在修改 A3 单元格...");
range = worksheet.GetType().InvokeMember("Range",
BindingFlags.GetProperty | BindingFlags.InvokeMethod,
null, worksheet, new object[] { "A3" });
object originalValue = range.GetType().InvokeMember("Value",
BindingFlags.GetProperty, null, range, null);
Console.WriteLine("📝 A3 单元格原值: " + (originalValue ?? "(空)"));
range.GetType().InvokeMember("Value",
BindingFlags.SetProperty, null, range, new object[] { "aaaa" });
Console.WriteLine("✅ A3 单元格已成功修改为: aaaa");
workbook.GetType().InvokeMember("Save",
BindingFlags.InvokeMethod, null, workbook, null);
Console.WriteLine("💾 文件已保存");
object newValue = range.GetType().InvokeMember("Value",
BindingFlags.GetProperty, null, range, null);
Console.WriteLine("🔍 验证: A3 单元格当前值: " + newValue);
Console.WriteLine("\n⚠️ 注意:应用程序将保持打开状态");
Console.WriteLine("现在您可以在 " + appName + " 中继续操作");
Console.WriteLine("工作簿和应用程序都不会自动关闭");
ShowOperationTips(appName);
excelApp.GetType().InvokeMember("DisplayAlerts",
BindingFlags.SetProperty, null, excelApp, new object[] { true });
try
{
excelApp.GetType().InvokeMember("Activate",
BindingFlags.InvokeMethod, null, excelApp, null);
}
catch
{
// 忽略错误
}
}
catch (COMException comEx)
{
if (comEx.HResult == unchecked((int)0x800A03EC))
{
Console.WriteLine("❌ 密码错误 (0x800A03EC)");
Console.WriteLine("请检查密码是否正确");
if (excelApp != null)
{
Console.WriteLine("应用程序仍保持打开,您可以手动操作");
}
}
else if (comEx.HResult == unchecked((int)0x800A13ED))
{
Console.WriteLine("❌ 文档已加密 (0x800A13ED)");
}
else
{
Console.WriteLine("❌ COM 错误: " + comEx.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("❌ 发生错误: " + ex.Message);
}
finally
{
if (range != null) Marshal.ReleaseComObject(range);
if (worksheet != null) Marshal.ReleaseComObject(worksheet);
if (workbook != null) Marshal.ReleaseComObject(workbook);
if (workbooks != null) Marshal.ReleaseComObject(workbooks);
}
Console.WriteLine("\n程序操作已完成,但 WPS/Excel 应用程序仍在运行");
Console.WriteLine("您可以继续在应用程序中操作,或手动关闭它");
Console.WriteLine("按任意键退出控制台程序...");
Console.ReadKey();
}
static void ShowOperationTips(string appName)
{
Console.WriteLine("");
Console.WriteLine("==================================================");
Console.WriteLine("💡 操作提示:");
Console.WriteLine("1. " + appName + " 窗口已在前台显示");
Console.WriteLine("2. 您可以继续编辑、保存、打印文档");
Console.WriteLine("3. 要关闭应用程序,请手动点击右上角的关闭按钮");
Console.WriteLine("4. 修改的内容已自动保存");
Console.WriteLine("==================================================");
}
}
}
visual studio的运行结果:
