簡體   English   中英

System.NullReferenceException: Excel C#

[英]System.NullReferenceException: Excel C#

我編寫了一個 class 來處理打開、寫入和關閉 Excel 文件。

Excel class 中的方法被另一個調用調用。 當我第一次調用addDatatoExcel方法時,一切正常,但是當我再次調用它時,我得到一個“System.NullReferenceException:Excel C#”。 VB 說我正在傳遞一個 null object。 我知道錯誤是什么,我只是不知道如何防止它。

public class ExcelFile
{
    public static ExcelFile C1;
    private string excelFilePath = "‪‪C:\\Final.xlsx";
    private int rowNumber = 1; // define first row number to enter data in Excel

    Excel.Application excelApp;
    Excel.Workbook excelWorkbook;
    Excel.Worksheet excelWorksheet;
    int ExcelCounter = 0;
    int checker = 0;
    string str4 = "h";
    
    public   void openExcel()
    {
        excelApp = null;

        excelApp = new Excel.Application(); // create Excel App
        excelWorkbook = excelApp.Workbooks.Add();
        
        excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets.Add();
    }

    public void addDataToExcel(string str4)
    {
        excelWorksheet.Cells[5, 1] = str4;
                      
        ExcelCounter++;
    }

    public void closeExcel()
    {
        excelWorkbook.Close();
        excelApp.Quit();

        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorksheet);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorkbook);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

我希望能夠將數據添加到創建的第一個相同的 Excel 表中。

這是調用Excel class中的函數的方法。

public void AddTextToLabel(string str)
{
        ExcelFile _Excel = new ExcelFile();
        
        if (flag == 1) // Celsius 
        {
            //list Adding Function 1;                
            temp = Double.Parse(str);
            
            x++;
            list.Add(x, temp);
            zedGraphControl1.Invalidate();
            CreateChart(zedGraphControl1);
            //Example();
            
            if (Excelcounter == 0)
            {
                _Excel.openExcel();
                Excelcounter++;
            }
            
            _Excel.addDataToExcel(str);

            if (Excelcounter == 15)
            {
                _Excel.closeExcel();
            }
        }
}

我已經檢查過,傳遞給addDatatoExcel的變量str不是 null。 第二次調用 function 時,還有其他參數變為 null。 這是因為我沒有在每次調用AddDatatoExecl();

第一次致電 function

在此處輸入圖像描述

第二次致電 function

在此處輸入圖像描述

我是 C# 的新手,所以請具體說明。 謝謝

您只是第一次打開 Excel:

// Each time, you call this:
ExcelFile _Excel = new ExcelFile();
...
//The first time, this is called
if (Excelcounter == 0)
{
       _Excel.openExcel();
       Excelcounter++;
}

然后下次調用AddTextToLabel function

// A new instance is created
ExcelFile _Excel = new ExcelFile();

// The excel counter is not 0 so the  _Excel.openExcel() will not be called.
if (Excelcounter == 0)

將您的代碼更改為:

  // Now your excel file is an instance member
  // as is your Excelcounter 
  ExcelFile _Excel = new ExcelFile();
  public void AddTextToLabel(string str)
        {
       
            if (flag == 1) // Celcuis 
            {
                //list Adding Function 1;                
                temp = Double.Parse(str);
                
                x++;
                list.Add(x, temp);
                zedGraphControl1.Invalidate();
                CreateChart(zedGraphControl1);
                //Example();
                
                
                if (Excelcounter == 0)
                {
                    _Excel.openExcel();
                    Excelcounter++;
                }
                
                _Excel.addDataToExcel(str);

                if (Excelcounter == 15)
                {
                    _Excel.closeExcel();
                }


            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM