Type Conversion trouble, when assigning a cel...

来源:百度文库 编辑:神马文学网 时间:2024/04/29 22:17:46
 am trying to assign the value2 from a cell in a worksheet to a int variable.

The following code gets compiled, but when in debug, a type conversion problem ocurs.

I don't understand, what happens.

 

Cheers

Heinz

 

this is the code in workbook.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace TypenTest
{
    public partial class ThisWorkbook
    {
        public int NumberOfPeople;
        NamedRange nPeopleCell;

        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            nPeopleCell = Globals.Tabelle1.nPeople; // nPeople ist the name of a cell in Worksheet "Tabelle1"

            nPeopleCell.Change += new Excel.DocEvents_ChangeEventHandler(nPeopleCell_Change);
        }




        void nPeopleCell_Change(Excel.Range Target)
        {
            NumberOfPeople = (int) Globals.Tabelle1.nPeople.Value2 ; ERROR Message "invalid type conversion" in this line
            Excel.Range tmpCell = Globals.Tabelle1.get_Range("D12", missing);
            tmpCell.Value2 = "nPeople: " + NumberOfPeople.ToString();

        }

        private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region Vom VSTO-Designer generierter Code

        ///


        /// Erforderliche Methode für Designerunterstützung -
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        ///

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisWorkbook_Startup);
            this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
        }

        #endregion

    }
}

 

Answers

  • Sunday, September 19, 2010 5:47 PMlilalaser   0 Sign In to Vote

    I found a solution

     

    NumberOfPeople = System.Convert.ToInt32(Globals.Tabelle1.nPeople.Value2)