2015-11-16 63 views
1

我需要的数据Grid.MVC导出到Excel中。我在这个链接中使用了解决方案。导出Grid.MVC数据到Excel

http://www.codeproject.com/Articles/325103/MVC-Grid-to-Excel-file-download?msg=5161340#xx5161340xx

它是工作,但我有2个问题。首先它是在铬工作,但它不在IE中工作。它在IE中给我一个错误(文件不能被读取)。第二个问题是当我过滤网格时,Excel中导出的数据仍显示所有数据而不是过滤数据。

如果这不是一个好的解决办法,请给我提供例如用于出口Grid.MVC数据到Excel。

+0

对于“文件无法读取”这是一个有点猜测的,而是从例子中,我注意到'''curContext.Response.AddHeader(“内容处置”,“附件;文件名=“+文件名);''' 并记住Firefox有一些问题,如果文件名有空格,会导致浏览器混淆。你应该把文件名一起放在引号中,像这样:'''''''''''''''' ''' –

回答

0

我有一个使用Javascript/jQuery的解决方案,为我的作品。

当您使用grid.mvc它的一些类添加到THEAD和TBODY,这个班需要为您生成Excel文件的正确的出口/可视化被删除。我也使用grid.mvc,并且此代码导出为ex​​cel,请让我知道这是否适用于您。

<script> 
 

 
    $("#btnExport").click(function (e) { 
 
     $('.grid-wrap').find('table').removeAttr('class'); 
 
     $('.grid-header').removeAttr('class'); 
 
     $('.grid-row').removeAttr('class'); 
 

 
     $('.grid-cell').removeAttr('data-name'); 
 
     $('.grid-cell').removeAttr('class'); 
 

 

 
     window.open('data:application/vnd.ms-excel,' + $('.grid-wrap').html()); 
 

 
     
 
     //MakeAnyFunctionToReloadThePageToGetTheClassesAgain(); 
 
     e.preventDefault(); 
 
    }); 
 

 
</script>
@Html.Grid(Model).Columns(columns => 
 
{ 
 
    
 
    columns.Add(foo => foo.Date).Sortable(true).Filterable(true); 
 
    columns.Add(foo => foo.User).Sortable(true).Filterable(true); 
 
    columns.Add(foo => foo.Controller).Sortable(true).Filterable(true); 
 
    columns.Add(foo => foo.Action).Sortable(true).Filterable(true); 
 
    columns.Add(foo => foo.ActionType).Sortable(true).Filterable(true); 
 
    columns.Add(foo => foo.JsonObject).Sortable(true).Filterable(true); 
 
}).WithMultipleFilters() 
 

 
<button type="button" class="btn btn-danger" id="btnExport">export csv</button>