none
Get values from Excel cell and print in textbox RRS feed

  • Question

  • Hi All!

    I'm attempting to write a small C# program to assist with automating my report writing process.

    The logic of the program is to
    - Tick a checkbox from checkedlistbox which corresponds to a specific excel spreadsheet cell
    - print the text from that cell into a textbox (likewise, remove it if the checkbox is unticked)

    I've started the first step (using a simple single checkbox first to get my head around it) and now I'm trying to integrate the spreadsheet and pull text from the specific Excel cell.

    I've read articles on how to get values from an Excel cell but since they're relative to other people's programs it's difficult to integrate and adapt that code into my own.

    Any help would be great!

    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;
    
    namespace Strings
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
           
                if (checkBox1_MS.Checked == true)
                {
                    TextBoxStrings.Text = " Insert Excel cell data";
                }
                
       
            }
        }
    }


    • Moved by CoolDadTx Tuesday, December 15, 2015 4:55 PM Office related
    Tuesday, December 15, 2015 11:21 AM

Answers

  • Hi Football365,

    >> I've read articles on how to get values from an Excel cell but since they're relative to other people's programs it's difficult to integrate and adapt that code into my own

    For getting values from an excel cell, you could use Excel Object model, here is a simple code:

                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Workbook wk = app.Workbooks.Open(@"D:\Excel\Test.xlsx"); //excel file path
                app.Visible = true;
                Worksheet ws = wk.ActiveSheet;
                Range r = ws.Range["B12"];
                MessageBox.Show(r.Value);

    You need to add the reference Microsoft.Office.Interop.Excel.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 16, 2015 2:34 AM