Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Excel 2007中写入nextline_C#_Asp.net_Excel 2007 - Fatal编程技术网

C# 在Excel 2007中写入nextline

C# 在Excel 2007中写入nextline,c#,asp.net,excel-2007,C#,Asp.net,Excel 2007,目前,我有以下写入Excel2007的方法 public static void createSpreadsheet(String msg) { Excel.Application oXL; Excel.Workbook oWB; Excel.Worksheet oSheet; Excel.Range oRng; oXL = (Excel.Application)System.Runtime.InteropS

目前,我有以下写入Excel2007的方法

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

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

public static void createSpreadsheet(String msg, column)
在通话中:

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

您当前仅获取A1单元格。如果要写入其他行,只需将参数更改为您的范围。例如:

oRng = oSheet.Range["A" + rowNumber, "A" + rowNumber];
oRng.Value = msg;
现在,您可以在每条消息之后动态增加行数。同样的事情也适用于列,只需用适当的列替换“A”