C#:工作簿另存为保存,其中包含宏
下面是我复制工作表并将其另存为新文件的代码,它实际上在本地环境中不使用宏进行保存 Office 2003、Windows Server 2003 但在服务器中,它使用宏进行保存,这是我不想要的 环境是Office 2003、Windows Server 2008 C#: 请检查我的问题…并提出一些答案。C#:工作簿另存为保存,其中包含宏,c#,excel,windows-server-2008-r2,C#,Excel,Windows Server 2008 R2,下面是我复制工作表并将其另存为新文件的代码,它实际上在本地环境中不使用宏进行保存 Office 2003、Windows Server 2003 但在服务器中,它使用宏进行保存,这是我不想要的 环境是Office 2003、Windows Server 2008 C#: 请检查我的问题…并提出一些答案。 谢谢你这样记录你的异常情况: try { wBook = xCel.Workbooks.Open(ExcelPath); wSheet = (Ex
谢谢你这样记录你的异常情况:
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块,您将永远无法解决问题,因为您将永远无法找出哪里出了问题
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);
}
}
}