如何删除dataTable中的列标题?

时间:2015-06-30 18:22:31

标签: c# asp.net datatable epplus

我有通用列表,我将其转换为DataTable

然后我生成具有列表属性并向其添加行的列,现在我想删除表中的列标题

这是我将List转换为DataTable

的代码
    public class ListtoDataTableConverter
    {
        public DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);
            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
             //put a breakpoint here and check datatable
            return dataTable;
        }
    }

上述代码的结果是

    Name                  Age
   ---------------------------------
     A                    22
     B                    23

现在我希望输出没有名称和年龄。

如何删除它们

1 个答案:

答案 0 :(得分:1)

没有真正的方法从表中“删除”列标题 - 毕竟它是一个表。但是为什么不告诉epplus用.LoadFromDataTable(dtdata, false)来抑制标题行的输出 - 请注意false作为第二个参数'PrintHeaders'。

[TestMethod]
public void ListToDataTableConverter()
{
    //Use a func for demonstrative purposes
    Func<List<NameAgeObject>, DataTable> ToDataTable = (items) =>
    {
        DataTable dataTable = new DataTable(typeof(NameAgeObject).Name);
        //Get all the properties
        PropertyInfo[] Props = typeof(NameAgeObject).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name);
        }
        foreach (NameAgeObject item in items)
        {
            var values = new object[Props.Length];
            for (int i = 0; i < Props.Length; i++)
            {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
        //put a breakpoint here and check datatable
        return dataTable;
    };

    var itemlist = new List<NameAgeObject>
    {
        new NameAgeObject {Name = "A", Age = 22},
        new NameAgeObject {Name = "B", Age = 23},
        new NameAgeObject {Name = "C", Age = 24},
        new NameAgeObject {Name = "D", Age = 25},
        new NameAgeObject {Name = "E", Age = 26},
    };

    var dtdata = ToDataTable(itemlist);


    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var package = new ExcelPackage(existingFile))
    {
        var ws = package.Workbook.Worksheets.Add("Sheet1");
        ws.Cells[1, 1].LoadFromDataTable(dtdata, false);
        package.Save();
    }

}

如果它是一个非常大的表,您可能会看到LoadFromDataTable的性能问题,在这种情况下,您可以通过点击excel中的单个单元格手动写入工作表。在这里,您可以按行/列显示数据表,只需跳过任何列信息的写入。