首先,看一下我的窗体设计:

要插入的Excel表:

编码 名称 联系人 电话 省市 备注
100 100线 张三 12345678910 北京 测试
101 101线 张三 12345678910 上海 测试
102 102线 张三 12345678910 深圳 测试
103 103线 张三 12345678910 广州 测试
104 104线 张三 12345678910 福建 测试
105 105线 张三 12345678910 厦门 测试
106 106线 李四 12345678910 台湾 测试
107 107线 李四 12345678910 石家庄 测试
108 108线 李四 12345678910 合肥 测试
109 109线 李四 12345678910 东城 测试
110 110线 李四 12345678910 丰台 测试
111 111线 李四 12345678910 朝阳 测试
112 112线 王五 12345678910 海淀 测试
113 113线 王五 12345678910 昌平 测试
114 114线 王五 12345678910 大兴 测试
115 115线 王五 12345678910 西城 测试
116 116线 王五 12345678910 通州 测试
117 117线 赵六 12345678910 石景山 测试
118 118线 赵六 12345678910 门头沟 测试
119 119线 赵六 12345678910 房山 测试

后台代码:

/

private void btnFindFile_Click(object sender, EventArgs e)

        {

            #region 选择文件按钮事件

            openFile.Multiselect = true;

            openFile.FileName = "";

            openFile.Filter = "(*.xlsx,*.xls)|*.xlss;*.xls";

            openFile.ShowDialog();

            openFile.Reset();

            #endregion

        }

private void openFile_FileOk(object sender, CancelEventArgs e)

        {            

            #region 打开文件(确定事件)

            try

            {

                if (dgvData.Rows.Count > 0)

                {

                    dgvData.Rows.Clear();

                }

                strFileName = openFile.FileNames;

                strFilePath = openFile.SafeFileNames;

                Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();                

                strFilePaths.Clear();//清空上次要导入的文件

                for (int j = 0; j < strFileName.Length; j++)

                {                 

                    string FileName = strFileName[j];

                    int rows = this.dgvData.Rows.Count;

                    this.dgvData.Rows.Add(1);//添加一行,不然数据不显示

                    //获取工作表名

                    object Nothing = System.Reflection.Missing.Value;

                    Microsoft.Office.Interop.Excel.Workbook wbs = null;

                    //获取EXCEL中的第一张工作簿名称 

                    wbs = excel1.Workbooks.Open(FileName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing,Nothing, Nothing, Nothing, Nothing, Nothing);

                    Microsoft.Office.Interop.Excel.Worksheet SHT1 = (Microsoft.Office.Interop.Excel.Worksheet)wbs.Sheets[1];

                    string sheetname = SHT1.Name;                  

                    dgvData.Rows[j].Cells["XuHao"].Value=j+1;

                    dgvData.Rows[j].Cells["Lujing"].Value = FileName;

                    dgvData.Rows[j].Cells["Gongzuobiao"].Value = sheetname;

                    dgvData.Rows[j].Cells["Zhuangtai"].Value = "待导入";

                }

                Kill(excel1);

            }

            catch

            {

                return;

            }

            #endregion

        }

private void btnStartIn_Click(object sender, EventArgs e)

        {

            #region 开始导入按钮

            try

            {

                int row = 0;

                int DataNum = dgvData.Rows.Count;//将要导入的表的数量

                if (DataNum == 0)

                {

                    PublicClass.ShowMessage("请先选择文件");

                    return;

                }

                DataSet[] dss = new DataSet[DataNum];

                Microsoft.Office.Interop.Excel.Application m_objExcel = new Microsoft.Office.Interop.Excel.Application();

                Microsoft.Office.Interop.Excel.Workbooks workBooks = m_objExcel.Workbooks;

                Object missing = 1;

                //显示进度条

                grbIn.Visible = true;

                proJinDu.Visible = true;





                string textName = "";//获取选择文件的名称

                string sheetName = "";//获得选中工作表名称

               // DataSet das;

                for (int i = 0; i < DataNum;i++ )

                {

                    textName = dgvData.Rows[i].Cells[1].Value.ToString(); //获得选中的文件的地址                   

                    sheetName = dgvData.Rows[i].Cells[2].Value.ToString(); //获得选中工作表名称

                    workBooks.Open(textName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                    Microsoft.Office.Interop.Excel.Sheets objSheets = m_objExcel.Worksheets;

                    Microsoft.Office.Interop.Excel._Worksheet objSheet = (Microsoft.Office.Interop.Excel._Worksheet)objSheets[1];                   





                    //读取excel数据

                    dss[i] = GetDataBySheetName07(sheetName, textName);

                    row += dss[i].Tables[0].Rows.Count;

                }

                

                #region 循环表

                int numcout = 0;//记录上一张工作表导入成功的记录条数

                for (int j = 0; j < DataNum; j++)

                {

                    DataSet ds = new DataSet();

                    ds=dss[j];

                    int ii = 1;//导入工作表中的当前行                    

                    textName = dgvData.Rows[j].Cells[1].Value.ToString(); //获得选中的文件的地址                   

                    sheetName = dgvData.Rows[j].Cells[2].Value.ToString(); //获得选中工作表名称

                    //声明导入对象

                    workBooks.Open(textName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                    Microsoft.Office.Interop.Excel.Sheets objSheets = m_objExcel.Worksheets;

                    Microsoft.Office.Interop.Excel._Worksheet objSheet = (Microsoft.Office.Interop.Excel._Worksheet)objSheets[1];                    





                    //读取excel数据

                    ds = GetDataBySheetName07(sheetName, textName);

                    //row = ds.Tables[0].Rows.Count;

                    proJinDu.Minimum = 0;

                    proJinDu.Value = 0;

                    proJinDu.Maximum = row;

                    if (row == 0)

                    {

                        PublicClass.ShowMessage("当前选择的工作表没有数据信息!");

                        return;

                    }                    

                    #region 循环每张表的记录

                    for (int m = 0; m < ds.Tables[0].Rows.Count; m++)

                    {

                        try

                        {

                            ii++;





                            int_in++;//正在导入第XX条记录

                            string CarriersCode = ds.Tables[0].Rows[m][0].ToString().Trim();//承运商编码

                            string CarriersName = ds.Tables[0].Rows[m][1].ToString().Trim();//名称

                            string CarriersCity = ds.Tables[0].Rows[m][4].ToString().Trim();//省、市





                            //检查编码、名称是否为空

                            if (CarriersCode.Equals(""))

                            {

                                objSheet.Cells[ii, 7] = "编码不能为空!";

                                int_wrong++;                                

                                lblRecord.Text = "共" + row + "条记录,正在导入" + int_in + "条记录,错误" + int_wrong + "条!";

                                continue;

                            }

                            if (CarriersName.Equals(""))

                            {

                                objSheet.Cells[ii, 7] = "名称不能为空!";

                                int_wrong++;

                                lblRecord.Text = "共" + row + "条记录,正在导入" + int_in + "条记录,错误" + int_wrong + "条!";

                                continue;

                            }

                            if (CarriersCity.Equals(""))

                            {

                                objSheet.Cells[ii, 7] = "省市不能为空!";

                                int_wrong++;

                                lblRecord.Text = "共" + row + "条记录,正在导入" + int_in + "条记录,错误" + int_wrong + "条!";

                                continue;

                            }

                            //检查编码、名称是否存在

                            string ssql = "select * from T_Base_Carriers where Carriers_Code='" + CarriersCode + "' or Carriers_Name='" + CarriersName + "'";

                            DataSet ddss = DataAccess.GetDataSet(ssql, false, null);

                            if (ddss.Tables[0].Rows.Count > 0)

                            {

                                if (CarriersCode == ddss.Tables[0].Rows[0]["Carriers_Code"].ToString())

                                {





                                    objSheet.Cells[ii, 7] = "编码已存在!";

                                    int_wrong++;

                                    lblRecord.Text = "共" + row + "条记录,正在导入" + int_in + "条记录,错误" + int_wrong + "条!";

                                    continue;

                                }

                                if (CarriersName == ddss.Tables[0].Rows[0]["Carriers_Name"].ToString())

                                {

                                    objSheet.Cells[ii, 7] = "名称已存在!";

                                    int_wrong++;

                                    lblRecord.Text = "共" + row + "条记录,正在导入" + int_in + "条记录,错误" + int_wrong + "条!";

                                    continue;

                                }

                            }





                            object[] obj = new object[8];

                            obj[0] = doubleid;

                            obj[1] = CarriersCode;

                            obj[2] = CarriersName;

                            obj[3] = CarriersCity;

                            obj[4] = ds.Tables[0].Rows[m][2].ToString();

                            obj[5] = ds.Tables[0].Rows[m][3].ToString();

                            obj[6] = ds.Tables[0].Rows[m][5].ToString();

                            obj[7] = 1;

                            //obj[7] = Frm_Login.userID;

                            int hh = Convert.ToInt32(DataAccess.ExecuteNonQuery("P_Base_Carriers_Save", true, obj));

                            if (hh > 0)

                            {

                                int_right++;

                            }

                            else

                            {

                                int_wrong++;

                            }

                        }

                        catch

                        {

                            m_objExcel.Visible = true;

                            throw;

                        }





                        lblRecord.Text = "共" + row + "条记录,正在导入" + int_in + "条记录,错误" + int_wrong + "条!";

                        this.proJinDu.Value = int_in;

                    }

                    #endregion

                    lblRecord.Text = "共" + row + "条记录,导入成功" + (int_in - int_wrong) + "条记录,错误" + int_wrong + "条!";

                    this.proJinDu.Maximum = row - int_wrong;

                    this.proJinDu.Value = row - int_wrong;

                    numcout = int_right-numcout;

                    if (numcout < ds.Tables[0].Rows.Count)

                    {

                        dgvData.Rows[j].Cells[3].Value = "有错误记录";

                    }

                    else 

                    {

                        dgvData.Rows[j].Cells[3].Value = "导入成功!";

                    }

                }

                #endregion

                if (int_right < row)

                {

                    //设置进度条不显示

                    grbIn.Visible = false;

                    proJinDu.Visible = false;                   

                    PublicClass.ShowMessage("共" + row + "条记录,导入成功" + int_right + "条记录,错误" + int_wrong + "条!");

                    m_objExcel.Visible = true;

                    System.Windows.Forms.Application.DoEvents();

                    return;

                }                 

                System.Windows.Forms.Application.DoEvents();

                m_objExcel.Quit();

                Kill(m_objExcel);

                m_objExcel = null;

                for (int s = 0; s < DataNum; s++)

                {

                    dss[s].Dispose();

                }

                    

                GC.Collect();

                //设置进度条不显示

                grbIn.Visible = false;

                proJinDu.Visible = false;

                PublicClass.ShowMessage("导入成功! 共" + row + "条记录,导入成功" + (int_in - int_wrong) + "条记录,错误" + int_wrong + "条!");

                nowpage = 1;

                DataBind();

                PageState();

                btnCancel.Focus();

            }

            catch (Exception ex)

            {

                PublicClass.ShowMessage(ex.Message);

            }

            #endregion          

        }

效果图:

备注:

1>代码多处调用了自定类,出于机密原因,不能贴出来,敬请原谅。

2>给需要帮助的人们一个思路,里面肯定有许多要完善的地方,大家共同学习。

版权声明:本文为博主原创文章,未经博主允许不得转载。

winform Execl数据 导入到数据库(SQL) 分类: WinForm C# 2014-05-09 20:52 191人阅读 评论(0) 收藏的更多相关文章

  1. UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏

    UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...

  2. OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏

    1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...

  3. HBase -ROOT-和.META.表结构(region定位原理) 分类: B7_HBASE 2015-03-13 20:52 90人阅读 评论(0) 收藏

    在HBase中,大部分的操作都是在RegionServer完成的,Client端想要插入,删除,查询数据都需要先找到相应的RegionServer.什么叫相应的RegionServer?就是管理你要操 ...

  4. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...

  5. 选择排序 分类: 算法 c/c++ 2014-10-10 20:32 509人阅读 评论(0) 收藏

    选择排序(假设递增排序) 每次选取从当前结点到末尾结点中最小的一个与当前结点交换,每一轮固定一个元素位置. 时间复杂度O(n^2),空间复杂度O(1).下面的示例代码以带头结点的链表为存储结构: #i ...

  6. Oracle 字符集的查看和修改 分类: H2_ORACLE 2013-06-19 16:52 316人阅读 评论(0) 收藏

    一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...

  7. 跨服务器备注SQL数据库 分类: SQL Server 2015-03-05 08:52 227人阅读 评论(0) 收藏

    任务:把服务器1上的SQL数据库自动备份到服务器2上,命名格式=数据库名+年月日+小时. 说明: 服务器2=>192.168.0.22 数据库名=>Book 共享文件夹路径:192.168 ...

  8. winform清空DataGridView中的数据 分类: DataGridView 2014-05-19 20:56 180人阅读 评论(0) 收藏

    我们一般要把dgv情况,一般用: DataTable dt = (DataTable)dgvData.DataSource; dt.Rows.Clear(); dgvData.DataSource = ...

  9. Winform Windows Media Player 简易播放器 分类: WinForm 2014-07-31 20:12 589人阅读 评论(0) 收藏

    新手上路,高手勿进! 窗体设计: 实现效果: 实现代码: using System; using System.Collections.Generic; using System.ComponentM ...

随机推荐

  1. Mediator 模式

    在面向对象系统的设计和开发过程中,对象之间的交互和通信是最为常见的情况,因为对象间的交互本身就是一种通信.在系统比较小的时候,可能对象间的通信不是很多.对象也比较少,我们可以直接硬编码到各个对象的方法 ...

  2. DOM&SAX解析XML

    在上一篇随笔中分析了xml以及它的两种验证方式.我们有了xml,但是里面的内容要怎么才能得到呢?如果得不到的话,那么还是没用的,解析xml的方式主要有DOM跟SAX,其中DOM是W3C官方的解析方式, ...

  3. linux安装git方法(转)

    转自:http://jingyan.baidu.com/article/e9fb46e16698687521f766ec.html 以下内容亲测,确实可行. 由于我的机器是linux6.7,所以省略了 ...

  4. 中文版Chrome浏览器不支持12px以下字体的解决方案

    中文版Chrome浏览器不支持12px以下字体的解决方案 Chrome 27之前的中文版桌面浏览器会默认设定页面的最小字号是12px,英文版则没有限制,主要是因为chrome认为汉字小于12px就会增 ...

  5. win8 + ubuntu14.04 安装步骤

    一.首先,从硬盘上划分一个空闲分区(推荐最少20G,每个人也可以按照自己的需要自行设定).记住各个分区的容量,方便安装时辨认.并从Ubuntu官方网站上下载Ubuntu 14.04 LTS 光盘镜像. ...

  6. PHP代码分离

    所谓的代码分离 其实只是一种思路,既然是一种思路 那就意味着他是有需求的 没有需求就没有解决方案 没有方案就不存在思路. 在这之前,我们制作 PHP 程序页面的时候.都是 HTML 和 PHP 混合写 ...

  7. lamp环境中的/到底是指的网站根目录还是linux的根

    在lamp中经常会用到 require,require_once等包含文件的语句. 如果你使用相对路径没有问题. 但是如果你使用了这样的语句就要小心了. 对于如图网站目录 require_once ' ...

  8. Android APK安装过程介绍

    课题路径:从Myfile中点击应用进行安装,到安装完成,过程分析 思想方法:在研究PreloadInstaller的时候我们直接从整个apk的文件结构入手,由整体到部分的分析:但现在整个PMS非常庞大 ...

  9. 2016030205 - ubuntu安装mysql

    ubuntu上安装mysql 1.检查ubuntu上是否已经安装mysql sudo netstat -tap | grep mysql 本机上没有安装mysql 2.安装mysql服务器端和客户端 ...

  10. objective-C: nonatomic retain copy assgin 等属性详解

    http://my.oschina.net/u/728866/blog/90798 property,可以提供的功能有:提供成员变量的访问方法的声明.控制成员变量的访问权限.控制多线程时成员变量的访问 ...