C#保存Excel文件的问题 本帖最后由 innerorouter 于 2009-08-15 13:19:49 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是excel进程没杀死的原因# wbook.Save(); # wbook.Close(true, null, null); # System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet); # System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook); # System.Runtime.InteropServices.Marshal.ReleaseComObject(app); # wsheet = null; # wbook = null; # app = null; # GC.Collect(); 这样试试 多谢LCL_data的建议!我试了一下,还是同样的问题,在执行到这些代码之前就已经抛出异常了。 执行之前你先看一下你的任务管理器里有没有excel进程,有的话先杀死在执行 excelWorkSheet.SaveAs(paths, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value)不过要看你引用的Excel是哪个版本了。。好像2000和2003,2007都不一样,要根据你的Excel版本号来写上面的我已经用很久了都没有错误 excelWorkSheet.SaveAs(paths, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value) CloseExcelApplication();//关闭Excel文件,释放对象; public void CloseExcelApplication() { try { //excelWorkBook.Saved = true; excelWorkBooks = null; excelWorkBook = null; excelWorkSheet = null; excelRange = null; if (excelApplication != null) { //ExcelApp.DisplayAlerts=fales excelApplication.DisplayAlerts = false; excelApplication.Workbooks.Close(); excelApplication.Quit(); excelApplication = null; //ReleaseAllRef(excelApplication);//Error } } finally { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } } 和excel进程的释放应该没关系异常贴出来IO错误原因很多 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); 注意红色顺序 异常如下:Message:不能访问“test.xls”。Source:"Microsoft Office Excel"StackTrace: 在 Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local) ... msdn上这么说的private void WorkbookSaveAs(){ if (this.FileFormat == Excel.XlFileFormat.xlWorkbookNormal) { this.SaveAs(this.Path + @"\XMLCopy.xls", Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); }} 如果是覆盖 直接 wbook.Save(); 就可以了 SaveAs 可以是另存为 不是覆盖。是创建一个新文件。比如,创建一个"D:\test.xls"。现在的问题是一走到SaveAs就抛出无法访问文件的异常,但是在"D:\"能找到一个叫做"test.xls"的空文件。我刚才尝试了一下把SaveAs那一行注释掉,然后一直走过System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)那一段,直到GC.Collect()都没有问题。到了System.Diagnostics.Process.Start(strName);文件被打开,但是提示说该文件处于锁定状态,正在被其他用户编辑。 xlSheet.SaveAs(strName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);我用vs2008试了没问题 关于combobox的SelectedIndexChanged事件 C# WPF DrawingContext 背景颜色 怎么在ListView中添加控件 switch问题 关于对话框 .net下,怎么导入包呀? c#如何将窗体的数据传输到网页中? 对你来说,算挑战吗?不妨进来看看!来者有分! 问几个关于C#的常识问题,我在学习中 用C#处理html 文件. 关于继承的一个小问题! 很意外新手交错数组排序,是怎么回事?
# wbook.Save();
# wbook.Close(true, null, null);
# System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
# System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
# System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
# wsheet = null;
# wbook = null;
# app = null;
# GC.Collect();
这样试试
public void CloseExcelApplication()
{
try
{
//excelWorkBook.Saved = true;
excelWorkBooks = null;
excelWorkBook = null;
excelWorkSheet = null;
excelRange = null;
if (excelApplication != null)
{
//ExcelApp.DisplayAlerts=fales
excelApplication.DisplayAlerts = false;
excelApplication.Workbooks.Close();
excelApplication.Quit();
excelApplication = null;
//ReleaseAllRef(excelApplication);//Error }
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
异常贴出来
IO错误原因很多
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
注意红色顺序
Message:不能访问“test.xls”。
Source:"Microsoft Office Excel"
StackTrace: 在 Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local) ...
private void WorkbookSaveAs()
{
if (this.FileFormat == Excel.XlFileFormat.xlWorkbookNormal)
{
this.SaveAs(this.Path + @"\XMLCopy.xls",
Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
}
}
SaveAs 可以是另存为
我刚才尝试了一下把SaveAs那一行注释掉,然后一直走过System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)那一段,直到GC.Collect()都没有问题。到了System.Diagnostics.Process.Start(strName);文件被打开,但是提示说该文件处于锁定状态,正在被其他用户编辑。