Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 如何使用Microsoft.Office.Interop.Excel动态增加单元格_C#_Excel_Asp.net Mvc 4 - Fatal编程技术网

C# 如何使用Microsoft.Office.Interop.Excel动态增加单元格

C# 如何使用Microsoft.Office.Interop.Excel动态增加单元格,c#,excel,asp.net-mvc-4,C#,Excel,Asp.net Mvc 4,我正在尝试开发一个可重用的列表到excel方法。使用此示例,“” 我看到单元格被单独引用,就像这样 workSheet.Cells[1, "A"] = "ID Number"; 我希望能够自动填充列名,以及从左到右动态填充每个单元格。我认为有一种更好的方法,比必须在工作表中直接输入“a”、“B”…“A1”等更好。单元格[],甚至比将字母表存储到列表中更好 这是我到目前为止得到的 public static void DownloadExcelFile<T>(List<T>

我正在尝试开发一个可重用的列表到excel方法。使用此示例,“”

我看到单元格被单独引用,就像这样

workSheet.Cells[1, "A"] = "ID Number";
我希望能够自动填充列名,以及从左到右动态填充每个单元格。我认为有一种更好的方法,比必须在工作表中直接输入“a”、“B”…“A1”等更好。单元格[],甚至比将字母表存储到列表中更好

这是我到目前为止得到的

public static void DownloadExcelFile<T>(List<T> list, string fileName)
{
    Excel.Application excelApp = new Excel.Application();

    // Make the object visible.
    excelApp.Visible = true;

    excelApp.Workbooks.Add();

    Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;

    //create column headings
    foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list))
    {
        //how can I reference workSheet.Cells by row, and have the 
        //column incremented somehow within the loop?
        workSheet.Cells[1, "A"] = descriptor.name;

    }
}
publicstaticvoiddownloadExcelFile(列表,字符串文件名)
{
Excel.Application excelApp=新的Excel.Application();
//使对象可见。
excelApp.Visible=true;
excelApp.Workbooks.Add();
Excel.\u工作表工作表=(Excel.Worksheet)excelApp.ActiveSheet;
//创建列标题
foreach(System.ComponentModel.TypeDescriptor.GetProperties(列表))中的System.ComponentModel.PropertyDescriptor描述符
{
//如何按行引用工作表单元格,并使
//列在循环中以某种方式递增?
工作表.Cells[1,“A”]=descriptor.name;
}
}

您可以使用序号而不是字母代码:

int colIndex = 1;
foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list))
{
    workSheet.Cells[1, colIndex++] = descriptor.Name;
}

啊好的。谢谢我觉得这很简单,但我在查看一个范围时感到困惑,我想我可能需要用它来代替。我已经很长时间没有使用这个库了,所以我基本上是在重新学习它。