c#客户端运行我的程序后,程序写入的Excel工作表总是在打开excel时打开

时间:2014-02-20 18:21:35

标签: c# excel com

我创建了一个C#程序,它将XML文档作为输入,拖放到exe上。输出是我使用输入创建的xlsx工作表。 当我将exe给同事运行时,它按预期工作,并输出xlsx。但现在,当同事关闭excel并在计算机上的任何位置打开任何excel工作簿之后,从我的程序创建的xlsx始终打开。 这不会发生在我的电脑上。不知道怎么可能。在保存文件时是否有一些我缺少的设置?

public static void WriteFile2(string filePath, InitialData initialData)
    {
        var excelApp = new Application {Visible = false};

        var workbooks = excelApp.Workbooks;
        _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        var sheets = workbook.Worksheets;
        var worksheet = (_Worksheet) sheets.Item[1];
        worksheet.Name = "BGPM1000XX";
        excelApp.StandardFont = "Arial";
        excelApp.StandardFontSize = 8;
/*Skipping body of code, mostly writing to cells and formatting*/
        //Insert Cover sheet tab

        var newWorksheet = (Worksheet) excelApp.Worksheets.Add();

        newWorksheet.Name = "COVER SHEET";
/*Skipping body of code, mostly writing to cells and formatting*/


        try
        {
            //TODO: make png path generic
#if DEBUG
             var fileName = @"..\..\..\ExternalReferences\logo.png";
            var newname = Path.GetFullPath(fileName);

           newWorksheet.Shapes.AddPicture(
                newname,

                MsoTriState.msoFalse, MsoTriState.msoCTrue, 280, 5, 55, 45);
#else

            newWorksheet.Shapes.AddPicture(
               @"logo.png",

               MsoTriState.msoFalse, MsoTriState.msoCTrue, 280, 5, 55, 45);
#endif
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
        }


        workbook.Saved = true;
        var version = 1;
        var inUse = true;
        var extension = Path.GetExtension(filePath);
        while (inUse)
        {
            inUse = IsFileInUse(filePath);
            if (!inUse)
                break;
            filePath = filePath.Substring(0, filePath.Length - extension.Length) + "_V" + version + ".xlsx";
            version++;
        }
        excelApp.DisplayAlerts = false;
        //workbook.SaveAs(filePath);
        workbook.SaveAs(filePath, XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
        excelApp.UserControl = false;
        excelApp.Quit();
    }

2 个答案:

答案 0 :(得分:0)

两件事

  1. 在致电Close之前,请致电Quit该文件。
  2. 将整个Excel代码放入try catch中,并将此命令作为finally命令释放com对象。

    System.Runtime.InteropServices.Marshal.ReleaseComObject( excelApp );

  3. 我的建议来自我的博客文章C# Open Word Documents using Visual Studio 2010 and .Net 4,该文章提供了使用互操作的示例。

答案 1 :(得分:0)

如果您的代码中存在异常,请确保终止该进程。处理它的一个好方法是使用try..catch块。调用异常后,请调用以下内容:

public void KillProcess()
{
    _workBook.Close();
    _workBooks.Close();
    _excelApp.Quit();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    Marshal.FinalReleaseComObject(_workSheets);
    Marshal.FinalReleaseComObject(_workBook);
    Marshal.FinalReleaseComObject(_workBooks);
    Marshal.FinalReleaseComObject(_excelApp);
}

此外,要清理以前的所有EXCEL任务,请重新启动计算机。