求一个完整的excel导入sql server的程序,用winform的,有的给个参考参考,谢谢

解决方案 »

  1.   

    www.51aspx.com上面啥都有,自己去下顺路接分
      

  2.   

    那是用web控制台的
    我是想用winform的
      

  3.   


    if (FileUpload1.HasFile == false)
            {
                Response.Write("<script>alert('请选择文件上传!')</script>");
                return;
            }
            string FileEx = Path.GetExtension(FileUpload1.PostedFile.FileName).ToString().ToLower();
            if (FileEx != "xls")
            {
                Response.Write("<script>alert('只可以选择Excel文件')</script>");
                return;
            }
            SqlConnection Conn = new SqlConnection(Connstr);
            Conn.Open();
            string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
            string serverpath = Server.MapPath("upfiles/")+filename;
            FileUpload1.PostedFile.SaveAs(serverpath);
            DataSet ds = ExcelDS(serverpath, filename);
            DataRow[] dr = ds.Tables[0].Select();
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('Excel表为空表,无数据!')</script>");
            }
            else 
            {
                for (int i = 0; i <dr.Length;i++)
                {
                    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 Cmd = new SqlCommand(sqlcheck,Conn);
                    int count = Convert.ToInt32(Cmd.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();
                        Cmd.CommandText = insertstr;
                        Cmd.Connection = Conn;
                        try
                        {
                            Cmd.ExecuteNonQuery();                    }
                        catch (MembershipCreateUserException ex)
                        {
                            Response.Write("<script>alert(" + ex.Message.ToString() + ")</script>");
                        }
                        finally
                        {
                            Conn.Close();
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('导入内容重复!');window.location.href='Default.aspx'</script>");
                        continue;
                    }
     
                }
                Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
            }        Conn.Close();
      

  4.   

    改成FileOpenDialog控件就可以了
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    using System.Data.SqlClient;namespace ExcelToSql
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();           
            }        //把datatable读入sql数据库
            private void btnImport_Click(object sender, EventArgs e)
            {
                string _strExcelFileName = @"D:\程序\ExcelToSql\区域.xls";
                DataTable dtExcel = ExcelToDataTable(_strExcelFileName, "Sheet1");
                for (int i = 0; i < dtExcel.Rows.Count; i++)
                {
                    InsertDataToSql(dtExcel.Rows[i][0].ToString(), dtExcel.Rows[i][1].ToString(), int.Parse(dtExcel.Rows[i][2].ToString()));
                }
            }        //把excel读入datatable
            public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
            {
                string strConn = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + strExcelFileName + ";Extended Properties=\"Excel 8.0;HDR=                                  YES\";";
                string strExcel = string.Format("select * from [Sheet1$]", strSheetName);
                DataSet ds = new DataSet();            using (OleDbConnection conn = new OleDbConnection(strConn))
                {
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                    adapter.Fill(ds, strSheetName);
                    conn.Close();
                }            return ds.Tables[strSheetName];
            }        //向sql数据库插入数据
            public static void InsertDataToSql(string _ShortPara, string _FullPara, int _Order)
            {
                SqlConnection SqlConn = new SqlConnection();
                SqlConn.ConnectionString = @"Provider=Microsoft.JET.OLEDB.4.0;integrated security=true;Data Source=QC1818_V2_Public";
                SqlConn.Open();            string strInsert = string.Format("insert into T_Region (RegionShortName,RegionFullName,DisplayOrder) values ({0},{1},{2})", 
                                                    "@RegionShortName",
                                                    "@RegionFullName",
                                                    "@DisplayOrder");
                SqlCommand SqlComm = new SqlCommand(strInsert, SqlConn);
                SqlComm.Parameters.Add("@RegionShortName", SqlDbType.NVarChar,64);
                SqlComm.Parameters["@RegionShortNam"].Value = _ShortPara;
                SqlComm.Parameters.Add("@RegionFullName", SqlDbType.NVarChar,64);
                SqlComm.Parameters["@RegionFullName"].Value = _FullPara;
                SqlComm.Parameters.Add("@DisplayOrder",SqlDbType.Int);
                SqlComm.Parameters["@DisplayOrder"].Value = _Order;
                SqlComm.ExecuteNonQuery();
                SqlConn.Close();
            } 
        }
    }
    有错误;adapter.Fill(ds, strSheetName)-----Microsoft Jet 数据库引擎找不到对象'Sheet1$'。请确定对象是否存在,并正确地写出它的名称和路径。为什么
      

  6.   

    Sorry! 刚才连接数据库的代码发掉了