在 Excel 2007 中写入下一行

发布于 2024-11-15 04:45:48 字数 796 浏览 2 评论 0原文

目前,我有以下写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-11-22 04:45:48

定义一个名为 currentColumn 的变量并将其传递给方法:

public static void createSpreadsheet(String msg, column)

在调用中:

// there is an integer variable called currentColumn
createSpreadsheet("a message", currentColumn++)

Define a variable called currentColumn and pass it to the method:

public static void createSpreadsheet(String msg, column)

In calling:

// there is an integer variable called currentColumn
createSpreadsheet("a message", currentColumn++)
半葬歌 2024-11-22 04:45:48

您目前仅获得 A1 细胞。如果您想写入其他行,只需将参数更改为您的范围即可。例如:

oRng = oSheet.Range["A" + rowNumber, "A" + rowNumber];
oRng.Value = msg;

现在您可以在每条消息后动态增加 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:

oRng = oSheet.Range["A" + rowNumber, "A" + rowNumber];
oRng.Value = msg;

And now you can dynamically increment rowNumber after each message. Same thing works for columns, just replace "A" with the appropriate column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文