尝试保存Excel时出现COMException

时间:2015-04-17 19:52:59

标签: c# excel-interop

我正在操作Excel并保存并且第一次保存效果很好,但是当它达到第二次保存时,我得到System.RunTime.InteropServices.COMException (0x800A03EC) Exception from HRESULT: 0x800A03EC

的com异常

我已验证路径有效,Excel仍处于打开状态,Excel仍然是活动工作簿。我该怎么做才能解决这个问题?

private void btn1_Click()
{
  Excel.Application oXL;
  Excel._Workbook oWB;
  Excel._Worksheet oWS;  
  string SaveDest = "C:\\ExcelFiles\\Reviewed\\";
  string EmailToSup = "C:\\Ready\\";
  string localdir = "C:\\ExcelFiles\\";
  var cFiles = Directory.EnumerateFiles(localdir, "*.xlsx", SearchOption.TopDirectoryOnly);
  foreach (string alpha in cFiles)
  {
    oWB = (Excel._Workbook)(oXL.Workbooks.Open(alpha));
    oWB = oXL.ActiveWorkbook;
    oWS = (Excel._Worksheet)(oWB.ActiveSheet);
    try { oWB.RefreshAll(); }
    catch { }
    oWB = oXL.ActiveWorkbook;
    oWB.SaveAs(SaveDest + Path.GetFileNameWithoutExtension(oXL.ActiveWorkbook.Name));
    oWB = oXL.ActiveWorkbook;
    try { oWB.SaveAs(EmailToSup + oXL.ActiveWorkbook.Name); }
  }
}

0 个答案:

没有答案