0

下面是我复制工作表并将其保存为新文件的代码,它实际上在我的本地没有宏保存,我们的环境为 Office 2003,Windows Server 2003

但是在服务器中它用宏保存,我不想要环境是 Office 2003,Windows Server 2008

C#:

try
            {
                wBook = xCel.Workbooks.Open(ExcelPath);                
                wSheet = (Excel.Worksheet)wBook.Worksheets.get_Item(1);                    
                wSheet.Copy(Type.Missing, Type.Missing);                    
                wSheet = (Excel.Worksheet)wBook.Sheets[1];
                wSheet.SaveAs(ExcelCopyPath);
             }

            catch {}
            finally
            {                
                if (wBook != null)
                {                    
                    wBook.Close();
                    wSheet = null;
                    wBook = null;
                    Thread.Sleep(500);
                }
                if (Excel.ProcessID > 0)
                 {

                    Process pxCel = Process.GetProcessById(Excel.ProcessID);
                    pxCel.Kill();
                 }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();                
                try { Marshal.FinalReleaseComObject(wSheet);}
                catch {}                                        
                try { Marshal.FinalReleaseComObject(wBook);}
                catch {}                
            }

请检查我的问题...并提出一些答案。谢谢

4

1 回答 1

0

像这样记录您的异常:

try
{
    wBook = xCel.Workbooks.Open(ExcelPath);                
    wSheet = (Excel.Worksheet)wBook.Worksheets.get_Item(1);                    
    wSheet.Copy(Type.Missing, Type.Missing);                    
    wSheet = (Excel.Worksheet)wBook.Sheets[1];
    wSheet.SaveAs(ExcelCopyPath);
 }

catch(Exception ex) 
{
    using(var sw = new StreamWriter(@"C:\temp\mylog.txt", true))
    {
        sw.WriteLine("First catch block");
        sw.WriteLine(ex.Message);
    }
}
finally
{                
    if (wBook != null)
    {                    
        wBook.Close();
        wSheet = null;
        wBook = null;
        Thread.Sleep(500);
    }
    if (Excel.ProcessID > 0)
     {

        Process pxCel = Process.GetProcessById(Excel.ProcessID);
        pxCel.Kill();
     }

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();                
    try { Marshal.FinalReleaseComObject(wSheet);}
catch(Exception ex) 
{
    using(var sw = new StreamWriter(@"C:\temp\mylog.txt", true))
    {
        sw.WriteLine("Second catch block");
        sw.WriteLine(ex.Message);
    }
}                                     
    try { Marshal.FinalReleaseComObject(wBook);}
catch(Exception ex) 
{
    using(var sw = new StreamWriter(@"C:\temp\mylog.txt", true))
    {
        sw.WriteLine("Third catch block");
        sw.WriteLine(ex.Message);
    }
}              
}

你真的应该使用像 NLog 这样的日志框架,但这应该在短期内帮助你。这不会解决您的问题,但至少现在您将能够打开C:\temp\mylog.txt并查看问题所在,这将帮助您诊断问题。使用空的 catch 块,您将永远无法解决问题,因为您永远无法弄清楚出了什么问题。

于 2013-03-19T14:26:18.613 回答