各位大哥们,小弟最近遇到个问题,就是如何将excel模版文件存入access 数据库中,
请各位有做过相关的高手们支个招啊!我用的语言是C#的!

解决方案 »

  1.   

    excel->datatable-> access 
      

  2.   


    从excel存到 datatableusing System.Data.OleDb
     /// <summary>
            /// 上传文件并返回数据(读Excal)
            /// </summary>
            /// <param name="ifile"></param>
            /// <param name="filePath">存放文件的路径</param>
            /// <param name="fileName">要保存的文件名</param>
            /// <param name="nameList">返回Table的列名的集合,一般就是数据库的字段名(比如 [l2_code  l2_desc])</param>
            /// <returns>返回上传的数据集</returns>
            public DataTable importExcalGetData(System.Web.UI.HtmlControls.HtmlInputFile ifile, string filePath, string fileName, List<string> nameList)
            {
                string iname = ifile.PostedFile.FileName;
                iname = fileName + Path.GetExtension(iname);
                if (File.Exists(filePath + iname))
                {
                    File.Delete(filePath + iname);
                }
                ifile.PostedFile.SaveAs(filePath + iname);            string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + iname + ";Extended Properties='Excel 8.0;HDR=yes;IMEX=1';";
                OleDbConnection conn = new OleDbConnection(strConn);
                OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]", conn);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                try
                {
                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                    {
                        ds.Tables[0].Columns[i].ColumnName = nameList[i];                }
                    return ds.Tables[0];
                }
                catch (Exception)
                {                return null;
                }
            }
      

  3.   

    在给你一个连接Access数据库的连接类DBhelp吧using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    using Microsoft.VisualBasic;namespace InsertAccess
    {
        public partial class yxbj : Form
        {
            public yxbj()
            {
                InitializeComponent();
            }
            private void conn()
            {
            }
            private void constring(string command)
            {
                string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\xjgl.mdb" + ";Persist Security Info=True";
                OleDbConnection cnn = new OleDbConnection(cn);
                string updatas = command;
                OleDbCommand com = new OleDbCommand(updatas, cnn);
                try
                {                cnn.Open();
                    com.ExecuteNonQuery();
                    cnn.Close();
                    yxbj_Load(null, null);
                    cls();
                }
                catch (Exception)
                {
                    MessageBox.Show("输入的不正确!");
                }
                finally
                {
                    cnn.Close();
                }
            }        private DataTable dt;
            private OleDbDataAdapter da;        private void button1_Click(object sender, EventArgs e)
            {
                string mycom = "insert into yxbj(yxbj_id,yxbj_name,yxbj_department,yxbj_major,yxbj_class) values( " + "'" + textBox1.Text + "'" + "," + "'" + textBox2.Text + "'" + "," + "'" + textBox3.Text + "'" + "," + "'" + textBox4.Text + "'" + "," + "'" + textBox5.Text + "')";
                constring(mycom);
            }//添加        private void yxbj_Load(object sender, EventArgs e)
            {
                string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\xjgl.mdb" + ";Persist Security Info=True";
                OleDbConnection cnn = new OleDbConnection(cn);
                string mysel = "Select yxbj_id as 学号,yxbj_name as 姓名,yxbj_department as 院系,yxbj_major as 专业,yxbj_class as 班级 from yxbj";
                OleDbCommand com = new OleDbCommand(mysel, cnn);
                dt = new DataTable();
                da = new OleDbDataAdapter();
                da.SelectCommand = com;
                OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
    }
    }
    }
      

  4.   

    DBhelp内容在长,帖不上来,只好另写了一个给你了,兄弟得互相交流吧,一起努力,为中国IT加油