C# 如何使用C将对象添加为Excel行#
我有一个类,比如说-Member,看起来像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
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。很好,很好。效果很好。