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:
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)
{
if (line != "")
{
for (int p = 2; p < 50; p++)
{
for (int q = 0; q < 7; q++)
xlWorkSheet.Cells[p, q] = line;
}
}
}
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
{
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);
}
}
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