首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >System.NullReferenceException: Excel C#

System.NullReferenceException: Excel C#
EN

Stack Overflow用户
提问于 2020-11-28 07:03:34
回答 1查看 45关注 0票数 0

我已经编写了一个处理打开、写入和关闭Excel文件的类。

Excel类中的方法正由另一个调用调用。当我第一次调用addDatatoExcel方法时,一切都正常,但是当我再次调用它时,我得到一个"System.NullReferenceException: Excel C#“。VB说我正在传递一个空对象。我知道错误是什么,我只是想不出如何防止它。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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类中的函数的方法。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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。这是因为我没有在每次调用AddDatatoExecl();之间保存Excel文件吗?

第一次调用函数

第二次调用函数

我是C#的新手,所以请具体一点。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-28 07:09:18

您只在第一次打开Excel:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// Each time, you call this:
ExcelFile _Excel = new ExcelFile();
...
//The first time, this is called
if (Excelcounter == 0)
{
       _Excel.openExcel();
       Excelcounter++;
}

下一次调用AddTextToLabel函数时

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 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)

将您的代码更改为:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  // 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();
                }


            }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65047249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文