Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.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# 如何使用C将对象添加为Excel行#_C#_Excel_Winforms_Excel Interop - Fatal编程技术网

C# 如何使用C将对象添加为Excel行#

C# 如何使用C将对象添加为Excel行#,c#,excel,winforms,excel-interop,C#,Excel,Winforms,Excel Interop,我有一个类,比如说-Member,看起来像 class Member { public string Id { get; set; } public string Login { get; set; } public string AddDate { get; set; } public string FirstName{ get; set; } public string LastName{ get; set; } public string F

我有一个类,比如说-Member,看起来像

class Member
{
    public string Id { get; set; }
    public string Login { get; set; }
    public string AddDate { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public string FullName { get; set; }            
}
现在我需要将成员的对象作为一行插入Excel工作表中。我使用的是
Microsoft.Office.Interop
,并使用以下代码创建了一个工作表对象:

Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet xlWorkSheet = (Worksheet)xlWorkBook.Worksheets[1];
我的最终输出应该是

您已经声明了您的
Excel.Worksheet
,现在您只需要迭代存储在某处的
成员
类,并将其每个属性写入工作表

假设所有
成员
类存储在
列表成员
中,所有标题值存储在
列表标题
中:

//Setting up headers

int column = 1;
foreach (var item in members)
    xlWorksheet.Cells[1, column++].Value = item;

//setting up member values

int row = 2;
foreach (var item in members)
{
    column = 1;
    xlWorksheet.Cells[row, column++].Value = item.Id;
    xlWorksheet.Cells[row, column++].Value = item.Login;
    xlWorksheet.Cells[row, column++].Value = item.AddDate;
    xlWorksheet.Cells[row, column++].Value = item.FirstName;
    xlWorksheet.Cells[row, column++].Value = item.LastName;
    xlWorksheet.Cells[row++, column].Value = item.FullName;
}
请注意,如果您正在处理大量需要插入工作表的数据,那么这可能不是最佳解决方案。在这种情况下,您可以禁用excel的屏幕更新(
xlApp.screenUpdate=false;
),但是,请注意,在插入完成后,您应该将其启用回
true
。我认为在您的情况下,我的代码的简单性可以很好地工作,但是如果您绝对必须优化它,那么您也可以使用
Excel.Range
Value2
直接插入
成员的数组。大概是这样的:

const int rowCount = 1, columnCount = 6;
object[,] membersArray = new object[rowCount, columnCount];
//fill up membersArray with your data here
xlWorksheet.Cells[1, 1].Value2 = membersArray;

您已经声明了
Excel.Worksheet
,现在只需迭代存储在某处的
成员
类,并将其每个属性写入工作表

假设所有
成员
类存储在
列表成员
中,所有标题值存储在
列表标题
中:

//Setting up headers

int column = 1;
foreach (var item in members)
    xlWorksheet.Cells[1, column++].Value = item;

//setting up member values

int row = 2;
foreach (var item in members)
{
    column = 1;
    xlWorksheet.Cells[row, column++].Value = item.Id;
    xlWorksheet.Cells[row, column++].Value = item.Login;
    xlWorksheet.Cells[row, column++].Value = item.AddDate;
    xlWorksheet.Cells[row, column++].Value = item.FirstName;
    xlWorksheet.Cells[row, column++].Value = item.LastName;
    xlWorksheet.Cells[row++, column].Value = item.FullName;
}
请注意,如果您正在处理大量需要插入工作表的数据,那么这可能不是最佳解决方案。在这种情况下,您可以禁用excel的屏幕更新(
xlApp.screenUpdate=false;
),但是,请注意,在插入完成后,您应该将其启用回
true
。我认为在您的情况下,我的代码的简单性可以很好地工作,但是如果您绝对必须优化它,那么您也可以使用
Excel.Range
Value2
直接插入
成员的数组。大概是这样的:

const int rowCount = 1, columnCount = 6;
object[,] membersArray = new object[rowCount, columnCount];
//fill up membersArray with your data here
xlWorksheet.Cells[1, 1].Value2 = membersArray;

我通常使用属性建立一个
数组
,以便写入Excel,您可以使用
字符串
数组,因为您的所有属性都是相同类型的。接下来,我将从
工作表
中获得一个
范围
,从我希望写入的第一列一直到
array.Length-1
。有了这个
范围
我将把它的
.Value2
属性设置为创建的数组。我通常用这些属性建立一个
数组
,以便写入Excel,你可以使用
字符串
数组,因为你所有的属性都是同一类型的。接下来,我将从
工作表
中获得一个
范围
,从我希望写入的第一列一直到
array.Length-1
。使用这个
范围
我将把它的
.Value2
属性设置为创建的array.PERFECT。很好,很好。效果很好。