简体   繁体   中英

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. What a mess..

And it can be on xlWorkbook = xlApp.Workbooks.Open(path); or 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.

Here's the method calling CsvParcoursVisuCrea in my soft crashing :

    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().

Hope its clear, its very long i'm sorry.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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