Home > front end >  How to fix this "Cannot implicitly convert type 'object' to 'Microsoft.Office.In
How to fix this "Cannot implicitly convert type 'object' to 'Microsoft.Office.In

Time:10-06

I am opening a CSV file for reading. And I got this error:

Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel.Worksheet'.

For some reason, this one project has this error, while my other projects that has to open CSV files never had this error.

What did I do wrong?

my code:

    public partial class Form1 : Form
    {
        //Define Excel data
        _Application importExcelToDataGridViewApp;
        _Workbook importExcelToDataGridViewWorkbook;
        _Worksheet importExcelToDataGridViewWorksheet;
        string position;
        Microsoft.Office.Interop.Excel.Range importExcelToDataGridViewRange;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "Browse for file";
            op.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments);
            op.Filter = "Excel CSV File(*.csv)|*.csv|";
            op.FilterIndex = 1;


            if (op.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    importExcelToDataGridViewApp = new Microsoft.Office.Interop.Excel.Application();

                    importExcelToDataGridViewWorkbook = importExcelToDataGridViewApp.Workbooks.Open(op.FileName);
                    importExcelToDataGridViewWorksheet = importExcelToDataGridViewWorkbook.ActiveSheet; <-- ERROR HERE
                    importExcelToDataGridViewRange = importExcelToDataGridViewWorksheet.UsedRange;

                    List<string> positionList = new List<string>();

                    for (int i = 2; i <= importExcelToDataGridViewRange.Rows.Count; i  )
                    {
                        string username= importExcelToDataGridViewWorksheet.Cells[i, 1].Value; 
                        int salary= (int)importExcelToDataGridViewWorksheet.Cells[i, 2].Value;
                        dataGridView1.Rows.Add(username, salary);

                    }
                }
                catch (Exception err)
                {
                    ErrorWindow eWindow = new ErrorWindow();
                    eWindow.label4.Text = err.GetType().ToString();
                    eWindow.errorDetails.Text = err.Message;
                    eWindow.ShowDialog();
                }
            }

            
        }
    }

CodePudding user response:

importExcelToDataGridViewWorkbook.ActiveSheet is an object type, not Excel.Worksheet type, so you should cast it manually:

importExcelToDataGridViewWorksheet = importExcelToDataGridViewWorkbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;

or

importExcelToDataGridViewWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)importExcelToDataGridViewWorkbook.ActiveSheet;

enter image description here

  • Related