Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

c# - Exception (HRESULT: 0x800AC472) when using Excel.Worksheet.Select after calling Excel.Workbook.SaveAs

I'm opening an xlsx file and saving each sheet into a csv file.

Here is the relevant code for the saving:

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;
}

There is no problem with the output file name or path, and the output file(s) do not exist. It saves the first two sheets then crashes. I tried with a different input file with 4 sheets and it worked perfectly, so it has to do with the input file.

The exception:

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:UsersEdwardDocumentsVisual Studio 2013ProjectsExcelXlsx2CsvExcelXlsx2CsvProgram.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: 

Any hints appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my case, the exception was been thrown because my excel Interop tools was displaying a modal dialog (associated with a expired licence key - shame on me). If I closed the dialog (which was been displayed in the background) and then hit 'continue' in Visual Studio, the program was able to connect to the xlsx file and retrive data succesfully.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...