简体   繁体   English

Microsoft.Office.Interop.Excel 打开和工作表错误

[英]Microsoft.Office.Interop.Excel Open and Sheet error

I have a problem that i don't get.我有一个我不明白的问题。 Let me explain first how my soft works.让我先解释一下我的软是如何工作的。

I've develop 3 different software looping every minute, two of them are openning the same excel file but not at the same time, but one of those is crashing sometimes, it can be after 8 hour, after 2 days or after 1 hour.我每分钟开发了 3 个不同的软件循环,其中两个打开同一个 excel 文件但不是同时打开,但其中一个有时会崩溃,可能是 8 小时后、2 天后或 1 小时后。 What a mess..真是一团糟..

And it can be on xlWorkbook = xlApp.Workbooks.Open(path);它可以在xlWorkbook = xlApp.Workbooks.Open(path); or xlWorksheet = xlWorkbook.Sheets{1};xlWorksheet = xlWorkbook.Sheets{1};

Here's the part giving me an error :这是给我一个错误的部分:

    public Excel.Application xlApp = new Excel.Application();
    public Excel.Workbook xlWorkbook;
    public Excel._Worksheet xlWorksheet;
    public Excel.Range xlRange;

    public void CsvParcoursVisuCrea(string flux, double delay, string hour, string name, string date, int index)
    {
        DateTime dateconvert = DateTime.Parse(date);
        string path = domain + @"TCD_ParcoursVisu.xlsx";
        if (!path.IsFileOpen())
        {
            if (index == 0)
                xlWorkbook = xlApp.Workbooks.Open(path);

            if (xlWorkbook == null)
            {
                if (index == 0)
                    LogWriteToFile("Error in workbook, impossible to open", "ALERT : WorkBook error");
            }
            else
            {
                if (index == 0)
                    LogWriteToFile("TCD_ParcoursVisu.xlsx is now open", "Process : Creation TCD parcours");

                xlWorksheet = xlWorkbook.Sheets[1];
                xlRange = xlWorksheet.UsedRange;

                int rowCount = xlRange.Rows.Count;
                int colCount = xlRange.Columns.Count;

                if (rowCount <= 1)
                {
                    xlWorksheet.Cells[rowCount, 1] = "Flux";
                    xlWorksheet.Cells[rowCount, 2] = "Nom Parcours";
                    xlWorksheet.Cells[rowCount, 3] = "Date";
                    xlWorksheet.Cells[rowCount, 4] = "Heure";
                    xlWorksheet.Cells[rowCount, 5] = "Minute";



                }
                xlWorksheet.Cells[rowCount + 1, 1] = flux;
                xlWorksheet.Cells[rowCount + 1, 2] = name;
                xlWorksheet.Cells[rowCount + 1, 3] = dateconvert;
                xlWorksheet.Cells[rowCount + 1, 4] = hour;
                xlWorksheet.Cells[rowCount + 1, 5] = delay / 60;
            }
        }
        else
        {
            LogWriteToFile("Being used by another process", "Process : Parcours visu crea");
        }
    }

    return false;
}

Here's a screenshot of my last log line before crash这是我崩溃前的最后一条日志行的屏幕截图

这是我崩溃前的最后一条日志行的屏幕截图

I've launched it at 10AM.我已经在上午 10 点启动了它。

Here's the method calling CsvParcoursVisuCrea in my soft crashing :这是在我的软崩溃中调用 CsvParcoursVisuCrea 的方法:

    private void AllCsvCreation()
    {
        SumParcours();
        int[] tabMemorySum = IndexSum();

        for (int i = 0; i < parcObj.Date.Length; i++)
        {
                if (parcObj.Date[i] != null)
                {
                    CsvCreation(parcObj.Date[tabMemoryIndex[i]], parcObj.Hour[tabMemoryIndex[i]], parcObj.Exploitant, parcObj.Name[tabMemoryIndex[i]], tabMemorydelay[tabMemorySum[i] - 1].ToString(), parcObj.Fiability[tabMemoryIndex[i]].ToString(), parcObj.StrnmeDelayXystaXyendScore[tabMemoryIndex[i]], i);
                    CsvParcoursVisuCrea(parcObj.Exploitant, tabMemorydelay[tabMemorySum[i] - 1], parcObj.Hour[tabMemoryIndex[i]], parcObj.Name[tabMemoryIndex[i]], parcObj.Date[tabMemoryIndex[i]], i);
                }
        }

        //Cleanup
        GC.Collect();
        GC.WaitForPendingFinalizers();
        //Close and release
        xlApp.DisplayAlerts = false;
        xlWorkbook.Save();
        xlWorkbook.Close();
        xlApp.DisplayAlerts = true;
    }

CsvCreation and CsvParcoursVisuCrea aren't opening the same Csv, CsvCreation never crashed, i fill CsvCreation with AppendLine(). CsvCreation 和 CsvParcoursVisuCrea 没有打开同一个 Csv,CsvCreation 从未崩溃,我用 AppendLine() 填充 CsvCreation。

Hope its clear, its very long i'm sorry.希望它清楚,很长,对不起。

明白了,我所要做的就是处理文件当前被锁定的异常,似乎经过一些研究,Interop 有时会锁定,这就是我的软崩溃的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM