我正在打开一个xlsx文件并将每个工作表保存到一个csv文件中.
以下是保存的相关代码:
int i=0;
foreach (Excel.Worksheet s in app.ActiveWorkbook.Sheets)
{
s.Select(true); // Error here
String outfile = outputpath + "("+i+")" + outputfilename + ".csv";
wkb.SaveAs(outfile, Excel.XlFileFormat.xlCSVMSDOS);
++i;
}
Run Code Online (Sandbox Code Playgroud)
输出文件名或路径没有问题,输出文件不存在.它保存前两张然后崩溃.我试着用4张不同的输入文件,它工作得很好,所以它与输入文件有关.
例外:
System.Runtime.InteropServices.COMException was unhandled
HResult=-2146777998
Message=Exception from HRESULT: 0x800AC472
Source=ExcelXlsx2Csv
ErrorCode=-2146777998
StackTrace:
at Microsoft.Office.Interop.Excel._Worksheet.Select(Object Replace)
at ExcelXlsx2Csv.Program.Main(String[] args) in c:\Users\Edward\Documents\Visual Studio 2013\Projects\ExcelXlsx2Csv\ExcelXlsx2Csv\Program.cs:line 109
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Run Code Online (Sandbox Code Playgroud)
任何提示赞赏!
mbm*_*ura 13
在我的情况下,抛出了异常,因为我的excel Interop工具显示了一个模态对话框(与过期的许可证密钥相关 - 对我很羞耻).如果我关闭对话框(这是在后台显示),然后打在Visual Studio"继续",该方案是能够连接到xlsx档案成功地retrive数据.
我认为这是一个具有约束力的问题,我只是用以下内容包围每个调用(SaveAs、Select):
bool failed = false;
do
{
try
{
// Call goes here
failed = false;
}
catch (System.Runtime.InteropServices.COMException e)
{
failed = true;
}
System.Threading.Thread.Sleep(10);
} while (failed);
Run Code Online (Sandbox Code Playgroud)