开发者

.Net C# Microsoft.Office.Interop.Excel ListObject refresh datasource asynchronously

I have a messaging application set up in C# and will add the messages received asynchronously into a list. I am using only the Microsoft.Office.Interop.Excel namespace and not VSTO extensions thus faces a problem with the listobject.

What I want to do is to create a listobject using the method below and use a timer object to write to excel spreadsheet.

public static ListObject AddListObject()
    {
        oXL = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

        oWB = oXL.Workbooks.get_Item(1);
        oSheet = (Excel.Worksheet)oWB.ActiveSheet;

        activeRow = oXL.ActiveCell.Row;

        //content
        oSheet.Cells[activeRow , 1] = Store[0].name;
        oSheet.Cells[activeRow , 2] = Store[0].address;
        oSheet.Cells[activeRow , 3] = Store[0].number;
        oSheet.Cells[activeRow , 4] = Store[0].dob;

        Excel.ListObject listObject1 = oSheet.ListObjects.Add(
            Excel.XlListObjectSourceType.xlSrcRange, oXL.ActiveCell.CurrentRegion, false,
            Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, Type.Missing);
        listObject1.TableStyle = "TableStyleLight9";



        oXL.Visible = true;
        oXL.UserControl = false;

        return listObject1;


    }

In the main program, after starting the messaging portion, I will get the listobject and pass it to the timer to update the spreadsheet.

listobject1 = AddListObject();
timer = new Timer(DoSomething, listobject1, 0, 1000);

How do I do that when the timer object calls the DoSomething method every second?

public void DoSomething(ListObject obj)
    { 
        //refresh listobject and write and update to sprea开发者_如何学Godsheet every second

    }

In VSTO, I will do the following:

listobject1 = AddListObject();
listobject1.DataSource=Store;
listobject1.AutSetDataBoundColumnHeaders=true;
timer = new Timer(DoSomething, listobject1, 0, 1000);

public void DoSomething(object state)
    { 
        //refresh listobject and write and update to spreadsheet every second
        ListObject lbo = (ListObject)state;
        lbo.RefreshDataRows();

    }

How do I do the following without VSTO extensions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜