我有一個包含四列(名稱,電話,國家,城市)的Excel表格 如何使用jquery將excel表格中的數據導出到gridview。 我正在用asp.net做它,但它非常耗時。使用jquery將excel表格中的數據導出到gridview
private void Import_To_Grid(string FilePath, string Extension)
{
try
{
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath, 1);
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
//dSUploadInventory.Tables.Add(dtExcelSchema);
connExcel.Close();
//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
////To
string expression = string.Empty;
expression = "ISNULL(ColumnId,0)=0";
DataRow[] rows = dt.Select(expression);
if (rows.Length > 0)
{
foreach (DataRow row in rows)
{
row.Delete();
row.AcceptChanges();
}
}
////To Do
ViewState["ExcelData"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
if (dt.Rows.Count > 0)
{
imgButtonSubmit.Visible = true;
}
}
catch (Exception ex)
{
AppLog.Log("Exception in Import_To_Grid on UploadInventory Page:" + ex.ToString());
}
}
幫助將不勝感激。
究竟需要多少時間?爲什麼你把表格放在視圖狀態?也許你需要分頁? –