如何在使用asp.net mvc将数据导出到Excel中时,将复选框转换为gridview中的字符串值?
型号:
public class empViewmodel
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
public bool male{ get; set; }
public Nullable<int> city_id { get; set; }
}
控制器:
public ActionResult export1(emplyeeModel em)
{
var grid = new GridView();
grid.DataSource = e1.getlist(em);
grid.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return View("export");
}
Getlist方法:
public List<emplyeeModel> getlist(emplyeeModel e1)
{
var tbl =db.emps.ToList();
List<emplyeeModel> lst = new List<emplyeeModel>();
foreach (var item in tbl)
{
emplyeeModel M1 = new emplyeeModel();
M1.id = item.id;
M1.name = item.name;
M1.address = item.address;
M1.male= item.gender;
lst.Add(M1);
}
return lst;
}
在male列中是一个布尔值,它显示一个复选框,但我想显示字符串yes / no(True / False)而不是复选框,同时将数据导出到excel中。