asp.net中处理excel的导入问题

一、方法一:

 //导入
        public static string into(string filename,string sqlinsert)
        {
            try
            {
                string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filename + ";Extended Properties=Excel 8.0";//获取excel文件
                OleDbConnection cnnxls = new OleDbConnection(mystring);//用数据库连接excel
                cnnxls.Open();

                Microsoft.Office.Interop.Excel.ApplicationClass oExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                object oMissing = System.Reflection.Missing.Value;

                oExcel.Workbooks.Add(oMissing);
                Microsoft.Office.Interop.Excel.Workbook oBook = oExcel.Workbooks[1];
                Microsoft.Office.Interop.Excel.Worksheet oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Sheets[1];
                string sheetname = oSheet.Name;
                OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);//创建匹配器
                DataSet myDs = new DataSet();
                myDa.Fill(myDs);
                if (myDs.Tables[0].Rows.Count > 0)
                {
                    SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Trafficdb"]);
                    strcon.Open();
                    SqlCommand myCmd = null;
                    for (int i = 1; i < myDs.Tables[0].Rows.Count; i++)
                    {
                        int j;
                        for (j = 0; j < myDs.Tables[0].Columns.Count - 1; j++)
                        {
                            sqlinsert += myDs.Tables[0].Rows[i].ItemArray[j].ToString() + "', '";
                        }
                        sqlinsert += myDs.Tables[0].Rows[i].ItemArray[j].ToString() + "' )";
                        myCmd = new SqlCommand(sqlinsert, strcon);
                        myCmd.ExecuteNonQuery();//写入SQL数据库
                        sqlinsert = sqlinsert.Substring(1, sqlinsert.IndexOf("'"));
                    }
                    strcon.Close();
                    strcon.Dispose();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            return null;

 

 

 

二、方法二:

  protected void Imagedaoru_Click(object sender, ImageClickEventArgs e)//导入
    {
      try
       {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);//获得文件名
        FileUpload1.PostedFile.SaveAs(@Server.MapPath(filename));//保存文件
        //string mystring = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source='E:/test.xls';Extended Properties=Excel 8.0";

      string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Server.MapPath(filename) + ";Extended       Properties=Excel 8.0";//获取excel文件
      OleDbConnection cnnxls = new OleDbConnection(mystring);//用数据库连接excel
      cnnxls.Open();
      OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);//创建匹配器
      DataSet myDs = new DataSet();
      myDa.Fill(myDs);
      if (myDs.Tables[0].Rows.Count > 0)
       {
         string strSql = "";
         SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constr"]);
         strcon.Open();
         SqlCommand myCmd = null;
        for (int i = 3; i < myDs.Tables[0].Rows.Count; i++)
        {
          strSql = "insert into YouFu_YF_ Handicapped(姓名,性别,身份证号,入伍时间,退伍时间,残疾军人,伤残人民警察,伤残国家机关          工作人员,伤残民兵民工,因战,因公,因病,证号,户籍地址,抚恤金,护理费,银行账号,备注) values ('";
          strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";
          strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "','";
          strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '";
          strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "','";
          strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '";
          strSql += myDs.Tables[0].Rows[i].ItemArray[5].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[6].ToString() + "', '";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[7].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[8].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[9].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[10].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[11].ToString() + "', '";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[12].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[13].ToString() + "', '";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[14].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[15].ToString() + "', '";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[16].ToString() + "','";
                        strSql += myDs.Tables[0].Rows[i].ItemArray[17].ToString() + "' )";
                        myCmd = new SqlCommand(strSql, strcon);
                        myCmd.ExecuteNonQuery();//写入SQL数据库

                    }
                    strcon.Close();
                    strcon.Dispose();
                    Response.Write("<script language=javascript>alert('数据导入成功')</script>");

                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('导入数据失败!')</script>");

            }
        }


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值