从Excel中读出数据并倒入sql2000中

读出并写入数据中

Code
    /// <summary>
    
/// 查询EXCEL电子表格添加到DATASET
   
/// </summary>
    
/// <param name="filenameurl">服务器路径</param>
    
/// <param name="table">表名</param>   
    public DataSet ExecleDs(string filenameurl, string table)
    {
        
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
        OleDbConnection conn 
= new OleDbConnection(strConn);
        conn.Open();
        DataSet ds 
= new DataSet();
        OleDbDataAdapter odda 
= new OleDbDataAdapter("select * from [Sheet1$]", conn);
        odda.Fill(ds, table);
        
return ds;
    }



//按钮中写入如下代码 

Code
if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
        {
            Response.Write(
"<script>alert('请您选择Excel文件')</script> ");
            
return;//当无文件时,返回
        }

        
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
        if (IsXls != ".xls")
        
{
            Response.Write(
"<script>alert('只可以选择Excel文件')</script>");
            
return;//当选择的不是Excel文件时,返回
        }

        SqlConnection cn 
= new SqlConnection(strConn);
        cn.Open();
        
string filename = DateTime.Now.ToString("yyyymmddhhMMss"+ FileUpload1.FileName;              //获取Execle文件名  DateTime日期函数
        string savePath = Server.MapPath(("~\\upfiles\\"+ filename);//Server.MapPath 获得虚拟服务器相对路径
        FileUpload1.SaveAs(savePath);                        //SaveAs 将上传的文件内容保存在服务器上
        DataSet ds = ExecleDs(savePath, filename);           //调用自定义方法
        DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
        int rowsnum = ds.Tables[0].Rows.Count;
        
if (rowsnum == 0)
        
{
            Response.Write(
"<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示
        }

        
else
        
{
            
for (int i = 0; i < dr.Length; i++)
            
{
                
string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名称不能变,否则就会出错】
                string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
                string hhaspx_xm = dr[i]["姓名"].ToString();
                
string hhaspx_dx = dr[i]["底薪"].ToString();
                
string hhaspx_kh = dr[i]["考核"].ToString();
                
string hhaspx_jl = dr[i]["奖励"].ToString();
                
string hhaspx_jt = dr[i]["津贴"].ToString();
                
string hhaspx_jb = dr[i]["加班"].ToString();
                
string hhaspx_zb = dr[i]["值班"].ToString();
                
string hhaspx_jx = dr[i]["绩效"].ToString();
                
string hhaspx_hj = dr[i]["合计"].ToString();
                
string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "' And hhaspx_xm='" + hhaspx_xm + "'";  //检查用户是否存在
                SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
                
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
                
if (count < 1)
                
{
                    
string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";

                    SqlCommand cmd 
= new SqlCommand(insertstr, cn);
                    
try
                    
{
                        cmd.ExecuteNonQuery();
                    }

                    
catch (MembershipCreateUserException ex)       //捕捉异常
                    {
                        Response.Write(
"<script>alert('导入内容:" + ex.Message + "')</script>");
                    }

                }

                
else
                
{
                    Response.Write(
"<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> ");
                    
continue;
                }

            }

            Response.Write(
"<script>alert('Excle表导入成功!');location='default.aspx'</script>");
        }


        cn.Close();

 1if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
 2        {
 3            Response.Write("<script>alert('请您选择Excel文件')</script> ");
 4            return;//当无文件时,返回
 5        }
 6        string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
 7        if (IsXls != ".xls")
 8        {
 9            Response.Write("<script>alert('只可以选择Excel文件')</script>");
10            return;//当选择的不是Excel文件时,返回
11        }
12        SqlConnection cn = new SqlConnection(strConn);
13        cn.Open();
14        string filename = DateTime.Now.ToString("yyyymmddhhMMss"+ FileUpload1.FileName;              //获取Execle文件名  DateTime日期函数
15        string savePath = Server.MapPath(("~\\upfiles\\"+ filename);//Server.MapPath 获得虚拟服务器相对路径
16        FileUpload1.SaveAs(savePath);                        //SaveAs 将上传的文件内容保存在服务器上
17        DataSet ds = ExecleDs(savePath, filename);           //调用自定义方法
18        DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
19        int rowsnum = ds.Tables[0].Rows.Count;
20        if (rowsnum == 0)
21        {
22            Response.Write("<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示
23        }
24        else
25        {
26            for (int i = 0; i < dr.Length; i++)
27            {
28                string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名称不能变,否则就会出错】
29                string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
30                string hhaspx_xm = dr[i]["姓名"].ToString();
31                string hhaspx_dx = dr[i]["底薪"].ToString();
32                string hhaspx_kh = dr[i]["考核"].ToString();
33                string hhaspx_jl = dr[i]["奖励"].ToString();
34                string hhaspx_jt = dr[i]["津贴"].ToString();
35                string hhaspx_jb = dr[i]["加班"].ToString();
36                string hhaspx_zb = dr[i]["值班"].ToString();
37                string hhaspx_jx = dr[i]["绩效"].ToString();
38                string hhaspx_hj = dr[i]["合计"].ToString();
39                string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "' And hhaspx_xm='" + hhaspx_xm + "'";  //检查用户是否存在
40                SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
41                int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
42                if (count < 1)
43                {
44                    string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";
45
46                    SqlCommand cmd = new SqlCommand(insertstr, cn);
47                    try
48                    {
49                        cmd.ExecuteNonQuery();
50                    }
51                    catch (MembershipCreateUserException ex)       //捕捉异常
52                    {
53                        Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
54                    }
55                }
56                else
57                {
58                    Response.Write("<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> ");
59                    continue;
60                }
61            }
62            Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
63        }
64
65        cn.Close();


/Files/weixing/Excel2sql.rar


 1if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
 2        {
 3            Response.Write("<script>alert('请您选择Excel文件')</script> ");
 4            return;//当无文件时,返回
 5        }
 6      string IsXls=System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
 7       if (IsXls != ".xls")
 8       {
 9           Response.Write("<script>alert('只可以选择Excel文件')</script>");
10            return;//当选择的不是Excel文件时,返回
11       }

12        SqlConnection cn = new SqlConnection(strConn);
13        cn.Open();
14        string filename = DateTime.Now.ToString("yyyymmddhhMMss"+ FileUpload1.FileName;              //获取Execle文件名  DateTime日期函数
15        string savePath = Server.MapPath(("~\\upfiles\\"+ filename);//Server.MapPath 获得虚拟服务器相对路径
16         FileUpload1.SaveAs(savePath);                        //SaveAs 将上传的文件内容保存在服务器上
17         DataSet ds = ExecleDs(savePath, filename);           //调用自定义方法
18        DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
19       int rowsnum = ds.Tables[0].Rows.Count;
20        if (rowsnum == 0)                                 
21        {
22            Response.Write("<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示
23        }

24        else
25       {
26          for (int i = 0; i < dr.Length; i++)
27           {
28               string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名称不能变,否则就会出错】
29               string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
30               string hhaspx_xm = dr[i]["姓名"].ToString();
31               string hhaspx_dx = dr[i]["底薪"].ToString();
32               string hhaspx_kh = dr[i]["考核"].ToString();
33               string hhaspx_jl = dr[i]["奖励"].ToString();
34               string hhaspx_jt = dr[i]["津贴"].ToString();
35               string hhaspx_jb = dr[i]["加班"].ToString();
36               string hhaspx_zb = dr[i]["值班"].ToString();
37               string hhaspx_jx = dr[i]["绩效"].ToString();
38               string hhaspx_hj = dr[i]["合计"].ToString();
39               string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "' And hhaspx_xm='" + hhaspx_xm + "'";  //检查用户是否存在
40               SqlCommand sqlcmd = new SqlCommand(sqlcheck,cn);
41               int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
42               if (count < 1)
43               {
44                   string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";
45
46                   SqlCommand cmd = new SqlCommand(insertstr, cn);
47                   try
48                   {
49                       cmd.ExecuteNonQuery();
50                   }

51                   catch (MembershipCreateUserException ex)       //捕捉异常
52                   {
53                       Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
54                   }

55               }

56               else
57               {
58                   Response.Write("<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> ");
59                   continue;
60               }

61           }

62           Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
63      }

64
65      cn.Close(); 

转载于:https://www.cnblogs.com/weixing/archive/2009/09/09/Excel-sql2000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值