是否可以将数据从Excel导入jQGrid
本文关键字:Excel 导入 jQGrid 数据 是否 | 更新日期: 2023-09-27 17:55:16
是否可以将数据从Excel文件导入到MVC 4 ASP.NET jqGrid。
我在这里看了这个例子,但它并没有完全谈论从 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);
提前致谢:)
我尝试运行您的代码,但得到了有关 [All$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>
}