本文整理汇总了C#中IQueryable.ToGridModel方法的典型用法代码示例。如果您正苦于以下问题:C# IQueryable.ToGridModel方法的具体用法?C# IQueryable.ToGridModel怎么用?C# IQueryable.ToGridModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IQueryable
的用法示例。
在下文中一共展示了IQueryable.ToGridModel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportExcel
public ActionResult ExportExcel(IQueryable query, string fileName, string columns, string orderBy, string filter)
{
var items = query.ToGridModel(1, int.MaxValue, orderBy, string.Empty, filter).Data;
var cols = (from c in columns.Split(';')
where !String.IsNullOrWhiteSpace(c)
let x = c.Split(':')
select new {
prop = x[0],
title = x[1]
}).ToArray();
//Create new Excel workbook
var workbook = new HSSFWorkbook();
//Create new Excel sheet
var sheet = workbook.CreateSheet();
//Create a header row
var headerRow = sheet.CreateRow(0);
for (int i = 0; i < cols.Length; i++)
{
sheet.SetColumnWidth(i, 12 * 256);
headerRow.CreateCell(i).SetCellValue(cols[i].title);
}
sheet.CreateFreezePane(0, 1, 0, 1);
//Populate the sheet with values from the grid data
int rowNumber = 1;
foreach (var item in items)
{
//Create a new row
var row = sheet.CreateRow(rowNumber++);
//Set values for the cells
for (int i = 0; i < cols.Length; i++)
{
object val = item.GetPropValue(cols[i].prop);
if (val != null)
{
if (val is bool)
{
row.CreateCell(i).SetCellValue((bool)val);
}
else if (val is DateTime)
{
row.CreateCell(i).SetCellValue((DateTime)val);
}
else if (val is int)
{
row.CreateCell(i).SetCellValue((int)val);
}
else if (val is double)
{
row.CreateCell(i).SetCellValue((double)val);
}
else if (val is decimal)
{
row.CreateCell(i).SetCellValue(Convert.ToDouble(val));
}
else
{
row.CreateCell(i).SetCellValue(val.ToString());
}
}
}
}
//Write the workbook to a memory stream
MemoryStream output = new MemoryStream();
workbook.Write(output);
//Return the result to the end user
return File(output.ToArray(), //The binary data of the XLS file
"application/vnd.ms-excel", //MIME type of Excel files
fileName + ".xls"); //Suggested file name in the "Save as" dialog which will be displayed to the end user
}