Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I am trying to read the cells in excel 2019 and after the reading the cell become empty.

???
I have Microsoft.excel 16.0 object library with excel 2019......

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace spark
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();

        }

private void button38_Click(object sender, EventArgs e)
        {
             Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

             if (xlApp == null)
             {
                 MessageBox.Show("Excel is not properly installed!!");
                 return;
             }
             Excel.Workbook xlWorkBook;
             Excel.Worksheet xlWorkSheet;
             Excel.Range range;
             object misValue = System.Reflection.Missing.Value;
             xlWorkBook = xlApp.Workbooks.Add(misValue);
             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
             range = xlWorkSheet.UsedRange;

            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {   InitialDirectory = @"D:\",
                Title = "Browse XLS Files",
                CheckFileExists = true,
                CheckPathExists = true,
                DefaultExt = "xls",
                Filter = "xls files (*.xls)|*.xls",
                FilterIndex = 2,
                RestoreDirectory = true,
                ReadOnlyChecked = true,
                ShowReadOnly = true
            };
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;
                if (filename != "")
                {
                    try
                    {  //  String filename = DialogResult.ToString();
                        var excelApp = new Excel.Application();
                        excelApp.Visible = true;
                       // excelApp.Visible = false;
                        excelApp.Workbooks.Open(filename);

                    }
                    catch (Exception ew)
                    {
                        MessageBox.Show("Errror:" + ew.ToString());
                    }
                }
            }
            var tt = Convert.ToString(xlWorkSheet.Cells[1, 1]);
            textBox1.Text = tt.ToString();
            MessageBox.Show(textBox1.Text.ToString()); 
               
            //textBox1.Text = (string)((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]).Value;

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();
        }
Posted
Comments
[no name] 9-Jun-21 21:14pm    
You have 2 Excel app instances and are referencing the wrong one.
Maciej Los 10-Jun-21 1:49am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900