濮阳杆衣贸易有限公司

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

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

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

aspx.cs核心代碼:
復(fù)制代碼 代碼如下:

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('請選擇要導(dǎo)入的文件')/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版本以后只需要做小小修改
復(fù)制代碼 代碼如下:

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應(yīng)用示例
  • ASP.NET MVC數(shù)組模型綁定詳解

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net讀取excel中的數(shù)據(jù)并綁定在gridview》,本文關(guān)鍵詞  asp.net,讀取,excel,中的,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net讀取excel中的數(shù)據(jù)并綁定在gridview》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net讀取excel中的數(shù)據(jù)并綁定在gridview的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    桐庐县| 辛集市| 怀柔区| 镇赉县| 柘荣县| 泾川县| 伊宁县| 莆田市| 松桃| 安福县| 颍上县| 碌曲县| 石狮市| 泰州市| 南澳县| 柳河县| 阳江市| 芦山县| 农安县| 沙田区| 广水市| 巴塘县| 甘德县| 霍州市| 阳新县| 靖边县| 额敏县| 垣曲县| 大新县| 罗定市| 凤阳县| 抚松县| 肇源县| 新蔡县| 盐池县| 华亭县| 静宁县| 神农架林区| 诸暨市| 常德市| 阿图什市|