Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
am receing data from serialport, for testing am taking two applications.
am receiving from serialport ,and placing in excel in button click.

Here is the code for receiving and placing:
C#
private void Download_Click(object sender, EventArgs e)
      {

          try
          {
              Excel.Application xlApp = default(Excel.Application);
              Excel.Workbook xlWorkBook = default(Excel.Workbook);
              Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);

              object misValue = System.Reflection.Missing.Value;
              xlApp = new Excel.Application();
              xlWorkBook = xlApp.Workbooks.Add(misValue);
              xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
              xlWorkSheet.Cells[1, 1] = "Name";
              xlWorkSheet.Cells[1, 2] = "ID Number";
              xlWorkSheet.Cells[1, 3] = "Installment";
              xlWorkSheet.Cells[1, 4] = "Paid Amount";
              xlWorkSheet.Cells[1, 5] = "Due Amount";

              xlWorkSheet.Cells[1, 1].Interior.ColorIndex = 39;
              xlWorkSheet.Cells[1, 2].Interior.ColorIndex = 39;
              xlWorkSheet.Cells[1, 3].Interior.ColorIndex = 39;
              xlWorkSheet.Cells[1, 4].Interior.ColorIndex = 39;
              xlWorkSheet.Cells[1, 5].Interior.ColorIndex = 39;



              serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);

              string[] lines = Regex.Split(s, "[$#,]");
              foreach (String line in lines)
              {
                  // MessageBox.Show("split Value:" + line);
                  if (line != "")
                  {

                      for (int p = 2; p < 50; p++)
                      {
                          for (int q = 0; q < 7; q++)

                              xlWorkSheet.Cells[p, q] = line;  //here is the error place

                      }

                  }
              }



              xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              xlWorkBook.Close(true, misValue, misValue);
              xlApp.Quit();
              releaseObject(xlWorkSheet);
              releaseObject(xlWorkBook);
              releaseObject(xlApp);
          }
          catch (Exception p)
          {
              MessageBox.Show(p.Message);
          }
          finally
          {
              if (xlApp != null)
                  releaseObject(xlApp);
              if (xlWorkBook != null)
                  releaseObject(xlWorkBook);
              if (xlWorkSheet != null)
                  releaseObject(xlWorkSheet);
          }
          if (System.IO.File.Exists(fName))
          {
              if (MessageBox.Show("Would you like to open the excel file?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
              {
                  try
                  {
                      System.Diagnostics.Process.Start(fName);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("Error opening the excel file." + Environment.NewLine +
                        ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }
              }
          }

      }


      private void releaseObject(object obj)
      {
          try
          {
              System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
              obj = null;
          }
          catch (Exception ex)
          {
              obj = null;
              MessageBox.Show("Unable to release the Object " + ex.ToString());
          }
          finally
          {
              GC.Collect();
          }
      }

      private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
      {
          try
          {
              /*  for (int p = 0; p <2; p++)
                {
                    inputdata = serialPort1.ReadExisting();
                    MessageBox.Show("Read:" + inputdata);

                }*/
              if (inputdata == "$M121,0#")

                  MessageBox.Show("value is:" + inputdata);

              else
              {
                  if (!serialPort1.IsOpen)
                      return;

                  Thread.Sleep(50);
                  byte[] buffer = new byte[serialPort1.BytesToRead];
                  serialPort1.Read(buffer, 0, buffer.Length);

                  s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                 // MessageBox.Show(s);


              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }

      }


but am getting error while storing in excel; as

COM Exception unhandled:Exception from HRESULT: 0x800A03EC

and stacktrace is:
System.Runtimetype.ForwardCallToInvokeManager(String MemberName,bindingFlags flag,Object target,Int32[] a Wrapper Types,messageData&msgdata)

at Microsoft.Office.Interop.Excel.Range.Set_Default(Object RowIndex, Object ColumnIndex,Object value)

Already i added column names at row 1.and trying to insert data from 2nd row onwards
Posted
Comments
Member 10263519 16-Dec-13 6:50am    
ok this exception was solved by starting columnindex 1.but data is not storing in excel.
Member 10263519 16-Dec-13 7:52am    
plz help me how to store that data in excel

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