当路径明显存在时获取 COM class factory Path not found 异常
Getting a COM class factory Path not found exception when the path clearly exists
我正在尝试使用 Microsoft.Office.Interop.Excel 库将 csv 转换为 xlsx。
这是我正在使用的功能
public static string ConvertToXlsx(string _sFilePath)
{
string _newFilePath = _sFilePath.Replace("csv", "xlsx");
Application app = new Application();
Workbook wb;
wb = app.Workbooks.Open(_sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
if (File.Exists(_newFilePath))
return "";
wb.SaveAs(_newFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close();
app.Quit();
return _newFilePath;
}
我在函数参数中获取路径的方法是使用 OpenFileDialog class
OpenFileDialogue openDialog = new OpenFileDialog();
DialogResult response = openDialog.ShowDialog();
if (response == DialogResult.Cancel)
return;
string convertedFileName = convertToXlsx(openDialog.FileName);
错误抛出在 app.Workbooks.Open()
貌似当它试图访问 OpenFileDialog
提供的文件路径时
这是我收到的包含完整调用堆栈的错误
System.IO.DirectoryNotFoundException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070003 The system cannot find the path specified. (Exception from HRESULT: 0x80070003).
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at ConvertToXlsx(String _sFilePath)
有问题的文件路径直接来自文件资源管理器,因此它必须存在。路径为C:\temp\example.csv.
有趣的是,这甚至不会发生在所有用户身上。对于许多人来说,它完全按照预期工作,所以这里会发生什么?
尝试在系统上安装Excel (MS Office) 或修复它。看来问题不在于要打开的文件,而在于 Excel 本身。请记住,您必须在尝试自动化的系统上安装 Excel。
确保磁盘上存在这样的文件并且所有用户都可以访问。我注意到以下错误消息:
80070003 The system cannot find the path specified. (Exception from HRESULT: 0x80070003).
不清楚在对话框中选择了什么文件(路径)window。注意,您需要选择一个本地文件。 Excel 可能无法访问网络共享,在这种情况下,您需要将文件复制到本地,然后才使用传递本地文件路径的 Workbooks.Open
方法。
您可能还会发现以下类似主题很有帮助:
我正在尝试使用 Microsoft.Office.Interop.Excel 库将 csv 转换为 xlsx。 这是我正在使用的功能
public static string ConvertToXlsx(string _sFilePath)
{
string _newFilePath = _sFilePath.Replace("csv", "xlsx");
Application app = new Application();
Workbook wb;
wb = app.Workbooks.Open(_sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
if (File.Exists(_newFilePath))
return "";
wb.SaveAs(_newFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close();
app.Quit();
return _newFilePath;
}
我在函数参数中获取路径的方法是使用 OpenFileDialog class
OpenFileDialogue openDialog = new OpenFileDialog();
DialogResult response = openDialog.ShowDialog();
if (response == DialogResult.Cancel)
return;
string convertedFileName = convertToXlsx(openDialog.FileName);
错误抛出在 app.Workbooks.Open()
貌似当它试图访问 OpenFileDialog
这是我收到的包含完整调用堆栈的错误
System.IO.DirectoryNotFoundException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070003 The system cannot find the path specified. (Exception from HRESULT: 0x80070003).
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at ConvertToXlsx(String _sFilePath)
有问题的文件路径直接来自文件资源管理器,因此它必须存在。路径为C:\temp\example.csv.
有趣的是,这甚至不会发生在所有用户身上。对于许多人来说,它完全按照预期工作,所以这里会发生什么?
尝试在系统上安装Excel (MS Office) 或修复它。看来问题不在于要打开的文件,而在于 Excel 本身。请记住,您必须在尝试自动化的系统上安装 Excel。
确保磁盘上存在这样的文件并且所有用户都可以访问。我注意到以下错误消息:
80070003 The system cannot find the path specified. (Exception from HRESULT: 0x80070003).
不清楚在对话框中选择了什么文件(路径)window。注意,您需要选择一个本地文件。 Excel 可能无法访问网络共享,在这种情况下,您需要将文件复制到本地,然后才使用传递本地文件路径的 Workbooks.Open
方法。
您可能还会发现以下类似主题很有帮助: