【发布时间】:2014-03-31 15:00:53
【问题描述】:
是否可以在 ASP.NET MVC 4 中将数据从 Excel 文件导入 jqGrid。
我查看了这个示例here,但它并没有完全讨论从 excel 文件中导入数据。我试图解决它,但数据没有显示在 jqGrid 上。我得到的只是一个只有标题的空表。任何帮助都会很棒:)
我的查看代码:jQGrid:
<table id="jQGridDemo"></table>
<div id="jQGridDemoPager"></div>
<script type="text/javascript">
jQuery("#jQGridDemo").jqGrid({
url: 'TopPlayedInVenueList',
datatype: "json",
colNames: ['Id', 'First Name', 'Last Name', 'Last 4 SSN', 'Department',
'Age'],
colModel: [
{ name: '_id', index: '_id', width: 20, stype: 'text' },
{ name: 'FirstName', index: 'FirstName', width: 150 },
{ name: 'LastName', index: 'LastName', width: 150 },
{ name: 'LastSSN', index: 'LastSSN', width: 100 },
{ name: 'Department', index: 'Department', width: 80, align: "right" },
{ name: 'Age', index: 'Salary', width: 80, align: "right" }
],
rowNum: 10,
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption: "List Employee Details"
});
</script>
我的控制器代码:
public JsonResult TopPlayedInVenueList()
{
try
{
if (Request.IsAuthenticated == true)
{
string Path = @"C:\\1.xls";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [All$A2:Z]", con);
//OleDbDataAdapter da = new OleDbDataAdapter("select * from [All$]", con);
con.Close();
System.Data.DataTable data = new System.Data.DataTable();
da.Fill(data);
List<TopPlayedInVenue> daa = new List<TopPlayedInVenue>();
foreach (DataRow p in data.Rows)
{
TopPlayedInVenue top = new TopPlayedInVenue()
{
TrackName = p.Field<string>("Track Name"),
ArtistName = p.Field<string>("Artist Name")
};
daa.Add(top);
}
var newlist = daa.OrderBy(i => i.TrackName).ToList();
return Json(daa);
提前致谢:)
【问题讨论】:
-
如果你调试你的代码,你会看到什么?我看到的第一件事是您在调用 Fill() 方法之前关闭了连接,不确定这是否有问题。
-
另外,尝试做一个更简单的选择...不使用范围...只是看看这是否可能是问题。
-
@Romias - 感谢您的输入,我已经清理了代码 :) - 还做了一个简单的选择来检查,但它没有用。
标签: c# asp.net-mvc-4 jqgrid-asp.net