我在C#中设置了一个消息传递应用程序,并将异步接收的消息添加到列表中。我只使用Microsoft.Office.Interop.Excel命名空间而不是VSTO扩展因此面临listobject的问题。
我想要做的是使用下面的方法创建一个listobject,并使用一个timer对象写入excel电子表格。
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;
}
在主程序中,启动消息传递部分后,我将获取listobject并将其传递给计时器以更新电子表格。
listobject1 = AddListObject();
timer = new Timer(DoSomething, listobject1, 0, 1000);
当计时器对象每秒调用DoSomething方法时,我该怎么做?
public void DoSomething(ListObject obj)
{
//refresh listobject and write and update to spreadsheet every second
}
在VSTO中,我将执行以下操作:
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();
}
如果没有VSTO扩展,如何执行以下操作?