简体   繁体   中英

Writing to nextline in Excel 2007

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)....

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++)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM