none
Move data from C# to excel RRS feed

  • Question

  • Hello, i have trouble here

    I used C# to read the result of the value of distance sensor who have i connected with my arduino. I also have interface from C# to excel

    Trouble : I can moved the data of the sensor value to the excel, but the all result only the last value who shown
    Could you help me please?
    Monday, April 9, 2018 4:30 AM

Answers

  • This is the code

    using System.IO.Ports;
    using System.Threading;
    using System.Diagnostics;
    using Microsoft.Office.Interop.Excel;

                                 

    namespace deformation_sensor
    {
        public partial class Form1 : Form
        {
            private SerialPort myport;
            private string DEFORMATION1;
            private string DEFORMATION2;

            private List<string> ListDeformation1 = new List<string>();
            private List<string> ListDeformation2 = new List<string>();

            public Form1()
            {
                InitializeComponent();
                timer1.Start();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Start();
            }
            int intlen = 0;

            private void on_Click(object sender, EventArgs e)
            {
                myport = new SerialPort();
                myport.BaudRate = 9600;
                myport.PortName = "COM3";
                myport.DataReceived += myport_DataReceived;
                try
                {
                    myport.Open();

                    label1.Text = "";
                    label2.Text= "";
                }
                catch (Exception)
                {
                    MessageBox.Show("ERROR. MAKE SURE YOUR DEVICE HAS BEEN CONNECTED");
                }
            }

            void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {

                this.Invoke(new EventHandler(displaydata_event));

                try
                {

                    DEFORMATION1 = myport.ReadLine();
                    DEFORMATION2 = myport.ReadLine();

                }
            }

            private void displaydata_event(object sender, EventArgs e)
            {

                label1.Text = DEFORMATION1;
                label2.Text = DEFORMATION2;

                ListDeformation1.Add(DEFORMATION1);
                ListDeformation2.Add(DEFORMATION2);
            }

            private void timer1_Tick(object sender, EventArgs e)
            {

                string[] port = SerialPort.GetPortNames();
                if (intlen != port.Length)
                {
                    intlen = port.Length;
                }
            }

            private void off_Click(object sender, EventArgs e)
            {
                try
                {

                    label1.Text = "0.00";
                    label2.Text = "0.00";

                }
                catch (Exception)
                {
                    MessageBox.Show("ERROR");
                }
            }

            private void pictureBox2_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
                Worksheet ws = (Worksheet)xla.ActiveSheet;

                xla.Visible = true;

                ws.Cells[1, 1] = "TIME";
                ws.Cells[1, 2] = "DEF.1";
                ws.Cells[1, 3] = "DEF.2";

                int data = ListDeformation1.Count() - 1;
                for(int i = 2;i<ListDeformation1.Count();i++)
                {
                    ws.Cells[i, 1] = i;
                    ws.Cells[i, 2] = ListDeformation1[data];
                    ws.Cells[i, 3] = ListDeformation2[data];
                    data--;
                }
            }
        }
    }


    • Marked as answer by andrean.kiki Thursday, April 12, 2018 6:29 AM
    Thursday, April 12, 2018 6:28 AM

All replies

  • Hello andrean.kiki,

    >>I also have interface from C# to excel

    How do you do interface from C# to excel? Could you share us your code? 

    >>all result only the last value who shown

    What do you need? Do you want to list all the history data? Usually, we use a range to get the value. Have you ever change the range so new value will show in new range instead of replacing original range?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 9, 2018 7:00 AM
  • This is the code

    using System.IO.Ports;
    using System.Threading;
    using System.Diagnostics;
    using Microsoft.Office.Interop.Excel;

                                 

    namespace deformation_sensor
    {
        public partial class Form1 : Form
        {
            private SerialPort myport;
            private string DEFORMATION1;
            private string DEFORMATION2;

            private List<string> ListDeformation1 = new List<string>();
            private List<string> ListDeformation2 = new List<string>();

            public Form1()
            {
                InitializeComponent();
                timer1.Start();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Start();
            }
            int intlen = 0;

            private void on_Click(object sender, EventArgs e)
            {
                myport = new SerialPort();
                myport.BaudRate = 9600;
                myport.PortName = "COM3";
                myport.DataReceived += myport_DataReceived;
                try
                {
                    myport.Open();

                    label1.Text = "";
                    label2.Text= "";
                }
                catch (Exception)
                {
                    MessageBox.Show("ERROR. MAKE SURE YOUR DEVICE HAS BEEN CONNECTED");
                }
            }

            void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {

                this.Invoke(new EventHandler(displaydata_event));

                try
                {

                    DEFORMATION1 = myport.ReadLine();
                    DEFORMATION2 = myport.ReadLine();

                }
            }

            private void displaydata_event(object sender, EventArgs e)
            {

                label1.Text = DEFORMATION1;
                label2.Text = DEFORMATION2;

                ListDeformation1.Add(DEFORMATION1);
                ListDeformation2.Add(DEFORMATION2);
            }

            private void timer1_Tick(object sender, EventArgs e)
            {

                string[] port = SerialPort.GetPortNames();
                if (intlen != port.Length)
                {
                    intlen = port.Length;
                }
            }

            private void off_Click(object sender, EventArgs e)
            {
                try
                {

                    label1.Text = "0.00";
                    label2.Text = "0.00";

                }
                catch (Exception)
                {
                    MessageBox.Show("ERROR");
                }
            }

            private void pictureBox2_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
                Worksheet ws = (Worksheet)xla.ActiveSheet;

                xla.Visible = true;

                ws.Cells[1, 1] = "TIME";
                ws.Cells[1, 2] = "DEF.1";
                ws.Cells[1, 3] = "DEF.2";

                int data = ListDeformation1.Count() - 1;
                for(int i = 2;i<ListDeformation1.Count();i++)
                {
                    ws.Cells[i, 1] = i;
                    ws.Cells[i, 2] = ListDeformation1[data];
                    ws.Cells[i, 3] = ListDeformation2[data];
                    data--;
                }
            }
        }
    }


    • Marked as answer by andrean.kiki Thursday, April 12, 2018 6:29 AM
    Thursday, April 12, 2018 6:28 AM
  • I have question
    My trouble before had finished, but now i have trouble around my program before. I connected both sensor to my arduino and interface to C# to see the result of measurement, I create label 1 to show the result of sensor deformation 1 and label 2 to show  the result of sensor deformation 2. But when i running the program, sometime the result measurement of sensor deformation 1 is showed on label 2 and the result measurement of sensor deformation 2 is showed on label 1 (Sometime is change but sometime is appropriate)
    How to fix it?
    That is problem about TX/RX?
    Thursday, April 12, 2018 6:42 AM
  • Hello andrean.kiki,

    We support one issue for thread. Please post new thread for your new issue.

    Besides, according to your description, the issue is more related to label and windows form development. I would suggest you thread on a Windows Forms forum.

    Thanks for understanding.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 12, 2018 6:48 AM