none
Exception throw when exexute C# with Excel Late Binding RRS feed

  • 問題

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System;
    using System.Reflection;

    namespace TestingExcel5
    {
        public partial class Form1 : Form
        {
            const int xlAutomatic = -4105;
            const int xlManual = -4135;
            const int xlUpward = -4171;
            const int xlWait = 2;
            object oApp;
            object oBook;
            object oBooks;
            object oSheets;
            object oSheet;
            object objRange_Late;
            object oCharts;
            object oChart;
            object oPoints;
            object oPoint;
            object oAxisX;
            object oAxisY;
            object oCursor;
            object oRange;
            object[] Parameters;

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {


                // Get the class type and instantiate Excel.
                Type objClassType;
                objClassType = Type.GetTypeFromProgID("Excel.Application");
                oApp = Activator.CreateInstance(objClassType);

                //Get the workbooks collection.
                oBooks = GetProperty(oApp, "Workbooks");

                //Add a new workbook.
                Parameters = new Object[1];
                Parameters[0] = "D:\\Book1.xls"; // System.Windows.Forms.Application.StartupPath + @"\EndoTherm.xlt";
                oBook = InvokeMethod(oBooks, "Add", Parameters);

                //Get the worksheets collection.
                oSheets = GetProperty(oBook, "Worksheets");

                //Get the first worksheet.
                /*
                oSheet = GetProperty(oSheets, "Item", "Temp");
                SetProperty(oApp, "Calculation", xlManual);
                 */
                SetProperty(oApp, "Visible", true);
                /*
                oCursor = GetProperty(oApp, "Cursor");
                SetProperty(oApp, "Cursor", xlWait);
                SetProperty(oApp, "UserControl", false);
                */
            }

            public void Cell(int Row, int Col, object Value)
            {
                oRange = GetProperty(oSheet, "Range", (char) Col + Row.ToString(), Missing.Value);
                SetProperty(oRange, "Value", Value);
            }

            #region private Wrappers
            private void SetProperty(object obj, string sProperty, object oValue)
            {
                object[] oParam = new object[1];
                oParam[0] = oValue;
                obj.GetType().InvokeMember(sProperty, BindingFlags.SetProperty, null, obj, oParam);
            }
            private object GetProperty(object obj, string sProperty, object oValue)
            {
                object[] oParam = new object[1];
                oParam[0] = oValue;
                return obj.GetType().InvokeMember
                    (sProperty, BindingFlags.GetProperty, null, obj, oParam);
            }
            private object GetProperty(object obj, string sProperty, object oValue1, object oValue2)
            {
                object[] oParam = new object[2];
                oParam[0] = oValue1;
                oParam[1] = oValue2;
                return obj.GetType().InvokeMember
                (sProperty, BindingFlags.GetProperty, null, obj, oParam);
            }
            private object GetProperty(object obj, string sProperty)
            {
                return obj.GetType().InvokeMember
                (sProperty, BindingFlags.GetProperty, null, obj, null);
            }
            private object InvokeMethod(object obj, string sProperty, object[] oParam)
            {
                return obj.GetType().InvokeMember
                (sProperty, BindingFlags.InvokeMethod, null, obj, oParam);
            }
            private object InvokeMethod(object obj, string sProperty, object oValue)
            {
                object[] oParam = new object[1];
                oParam[0] = oValue;
                return obj.GetType().InvokeMember
                (sProperty, BindingFlags.InvokeMethod, null, obj, oParam);
            }
            #endregion
        }

    }

    Exception throw in those code highlighted in Bold:
    System Null Exception: {"並未將物件參考設定為物件的執行個體"}




    Please help
    Thanks

    2009年7月6日 下午 01:42