是否可以将数据从Excel导入jQGrid?

时间:2014-03-31 15:00:54

标签: c# asp.net-mvc-4 jqgrid-asp.net

是否可以将数据从Excel文件导入ASP.NET MVC 4中的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);

提前致谢:)

1 个答案:

答案 0 :(得分:0)

我试图运行你的代码,我对 [所有$ A2:Z]

有例外

还尝试找到答案,但看起来查询无效。

尝试循环访问每个工作表。

string Path = @"E:\\DemoXls.xls";
        var con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");

        con.Open();
        DataTable mySheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        con.Close();

        var dt = new DataTable();
        var da = new OleDbDataAdapter();
        da.SelectCommand = new OleDbCommand();
        da.SelectCommand.Connection = con;
        var query = "Select * from [{0}]";
        foreach (DataRow item in mySheets.Rows)
        {
            da.SelectCommand.CommandText = string.Format(query, item["TABLE_NAME"].ToString());
            da.Fill(dt);
            //DO the mapping of DataTable to your List<T>   
        }