国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

asp.net讀取e、xcel中的數據并綁定在gridview

2019-11-02 14:20:52
字體:
來源:轉載
供稿:網友

 這篇文章主要介紹了asp.net讀取excel中的數據并綁定在gridview上的方法,需要的朋友可以參考下

   前臺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為數據源獲取數據集
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('沒有數據,或者" + e.Message + "')</script>");
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松滋市| 桃源县| 龙川县| 广安市| 高州市| 通渭县| 柳州市| 安远县| 钦州市| 石柱| 吴川市| 磐安县| 唐山市| 延寿县| 从化市| 宁陕县| 丰都县| 原平市| 灵寿县| 辽阳市| 重庆市| 红河县| 遵义市| 龙游县| 缙云县| 金沙县| 黑山县| 家居| 治县。| 吉木萨尔县| 锡林郭勒盟| 辽源市| 山东省| 三门峡市| 临沧市| 平塘县| 香港| 桂东县| 额尔古纳市| 海林市| 吉木萨尔县|