if (dataGridView1.RowCount == 0)
            {
                string file = “”; //variable for the Excel File Location
                DataTable dt = new DataTable(); //container for our excel data
                DataRow row;
                DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
                if (result == DialogResult.OK) // Check if Result == “OK”.
                {
                    file = openFileDialog1.FileName; //get the filename with the location of the file
                    try
                    {
                      
                        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
 
                        Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(file);
 
                        Microsoft.Office.Interop.Excel._Worksheet excelWorksheet = excelWorkbook.Sheets[1];
 
                        Microsoft.Office.Interop.Excel.Range excelRange = excelWorksheet.UsedRange;
 
                        int rowCount = excelRange.Rows.Count; //get row count of excel data
 
                        int colCount = excelRange.Columns.Count; // get column count of excel data
 
                        //Get the first Column of excel file which is the Column Name
 
                        for (int i = 1; i <= rowCount; i++)
                        {
                            for (int j = 1; j <= colCount; j++)
                            {
                                dt.Columns.Add(excelRange.Cells[i, j].Value2.ToString());
                            }
                            break;
                        }
 
                        //Get Row Data of Excel
 
                        int rowCounter; //This variable is used for row index number
                        for (int i = 2; i <= rowCount; i++) //Loop for available row of excel data
                        {
                            row = dt.NewRow(); //assign new row to DataTable
                            rowCounter = 0;
                            for (int j = 1; j <= colCount; j++) //Loop for available column of excel data
                            {
                                //check if cell is empty
                                if (excelRange.Cells[i, j] != null && excelRange.Cells[i, j].Value2 != null)
                                {
                                    row[rowCounter] = excelRange.Cells[i, j].Value2.ToString();
                                }
                                else
                                {
                                    row[i] = “”;
                                }
                                rowCounter++;
                            }
                            dt.Rows.Add(row); //add row to DataTable
                        }
 
                        dataGridView1.DataSource = dt;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                Cmtbox.Text = “最初に既存のデータを削除して下さい”;
                Cmtbox.BackColor = Color.Yellow;
            }
        }
 
Code to connect PLC and PC
 
  plc.ActLogicalStationNumber = 5;
            conn = this.plc.Open();
            if (conn == 0)
            {
                Cmtbox.Text = “接続しまた”;
                Cmtbox.BackColor = Color.Green;
                //  Writebtn.BackColor = Color.Green;
                Lamp.BackColor = Color.Red;
                label11.Text = “PLC ON”;
            }
            else
            {
                Cmtbox.Text = “接続を解除してください”;
                Cmtbox.BackColor = Color.Yellow;
                // Writebtn.BackColor = Color.Yellow;
            }
        }
 
Code To read the Excel File
 
if (plc.ActLogicalStationNumber == 5)
            {
                if (dataGridView1.RowCount>0)
                {
                    for (int i = 0; i < dataGridView1.RowCount; i++)
                    {
                        for (int j = 0; j < dataGridView1.ColumnCount; j++)
                        {
                            plc.SetDevice(dataGridView1.Columns[j].HeaderText.ToString(), Convert.ToInt16(dataGridView1.Rows[i].Cells[j].Value.ToString()));
                            Thread.Sleep(200);
                        }
                    }
                }
                else
                {
                    Cmtbox.Text = “エラーデータありませんでした”;
                    Cmtbox.BackColor = Color.Yellow;
                }
            }
            else
            {
                Cmtbox.Text = “エラー : 接続解除に失敗しました”;
                Cmtbox.BackColor = Color.Yellow;
 
            }
 
 
Demo video
 
Discussion (1)
Share Your Thoughts