Search code examples
c#stringserverexport-to-excelread-write

Writing string to Excel cell doesn't work


I have a server that can receive messages from clients through sockets.
With button 1, I open the server so I can receive strings. Then there's the ReceiveCallback:

public void ReceiveCallback(IAsyncResult AR)
{
    try
    {
        Socket socket = (Socket)AR.AsyncState;
        int received = socket.EndReceive(AR);

        if (received == 0)
        {
            return;
        }

        byte[] dataBuf = new byte[received];
        Array.Copy(BuFfer, dataBuf, received);
        string text = Encoding.ASCII.GetString(dataBuf);
        writetotextbox("Client: " + text);
        //writetoExcel(text); <<<<<<<<<-------------
        socket.BeginReceive(BuFfer, 0, BuFfer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); //deze zorgt voor problemen
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

public void writetotextbox(string text)
{
    MethodInvoker invoker = new MethodInvoker(delegate
    {
        textBox1.Text += text + "\r\n";
    });
    this.Invoke(invoker);
}

Now I want the same value written to Excel. That's still a problem for me, so far I got this code:

Excel.Application Start = null;
Excel._Workbook theWorkbook = null;
Excel.Range range = null;
object misValue = System.Reflection.Missing.Value;

I open excel sheet with button 2. Does the excel file refresh itself when it receives a value when the file is open or has it to be closed?

try
{
    Start = new Excel.Application(); 
    theWorkbook = Start.Workbooks.Add(misValue);
    theWorkbook = Start.Workbooks.Open(@"C:\Users\Name\Downloads\Map1.xlsx", misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
    Excel._Worksheet oSheet =     (Excel._Worksheet)theWorkbook.Worksheets.get_Item(1);
    string strWorksheetName = oSheet.Name;
    oSheet.Cells[1, 1] = "Hello";
    oSheet.get_Range("A1", "Z1").Font.Bold = true;
    Start.Visible = true;
    Start.UserControl = true;
}
catch (Exception theException)
{
    MessageBox.Show(errorMessage, "Error");
}

public void writetoExcel(string text)
{
      oSheet.Cells[1, 2] = text; // <-- doenst work, i get an error
}                 //oSheet is not working.

Individual the program works, combine it works, but the writetoExcel isn't, can someone help me out here?

When/or it is impossible to do. Then I want that value (a, b just a letter that I receive) to get in a switch like this:

switch (text)
{
    case "a":
    oSheet.Cells[1, 2] = "Kees";
    break;
    case "b":
    oSheet.Cells[2, 2] = "piet";
    break;
} //and ofcourse write the string to excel

Solution

  • It helps to include (or rather, research) the actual compiler error, in your case:

    The name 'oSheet' does not exist in the current context

    You seem to declare Excel._Worksheet oSheet in a different method than the method in which you want to use it.

    You can make it a field on your form and assign it in the first method:

    public partial class YourForm : Form
    {
        private Excel._Worksheet oSheet;
    
        private void SomeMethod()
        {
            oSheet = (Excel._Worksheet)theWorkbook.Worksheets.get_Item(1);
        }
    
        public void writetoExcel(string text)
        {
          oSheet.Cells[1, 2] = text;
        }
    
    }