c#读取excel全部工作簿入access

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;

namespace readExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {

        OleDbConnection dbconn = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; Data Source=db.mdb");
        dbconn.Open();
    
        string Path = this.textBox_FilePath.Text.ToString();
    
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
        int t = schemaTable.Rows.Count;
        foreach (System.Data.DataRow row in schemaTable.Rows)// 循环遍历Excel表单
        {
            
            string sheetTableName = row["TABLE_NAME"].ToString();//获得Excel工作簿Sheet页面(工作表名称)
    
            string strExcel = "";    
            OleDbDataAdapter myCommand = null; 
    
            strExcel = "select * from [" + sheetTableName + "]"; 
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            DataTable dt= new DataTable();
    
            myCommand.Fill(dt);
            string fwhz = dt.Rows[1][7].ToString().Trim();
            string bh = dt.Columns[2].ColumnName.ToString().Trim();
            string bdcdyh = bh;
            if (bh.Length > 7)
            {
                bdcdyh = "331002005003" + bh.Substring(bh.IndexOf("JC"), 7) + "F00010001";
            }
    
    
            string zjhm = dt.Rows[1][12].ToString().Trim();
    
            string zjhm2 = "";
    
            string xb = "不祥";
            if (zjhm.Length == 18)
            {
                zjhm2 = zjhm.Substring(zjhm.Length - 2, 1);
    
            if (zjhm2.Equals("1") || zjhm2.Equals("3") || zjhm2.Equals("5") || zjhm2.Equals("7") || zjhm2.Equals("9"))
            {
                xb = "男性";
    
            }
            else if (zjhm2.Equals("0") || zjhm2.Equals("2") || zjhm2.Equals("4") || zjhm2.Equals("6") || zjhm2.Equals("8"))
            {
                xb = "女性";
            }
            }
    
            string zl = dt.Rows[5][6].ToString().Trim();
            string clyj = dt.Rows[13][1].ToString().Trim();
            string ctdmj = clyj.Substring(clyj.IndexOf("积") + 1, clyj.IndexOf("㎡") - clyj.IndexOf("积")-1).Trim();
            clyj = clyj.Substring(clyj.IndexOf("㎡") + 1);
            string cjzmj = clyj.Substring(clyj.IndexOf("积") + 1, clyj.IndexOf("㎡") - clyj.IndexOf("积") - 1).Trim();
            clyj = clyj.Substring(clyj.IndexOf("㎡") + 1);
            string tdmj = clyj.Substring(clyj.IndexOf("积") + 1, clyj.IndexOf("㎡") - clyj.IndexOf("积") - 1).Trim();
            clyj = clyj.Substring(clyj.IndexOf("㎡") + 1);
            string jzmj = clyj.Substring(clyj.IndexOf("积") + 1, clyj.IndexOf("㎡") - clyj.IndexOf("积") - 1).Trim();
            string fj = "以户主为代表登记";
            if (!ctdmj.Equals("") && !cjzmj.Equals(""))
            {
                fj = "另有超土地面积" + ctdmj + "平方米,超建筑面积" + cjzmj + "平方米。" + fj;
            }
            else if (ctdmj.Equals("") && !cjzmj.Equals(""))
            {
                fj = "另有超建筑面积" + cjzmj + "平方米。" + fj;
            }
    
            string sctdmj = dt.Rows[5][3].ToString();
            string scjzmj = dt.Rows[5][5].ToString();
            string tdpw = dt.Rows[5][8].ToString();
            string tdpwmj = dt.Rows[5][12].ToString();
            string zxtdzh = dt.Rows[6][8].ToString();
            string tdfzmj = dt.Rows[6][12].ToString();
            string fwqymj = dt.Rows[8][10].ToString();
    
    
    
    
            string sql = "insert into nf(bdcdyh,wfhz,sex,zjhm,fwzl,ydqqmj,jzqqmj,fj,zxtdzh,bh,sctdmj,scjzmj,tdpw,tdpwmj,tdfzmj,fwqymj,tdcmj,fwcmj) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}')";
            sql = string.Format(sql, bdcdyh, fwhz, xb, zjhm, zl, tdmj, jzmj, fj, zxtdzh, bh, sctdmj, scjzmj, tdpw, tdpwmj, tdfzmj, fwqymj, ctdmj, cjzmj);
    
            OleDbCommand cmd = new OleDbCommand(@sql, dbconn);
            cmd.ExecuteNonQuery();
            cmd.Dispose();

}
            conn.Close();
            this.label1.Text = "OK!";
            
            
}

private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog fileDialog = new OpenFileDialog();
        fileDialog.InitialDirectory="C:\\";    //打开对话框后的初始目录
        fileDialog.Filter = "xls文件|*.xls";
        fileDialog.RestoreDirectory = false;    //若为false,则打开对话框后为上次的目录。若为true,则为初始目录
        if (fileDialog.ShowDialog() == DialogResult.OK)
            this.textBox_FilePath.Text = System.IO.Path.GetFullPath(fileDialog.FileName);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值