我正在使用实体框架。我以这种方式将表格导出到excel:
DetailsSet TheDetails = db.DetailsSet.SingleOrDefault(x => x.Id == id);
string filename = Guid.NewGuid() + ".xls";
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
// var List = db.GetRegList(TheDetails.CorrectAnswer);
List<RegistrationSet> RegList = TheDetails.RegistrationSet.Where(x => x.Answer == TheDetails.CorrectAnswer).ToList();
dgGrid.DataSource = RegList;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
Response.End();
问题是在 excel 中,db 中的所有希伯来文文本都显示为 gibresh。当我.Where(x => x.Answer == TheDetails.CorrectAnswer)
从网格中删除时,excel 正常 - 意味着希伯来语中的所有文本都正常。
像这样没关系:
List<RegistrationSet> RegList = TheDetails.RegistrationSet.ToList();
但像这样,不行:
List<RegistrationSet> RegList = TheDetails.RegistrationSet.Where(x => x.Answer == TheDetails.CorrectAnswer).ToList();
OK 函数的完整示例:
DetailsSet TheDetails = db.DetailsSet.SingleOrDefault(x => x.Id == id);
string filename = Guid.NewGuid() + ".xls";
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
// var List = db.GetRegList(TheDetails.CorrectAnswer);
List<RegistrationSet> RegList = TheDetails.RegistrationSet.ToList();
dgGrid.DataSource = RegList;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
Response.End();