Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

am storing string array values in excel sheet.here is the code with this , values are storing at only in 7 th column of excel.
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] = "Machine ID";
               xlWorkSheet.Cells[1, 2] = "Customer ID";
               xlWorkSheet.Cells[1, 3] = "Name";
               xlWorkSheet.Cells[1, 4] = "Total Balance";
               xlWorkSheet.Cells[1, 5] = "Paid Amount";
               xlWorkSheet.Cells[1, 6] = "Last Paid Date";
               xlWorkSheet.Cells[1, 7] = "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;
               xlWorkSheet.Cells[1, 6].Interior.ColorIndex = 39;
               xlWorkSheet.Cells[1, 7].Interior.ColorIndex = 39;



               serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);

                string[] lines = Regex.Split(s, "[$#,]");
            // string[] lines = s.Split(',');


               //MessageBox.Show("split Value:" + line);
                String line;


               for (int p = 1; p < lines.Length; p++)
               {
                  for (int q = 1; q < 7; q++)
                   {
                        line=lines[q];
                      xlWorkSheet.Cells[p, q] = lines;
                       if(line!="")
                   ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p + 1, q+1 ]).Value2 = lines[p];



                 }

               }






               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.StackTrace);
           }

           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);
                   }
               }
           }

       }
Posted
Comments
Maciej Los 17-Dec-13 2:01am    
And the question is...
What kind of error do you get?
Member 10263519 17-Dec-13 2:11am    
serial port receiving data from excel sheet. after every row imy appli need to send ok signal then only serialport read next row and send to application.

but my application reading all the rows at a time ,and placing in excel as

machid custid name totalBalance paid lastpaidDate remaining
m123 m123 m123 m123 ..........

so on like that ,every column(received data) is displaying in all excel columns ,

ny serialport_dartareceived is:
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);

1 solution

C#
for (int p = 1; p <= lines.Length; p++)
{
   for (int q = 1; q <= 7; q++)
    {
        blahh....blah....blah....
    }
}
 
Share this answer
 

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