导出到Excel文件时出现JqGrid DataBinding异常 [英] JqGrid DataBinding exception while exporting to excel file

查看:89
本文介绍了导出到Excel文件时出现JqGrid DataBinding异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JqGrid导出到excel,所以我遵循此说明,我使用了就像下面一样.

I am trying to export JqGrid to excel so i follow this instruction and i use it like at below.

        var grid = new JqGridModelParticipiant().JqGridParticipiant;

        var query = db.ReservationSet.Select(r => new
        {
            r.Id,
            Name = r.Doctor.Name,
            Identity = r.Doctor.Identity,
            Title = r.Doctor.Title.Name,
            Total = r.TotalTL,
            Organization = r.Organization.Name
        });

        grid.ExportToExcel(query,"file.xls");

我在"grid.ExportToExcel(query,"file.xls");"

And i get below exception on the line of " grid.ExportToExcel(query,"file.xls");"

直接绑定到商店查询(DbSet,DbQuery,DbSqlQuery)的数据是 不支持.而是用数据填充DbSet,例如通过 在DbSet上调用Load,然后绑定到本地数据.对于WPF绑定 到DbSet.Local.对于WinForms,请绑定到DbSet.Local.ToBindingList().

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

据我了解,它希望在DbSet.Local成员上具有ObservableCollection.但是我正在研究预计的查询,所以我无法做到这一点.

As far as i understand that it expect to have ObservableCollection that is on DbSet.Local member. But i am working on projected query so i can't do that.

该问题的解决方案是什么.

What is the solution for this problem.

推荐答案

答案中,我发布了演示,该演示显示了如何实现导出到Excel(以实际的* .XLSX文件代替HTML片段已重命名为* .XLS,此处).

In the answer I posted the demo which shows how to implement export to Excel (real *.XLSX file instead of HTML fragment renamed to *.XLS used here).

jqSuite(演示中用于导出到Excel的方法产生)像HTML片段

The method used for exported to the Excel in jqSuite (the demo) produce HTML fragment like

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/excel; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNetMvc-Version: 2.0
content-disposition: attachment; filename=grid.xls
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 29 Jun 2012 14:24:54 GMT
Connection: close

<table cellspacing="0" rules="all" border="1" id="_exportGrid" style="border-collapse:collapse;">
    <tr>
        <td>OrderID</td><td>CustomerID</td><td>OrderDate</td><td>Freight</td><td>ShipName</td>
    </tr><tr>
        <td>10248</td><td>VINET</td><td>1996/07/04</td><td>32.3800</td><td>Vins et alcools Chevalier</td>
    </tr><tr>
        <td>10249</td><td>TOMSP</td><td>1996/07/05</td><td>11.6100</td><td>Toms Spezialitäten</td>
    </tr><tr>
        <td>10250</td><td>HANAR</td><td>1996/07/08</td><td>65.8300</td><td>Hanari Carnes</td>
    </tr><tr>
    ...
</table>

而不是创建真实的Excel文件.这种方法非常不安全,因为在开始时将始终使用标准"类型的数据.例如,如果您要导出数据,例如

instead of creating of real Excel file. The way is very unsafe because at the opening the "Standard" type of data will be always used. For example if you would export the data like

<td>10249</td><td>TOMSP</td><td>1996/07/05</td><td>11.02.12</td><td>Toms Spezialitäten</td>

如果默认使用德语语言环境,则文本"11.02.12"将自动转换为日期11.02.2012:

the text "11.02.12" will be automatically converted to the date 11.02.2012 if German locale are used as default:

"TomsSpezialitäten"的名称将错误地显示为"TomsSpezialitäten".

The name "Toms Spezialitäten" from will be wrong displayed as "Toms Spezialitäten".

在大表中,网格中间的一小部分数据将被错误转换的情况下,这尤其危险.在一个项目中,我显示了有关软件的信息,某些软件版本将错误地转换为日期类型.

It can be especially dangerous in case of large table where some small part of data in the middle of grid will be wrong converted. In one project I displayed information about Software and some software versions will be wrong converted to the Date type.

由于此类和其他紧密的问题,我在服务器上使用打开XML SDK 2.5 打开XML SDK 2.0 .顺便说一句,上面没有问题.因此,我建议您遵循我的旧答案中所述的方法.

Because of such and other close problems I create real Excel file on the server using Open XML SDK 2.5 or Open XML SDK 2.0. In the way one have no problems described above. So I recommend you to follow the approach described in my old answer.

这篇关于导出到Excel文件时出现JqGrid DataBinding异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆