校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > asp.net讀取excel中的數(shù)據(jù)并綁定在gridview

asp.net讀取excel中的數(shù)據(jù)并綁定在gridview

熱門標簽:電銷機器人 數(shù)據(jù) 地圖標注多少錢一張 ai電銷機器人對貸款有幫助嗎 宿遷智能外呼系統(tǒng)排名 廣州銷售外呼系統(tǒng)定制 怎樣給陜西地圖標注顏色 400電話辦理信任翰諾科技 云狐人工智能電話機器人 福州人工智能電銷機器人加盟
前臺label,DropDownList,gridview控件

aspx.cs核心代碼:
復制代碼 代碼如下:

using System.Data.OleDb;//需要引入命名
public void Excel_Click(object sender, EventArgs e)
{
if (this.AttachmentFile.Value == "" this.Label1.Text == "" DropDownList2.SelectedValue == "")
{
Response.Write("script>window.alert('請選擇要導入的文件')/script>");
}
if (this.AttachmentFile.Value != "" this.DropDownList2.SelectedValue == "")
{
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files[0];
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
postedFile.SaveAs("\\\\localhost\\文件夾\\" + fileName);
}
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "\\\\localhost\\文件夾\\" + fileName + ";Extended Properties=Excel 8.0;";//this.AttachmentFile.Value.ToString()
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable sheetNames = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow dr in sheetNames.Rows)
{
DropDownList2.Items.Add(dr[2].ToString());
}
this.Label1.Text = "\\\\localhost\\文件夾\\" + fileName;//this.AttachmentFile.Value.ToString();
conn.Close();
}
if (this.Label1.Text.ToString() != "" this.DropDownList2.SelectedValue != "")// this.DropDownList1.SelectedValue.ToString() != "全部"
{

//綁定到gridview
GridView1.DataSource = createDataSource(DropDownList2.SelectedValue.ToString(), this.Label1.Text.ToString());//, this.DropDownList1.SelectedValue.ToString()
GridView1.DataBind();


}


}
//以Excel為數(shù)據(jù)源獲取數(shù)據(jù)集
private DataSet createDataSource(string select, string lable)

{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lable + ";Extended Properties=Excel 8.0;";
string strsql = "select 登記號碼,姓名,日期,簽到時間,簽退時間,部門 from [" + select + "] order by 部門,日期,姓名";//excel表格的字段
OleDbConnection conn = new OleDbConnection(strCon);
OleDbDataAdapter da = new OleDbDataAdapter(strsql, conn);
try
{
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
catch (Exception e)
{
Response.Write("script>window.alert('沒有數(shù)據(jù),或者" + e.Message + "')/script>");
return null;
}
}

以上是插入07以前版本excel

如果07版本以后只需要做小小修改
復制代碼 代碼如下:

string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + lable + ";Extended Properties=Excel 12.0;";
您可能感興趣的文章:
  • asp.net gridview代碼綁定
  • asp.net ListView 數(shù)據(jù)綁定
  • asp.net中將數(shù)據(jù)庫綁定到DataList控件的實現(xiàn)方法與實例代碼
  • asp.net中綁定TextBox回車事件的解決方法
  • asp.net中ListBox 綁定多個選項為選中及刪除實現(xiàn)方法
  • ASP.NET中ListView(列表視圖)的使用前臺綁定附源碼
  • asp.net數(shù)據(jù)綁定DataBind使用方法
  • Asp.net中的數(shù)據(jù)綁定Eval和Bind應用示例
  • ASP.NET MVC數(shù)組模型綁定詳解

標簽:焦作 大興安嶺 綿陽 曲靖 延安 新疆 黃南 宜春

巨人網絡通訊聲明:本文標題《asp.net讀取excel中的數(shù)據(jù)并綁定在gridview》,本文關鍵詞  asp.net,讀取,excel,中的,數(shù)據(jù),;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net讀取excel中的數(shù)據(jù)并綁定在gridview》相關的同類信息!
  • 本頁收集關于asp.net讀取excel中的數(shù)據(jù)并綁定在gridview的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 大连市| 通化市| 赣州市| 大宁县| 富宁县| 荆州市| 千阳县| 长葛市| 乐昌市| 公主岭市| 油尖旺区| 观塘区| 西华县| 佛冈县| 蕉岭县| 宜兰县| 潼关县| 商都县| 霸州市| 剑川县| 永春县| 新泰市| 聂拉木县| 榆树市| 民和| 新蔡县| 高淳县| 西贡区| 渑池县| 宣武区| 镇赉县| 八宿县| 马尔康县| 全椒县| 上犹县| 嘉祥县| 色达县| 铜川市| 托克逊县| 五常市| 全椒县|