下面是我的代码,我复制工作表并将其保存为新文件,它实际上在没有宏的情况下保存在我的本地环境中 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 {}
}
请检查我的问题......并提出一些答案。 感谢
答案 0 :(得分: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
并查看出现了什么问题,这将有助于您诊断问题。使用空的挡块,你永远无法解决问题,因为你永远无法弄清楚出了什么问题。