KeyValuePair export to excel

本文关键字:excel to export KeyValuePair | 更新日期: 2023-09-27 17:50:30

我试图将我的数据导出到excel文件,我构建对象并将其传递到导出,我想在构建完整对象之前进行检查,但失败了

public ActionResult ExportToExcel(int roleId, int? levelId)
        {
            var moduleItems = db.C_UserItems
                .Include(x => x.S_Registration)
                .Where(x => x.RoleId == roleId);
            if (levelId != null)
                moduleItems = moduleItems.Where(x => x.S_Registration.LevelId == levelId);
            List<object> objList = new List<object>();
            int i = 0;
            foreach (var item in moduleItems)
            {
                i++;
                List<KeyValuePair<string, object>> obj = new List<KeyValuePair<string, object>>();
                obj.Add(new KeyValuePair<string, object>("Num", i));
                obj.Add(new KeyValuePair<string, object>("Name", item.Name));
                obj.Add(new KeyValuePair<string, object>("Email", item.Email));
                if (roleId == 5)
                {
                    obj.Add(new KeyValuePair<string, object>("Phone", item.Phone));
                    obj.Add(new KeyValuePair<string, object>("CellPhone", item.CellPhone));
                }
                objList.Add(obj);
            }
            SaveToExcel(objList);
            return RedirectToAction("Index", new { id = roleId, levelId = levelId });
        }

这是我的导出方法

protected void SaveToExcel(List<object> objList)
            {
                //Save result as excel file
                HttpResponse response = System.Web.HttpContext.Current.Response;
                // first let's clean up the response.object
                response.Clear();
                // set the response mime type for excel
                response.ContentType = "application/vnd.ms-excel";
                response.AddHeader("Content-Disposition", "attachment;filename='"Alaglan.xls'"");
                response.ContentEncoding = System.Text.Encoding.Unicode;
                response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        // instantiate a datagrid
                        GridView dg = new GridView();
                        dg.DataSource = objList;
                        dg.DataBind();
                        dg.RenderControl(htw);
                        response.Write(sw.ToString());
                        response.End();
                    }
                }
            }

结果显示为每行的计数,容量,我之前尝试过另一种方法来构建这个对象,它工作得很好,但它很难构建大数据,

if (roleId == 4)
                {
                    objList.Add(new
                    {
                        m = i,
                        name = item.Name,
                        email = item.Email
                    });
                }
                else
                {
                    objList.Add(new
                    {
                        m = i,
                        name = item.Name,
                        email = item.Email,
                        identity = item.S_Registration.Identification
                    });
                }

任何人都可以帮助我,请最好的方式来建立对象与一些检查

KeyValuePair export to excel

首先,我觉得这是不对的:

obj.Add(new KeyValuePair<string, object>("Name", item.Phone));
obj.Add(new KeyValuePair<string, object>("Email", item.CellPhone));

应该是PhoneCellPhone而不是NameEmail:

obj.Add(new KeyValuePair<string, object>("Phone", item.Phone));
obj.Add(new KeyValuePair<string, object>("CellPhone", item.CellPhone));

第二,我认为你不能从使用KeyValuePair的结构中生成GridView

然而,我将重写代码以使用DataTable而不是匿名对象和键/值对,并将DataTable绑定到GridView。它提供了比匿名对象更大的灵活性,并且没有KeyValuePair存在的绑定问题。

例如:

// Create data table to be filled
var DT = new DataTable();
DT.Columns.Add("Num", typeof(int));
DT.Columns.Add("Name");
DT.Columns.Add("Phone");
DT.Columns.Add("CellPhone");
...
int i = 0;
foreach (var item in moduleItems)
{
    i++;
    DataRow Row = DT.NewRow();
    Row["Num"] = i;
    Row["Name"] = item.Name;
    Row["Email"] = item.Email;
    if (roleId == 5)
    {
        Row["Phone"] = item.Phone;
        Row["CellPhone"] = item.CellPhone;
    }
    DT.Rows.Add(Row);
}
...
// instantiate a datagrid
GridView dg = new GridView();
dg.DataSource = DT.DefaultView;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();