asp.net EXCEL数据 导入到数据库

在项目中经常有客户要求用EXCEL导入到数据库,提高效率。

注意:EXCEL中的第一行不能导入。

下面是源码;

IntoExcel.aspx:

  1. <mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IntoExcel.aspx.cs" Inherits="DEMO.IntoExcel" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>无标题页</title>  
  6. <mce:script language="javascript" type="text/javascript"><!--  
  7. // <!CDATA[  
  8. function check() {  
  9. var k=//S+/.[xls]/;  
  10. if(!k.test(document.getElementById("fileId").value))  
  11. {  
  12.     alert("只能上次xls格式的文件");  
  13.     return false;  
  14. }  
  15. return true;  
  16. }  
  17. // --></mce:script>  
  18. </head>  
  19. <body>  
  20.     <form id="form1" runat="server">  
  21.     <div>  
  22.     <p>  
  23.         <asp:FileUpload ID="fileId" runat="server" /><asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="return check()" onclick="Button1_Click" /></p>  
  24.     </div>  
  25.     </form>  
  26. </body>  
  27. </html>  
 

IntoExcel.aspx.cs

 

 

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.IO;  
  12. using System.Data.OleDb;  
  13. using System.Data.SqlClient;  
  14. namespace DEMO  
  15. {  
  16.     public partial class IntoExcel : System.Web.UI.Page  
  17.     {  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.         }  
  21.         /// <summary>  
  22.         /// 上传文件  
  23.         /// </summary>  
  24.         /// <param name="sender"></param>  
  25.         /// <param name="e"></param>  
  26.         protected void Button1_Click(object sender, EventArgs e)  
  27.         {  
  28.             string fileName = fileId.FileName;  
  29.             string savePath = Server.MapPath("~/file/");  
  30.             FileOperatpr(fileName, savePath);  
  31.             fileId.SaveAs(savePath + fileName);  
  32.             DataOperator(fileName, savePath);  
  33.         }  
  34.         /// <summary>  
  35.         /// 数据操作  
  36.         /// </summary>  
  37.         /// <param name="fileName"></param>  
  38.         /// <param name="savePath"></param>  
  39.         private void DataOperator(string fileName, string savePath)  
  40.         {  
  41.             string myString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =  " + savePath + fileName + ";Extended Properties=Excel 8.0";  
  42.             OleDbConnection oconn = new OleDbConnection(myString);  
  43.             oconn.Open();  
  44.             DataSet ds = new DataSet();  
  45.             OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", oconn);  
  46.             oda.Fill(ds);  
  47.             oconn.Close();  
  48.             DataSetOperator(ds,savePath+fileName);  
  49.         }  
  50.         /// <summary>  
  51.         /// 数据集操作  
  52.         /// </summary>  
  53.         /// <param name="ds"></param>  
  54.         private void DataSetOperator(DataSet ds,string filePath)  
  55.         {  
  56.             SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestExcel;Integrated Security=True");  
  57.             conn.Open();  
  58.             SqlTransaction str = conn.BeginTransaction();//利用事务处理 防止中断  
  59.             int k = 0;  
  60.             if (ds.Tables[0].Rows.Count < 1)  
  61.             {  
  62.                 Response.Write("<mce:script type="text/javascript"><!--  
  63. alert('没有数据!')  
  64. // --></mce:script>");  
  65.                 return;  
  66.             }  
  67.             try  
  68.             {  
  69.                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  70.                 {  
  71.                     string sqlStr = "insert into IntoExcel(Tname,Tage,Taddress)values";  
  72.                     sqlStr +="('"+ ds.Tables[0].Rows[i][0].ToString()+"',";  
  73.                     sqlStr += ds.Tables[0].Rows[i][1].ToString()+",";  
  74.                     sqlStr +="'" +ds.Tables[0].Rows[i][2].ToString()+"')";  
  75.                     SqlCommand cmd = new SqlCommand(sqlStr, conn, str);  
  76.                     cmd.Transaction = str;  
  77.                     k += cmd.ExecuteNonQuery();  
  78.                 }  
  79.                 str.Commit();  
  80.             }  
  81.             catch (Exception ex)  
  82.             {  
  83.                 Response.Write("发生异常,数据已回滚/n信息/n" + ex.Message);  
  84.                 str.Rollback();  
  85.             }  
  86.             finally  
  87.             {  
  88.                 Response.Write("上传成功" + k + "条");  
  89.                 File.Delete(filePath);  
  90.             }  
  91.         }  
  92.         /// <summary>  
  93.         /// 文件操作  
  94.         /// </summary>  
  95.         /// <param name="fileName"></param>  
  96.         /// <param name="savePath"></param>  
  97.         private void FileOperatpr(string fileName, string savePath)  
  98.         {  
  99.             if (!Directory.Exists(savePath))  
  100.             {  
  101.                 Directory.CreateDirectory(savePath);  
  102.             }  
  103.             if (File.Exists(savePath + fileName))  
  104.             {  
  105.                 File.Delete(savePath + fileName);  
  106.             }  
  107.         }  
  108.     }  
  109. }  

 

数据库中的字段:

[javascript] view plain copy print ?
  1. create table IntoExcel  
  2. (  
  3.     Tid int identity(1,1) primary key,  
  4.     Tname varchar(50),  
  5.     Tage int,  
  6.     Taddress varchar(200),  
  7.       
  8. )  
 

EXCEL数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值