我正在打开一个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;
}
输出文件名或路径没有问题,输出文件也不存在。它保存前两张床单,然后崩溃。我尝试了一个不同的输入文件和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:
任何暗示都很感激!
发布于 2014-06-09 15:42:11
在我的例子中,异常被抛出,因为我的excel Interop工具显示了一个模态对话框(与过期的许可证密钥相关联--这让我感到羞愧)。如果我关闭了对话框(该对话框显示在后台),然后在Visual中单击“继续”,程序就能够连接到xlsx文件并成功地检索数据。
https://stackoverflow.com/questions/20737432
复制相似问题