无论如何,要在excel文件中排除DhtmlxGrid的隐藏列?
现在当我导出到excel时,它还包括隐藏在excel中的列。请建议。
MVC3
in View:
on a button click my code:
_grid.toExcel('@Url.Content("~/Shared/ExportGridToExcel?TableName")' +'Doc');
in Controller:
public ActionResult ExportGridToExcel(string TableName)
{
var generator = new ExcelWriter();
var xml = this.Request.Form["grid_xml"];
xml = this.Server.UrlDecode(xml);
var stream = generator.Generate(xml);
return File(stream.ToArray(), generator.ContentType, TableName + ".xls");
}
答案 0 :(得分:0)
找到此方法并替换此
public ActionResult GenerateExcel()
{
var generator = new ExcelWriter();
var xml = this.Request.Form["grid_xml"];
xml = this.Server.UrlDecode(xml);
var xmlObj = XDocument.Parse(xml);
foreach (var x in xmlObj.Descendants("columns").ElementAt(0).Descendants("column"))
{
var w = x.Attribute("width").Value;
if (Convert.ToString(w).Equals("0")) { x.SetAttributeValue("hidden", "true"); }
}
xml = xmlObj.ToString();
var stream = generator.Generate(xml);
return File(stream.ToArray(), generator.ContentType, "grid.xlsx");
}
如果有效,请不要忘记将此标记为答案
答案 1 :(得分:0)
使用setColumnHidden api在导出调用之前隐藏列,并在其后显示该列。隐藏的列不包含在导出结果中。