在 Excel 2007 中写入下一行
目前,我有以下写入 Excel 2007 的方法。
public static void createSpreadsheet(String msg)
{
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRng;
oXL = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
oWB = oXL.Workbooks.get_Item(1);
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oXL.Visible = true;
oXL.UserControl = false;
oRng = oSheet.get_Range("A1", "A" + 1);
oRng.Value2 = msg;
}
但是,无论我发送什么消息,它都只能写入 A1 列,这从上面的代码中可以明显看出。
如何扩展上面的代码,以便每当发送其他消息时,它们都会附加到之前编写的列下方。? 在控制台应用程序中,我可以这样做:Console.writeline(msg)。我如何在 Excel 中实现这一目标?
例如:消息 1(A1 列) 消息 2(A2 栏) 消息 3(A3 栏) ....
Currently, I have the following method that writes to excel 2007.
public static void createSpreadsheet(String msg)
{
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRng;
oXL = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
oWB = oXL.Workbooks.get_Item(1);
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oXL.Visible = true;
oXL.UserControl = false;
oRng = oSheet.get_Range("A1", "A" + 1);
oRng.Value2 = msg;
}
However, whatever msg I sent, it only get to write to column A1 which is evident from the code above.
How do I expand the code above so that whenever additional messages are sent, they are appended below the previously written column.??
In a console app, I could do this: Console.writeline(msg). How do I achieve that in excel?
Eg: Msg 1 (Col A1)
Msg 2 (Col A2)
Msg 3 (Col A3)
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义一个名为
currentColumn
的变量并将其传递给方法:在调用中:
Define a variable called
currentColumn
and pass it to the method:In calling:
您目前仅获得 A1 细胞。如果您想写入其他行,只需将参数更改为您的范围即可。例如:
现在您可以在每条消息后动态增加 rowNumber。同样的事情也适用于列,只需将“A”替换为适当的列即可。
You're only getting the A1 cell currently. If you want to write to some other row, just change the parameters to your range. For example:
And now you can dynamically increment rowNumber after each message. Same thing works for columns, just replace "A" with the appropriate column.