对象引用未设置为对象的实例(Excel生成问题)

时间:2014-05-27 16:05:08

标签: c# excel reference null crash

我有一个奇怪的问题。我在VS 2013中构建了这个应用程序。它很简单,我想将这个应用程序提供给其他人使用。当我构建它并在其他计算机上使用exe时,我收到此错误

Object reference not set to an instance of an object.

但它可以在我构建应用程序的计算机上运行。

以下是我用于将某些信息输出到Excel文件的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Company_Total_Calls_Project_v1._0._6
{
class ExcelModel
{
    #region Generate Excel Report

    //Creating Excel File Variables
    private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
    private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
    private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
    private static Microsoft.Office.Interop.Excel.Application oXL;

    public static void GenerateReport(int newInboundCalls, int newOutboundCalls, int newDropCalls, List<string> newDropTickets, string newUserName, string newCurrentDate)
    {

        string path2 = @"S:\Test Folder\Company Total Calls Excel Folder\";
        string tempHolder = "";
        try
        {
            //Creating new Excel Object
            oXL = new Microsoft.Office.Interop.Excel.Application();

            //Opening up existing Excel File
            mWorkBook = oXL.Workbooks.Open(path2 + "\\Company Total Calls Excel Folder\\Helpdesk Total Calls.xlsx");
            mWorkSheets = mWorkBook.Worksheets;

            //Selects the sheet you want. 
            //Grab current User name from the Program and then find it in the program Sheets of the Excel File
            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item(newUserName);

            //This gets the current range of the Sheet in excel. 
            Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;

            //Getting Drop Tickets from the List and adding to the string tempHolder
            foreach (string element in newDropTickets)
            {
                tempHolder += "#" + element.ToString() + " ";
            }

            //This just gets the columns and rows numbers.
            int colCount = range.Columns.Count;
            int rowCount = range.Rows.Count;

            //Increment Rowcount so I do not override an existing information         
            rowCount++;

            //This grabs the values from the GenerateReport Function
            mWSheet1.Cells[rowCount, "A"] = newCurrentDate.ToString();
            mWSheet1.Cells[rowCount, "B"] = newInboundCalls.ToString();
            mWSheet1.Cells[rowCount, "C"] = newOutboundCalls.ToString();
            mWSheet1.Cells[rowCount, "D"] = newDropCalls.ToString();
            mWSheet1.Cells[rowCount, "E"] = tempHolder.ToString();

            //Saves Workbook
            mWorkBook.Save();

            //Closes the Workbook
            mWorkBook.Close();

            //Excel Closes
            oXL.Quit();
        }
        catch
        {
            //Closes the Workbook
            mWorkBook.Close();

            //Excel Closes
            oXL.Quit();
            MessageBox.Show("No Name in Excel File");  
        }

        MessageBox.Show("Total Calls Report Generated", "Complete", MessageBoxButtons.OK, MessageBoxIcon.None);
    }
    #endregion
}
}

我不明白它为什么不起作用,它可以在我的计算机和其他一些计算机上运行,​​但不能在其他人的计算机上运行。

任何帮助都会很棒

谢谢

更新:

I get this Error when running the exe on a computer that has the issue
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at Company_Total_Calls_Project_v1._0._6.ExcelModel.GenerateReport(Int32 newInboundCalls, Int32 newOutboundCalls, Int32 newDropCalls, List`1 newDropTickets, String newUserName, String newCurrentDate)
   at Company_Total_Calls_Project_v1._0._6.Form1.GenerateReportFunction()
   at Company_Total_Calls_Project_v1._0._6.Form1.GenerateReportBtn_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
Company Total Calls Project v1.0.6
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/snoorizadeh/Desktop/Company%20Total%20Calls%20Project%20v1.0.6.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.DirectoryServices.AccountManagement
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.DirectoryServices.AccountManagement/v4.0_4.0.0.0__b77a5c561934e089/System.DirectoryServices.AccountManagement.dll
----------------------------------------
System.DirectoryServices
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.DirectoryServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.dll
----------------------------------------
System.DirectoryServices.Protocols
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.DirectoryServices.Protocols/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.Protocols.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml.Linq
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
Microsoft.CSharp
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll
----------------------------------------
Accessibility
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

0 个答案:

没有答案