码迷,mamicode.com
首页 > 数据库 > 详细

asp.net 从Excel表导入数据到数据库中

时间:2014-05-15 09:25:54      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

http://www.cnblogs.com/hfzsjz/archive/2010/12/31/1922901.html

http://hi.baidu.com/ctguyg/item/ebc857e90e436ae1fb42ba01

 

bubuko.com,布布扣
 1   <form action="" method="post" runat ="server">
 2         <div>
 3       <span>请选择文件:</span><asp:FileUpload ID="FileUpload1" runat="server" />
 4         <asp:Button ID="btnExport" runat="server" Text="导入" onclick="btnExport_Click" />
 5     </div>
 6     <div>
 7         <asp:GridView ID="GridView1" runat="server">
 8         </asp:GridView>
 9     </div>
10     </form>
bubuko.com,布布扣
bubuko.com,布布扣
 1         protected void btnExport_Click(object sender, EventArgs e)
 2         {
 3             try
 4             {
 5                 if (this.FileUpload1.HasFile)
 6                 {
 7                     DataTable inputdt = new DataTable();
 8                     int len = this.FileUpload1.FileName.ToString().Trim().Length;
 9                     string path = "~/temp/upfile/" + this.FileUpload1.FileName.ToString().Trim();
10                     path = Server.MapPath(path);
11                     this.FileUpload1.SaveAs(path); //上传文件
12                     inputdt = JDBMS.DBUtility.MDBHelper.InputExcel(path, this.FileUpload1.FileName.ToString().Trim().Substring(0, len - 4));
13                     if (Session["inputdt"] != null)
14                         Session.Remove("inputdt");
15                     Session.Add("inputdt", inputdt);
16                     if (inputdt.Rows.Count > 0)
17                     {
18                         this.GridView1.DataSource = inputdt;
19                         this.GridView1.DataBind();
20                     }
21                 }
22                 else
23                 {
24                     throw new Exception("请选择导入表的路径");
25                 }
26             }
27             catch (Exception ex)
28             {
29                 Response.Write("<script language=‘javascript‘>alert(‘" + ex.Message + "‘);</script>");
30             }
31         }
bubuko.com,布布扣
bubuko.com,布布扣
 1      /// <summary>
 2         /// 导入数据到数据集中
 3         /// </summary>
 4         /// <param name="Path"></param>
 5         /// <param name="TableName"></param>
 6         /// <param name="tablename2">如果这个有就以他为表名,没有的话就以TableName</param>
 7         /// <returns></returns>
 8         public static DataTable InputExcel(string Path, string TableName)
 9         {
10             try
11             {
12                 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
13                 OleDbConnection conn = new OleDbConnection(strConn);
14                 conn.Open();
15                 string strExcel = "";
16                 OleDbDataAdapter myCommand = null;
17                 strExcel = "select * from [" + TableName + "$]";
18                 myCommand = new OleDbDataAdapter(strExcel, strConn);
19                 DataTable dt = new DataTable();
20                 myCommand.Fill(dt);
21                 conn.Close();
22                 return dt;
23             }
24             catch (Exception ex)
25             {
26                 throw new Exception(ex.Message);
27             }
28         }
bubuko.com,布布扣

 

 

 

asp.net 从Excel表导入数据到数据库中,布布扣,bubuko.com

asp.net 从Excel表导入数据到数据库中

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/ljg3020/p/3729023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!