第一步:转换导入的文件

  private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Excel文件";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd.Filter = "Excel文件(*.xls)|*.xls";
ofd.ValidateNames = true;
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog.FileName;
} }

第二步:执行导入

    private void btnImport_Click(object sender, EventArgs e)
{
if (cboSaleStock.SelectedIndex == -)
{
MessageBox.Show("没有选择销售仓库");
return;
}
if (cboShop.SelectedIndex == -)
{
MessageBox.Show("没有选择销售门店");
return;
}
if (string.IsNullOrEmpty(txtFileName.Text) || txtFileName.Text.Length == )
{
MessageBox.Show("没有选择Excel文件!无法进行数据导入");
return;
}
DataTable dt = ExcelToDataTable(txtFileName.Text);
string sql = string.Empty;
for (int i = ; i < dt.Rows.Count; i++)
{
sql += string.Format(@" declare @sno{4} int,@dgrID{4} varchar(20)
set @dgrID{4}=(select top 1 dgoodsresultid from tbdGoodsResult where shopID='{0}' and stockid='{1}' and goodsid='{2}')
if @dgrID{4} is null begin
set @sno{4}=(select top 1 sno from tbpKeyManager where tablename='tbdGoodsResult')
if @sno{4} is null begin
insert into tbpKeyManager(tablename,sno,trdate) values('tbdGoodsResult',1,CONVERT(varchar(12),getdate(),112 ))
set @sno{4}=1
end else begin
set @sno{4}=@sno{4}+1
update tbpKeyManager set sno=sno+1 where tablename='tbdGoodsResult'
end
insert into tbdGoodsResult(dgoodsresultid,shopid,stockid,goodsid,pthismonthqty)
values(cast(@sno{4} as varchar(11)),'{0}','{1}','{2}','{3}')
end else begin
update tbdGoodsResult set pthismonthqty={3} where dgoodsresultid=@dgrID{4}
end", cboShop.SelectedValue, cboSaleStock.SelectedValue, dt.Rows[i][], dt.Rows[i][], i);
}
string msg = "";
if (ws.ExecuteNonQueryWithTrans(sql, ref msg) > )
{
MessageBox.Show("导入数据成功");
}
if (msg.Length > )
{
MessageBox.Show("导入数据失败:\r\n" + msg);
}
}

第三步:Excel数据导入

      private DataTable ExcelToDataTable(string filePath)
{
//根据路径打开一个Excel文件并将数据填充到DataSet中
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
DataTable dt = ds.Tables[];
dt.Columns.Add("goodsid");
string joinSql = "select '' pluno";
foreach (DataRow dr in dt.Rows)
{
joinSql += string.Format(" union all select '{0}' pluno", dr[]);
}
string sql = string.Format(@"select tbgoods.goodsid,tbgoods.pluno from tbgoods join ({0}) as t on t.pluno=tbgoods.pluno ", joinSql);
DataTable dtGoods = cf.GetDataSet(sql).Tables[];
foreach (DataRow dr in dt.Rows)
{
foreach (DataRow dr1 in dtGoods.Rows)
{
if (dr[].ToString().ToUpper() == dr1["pluno"].ToString())
{
dr["goodsid"] = dr1["goodsid"].ToString();
}
}
}
return ds.Tables[];
}

导入Excel表中的数据的更多相关文章

  1. oracle数据库中导入Excel表格中的数据

    1.点击[工具]-->[ODBC 导入器],如图: 2.在导入器里选择第一个[来自ODBC的数据],用户名/系统DSN-->填写[Excel Files],输入用户名和密码,点击 [连接] ...

  2. 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型

    导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...

  3. vlookup函数基本使用--如何将两个Excel表中的数据匹配;excel表中vlookup函数使用方法将一表引到另一表

    vlookup函数基本使用--如何将两个Excel表中的数据匹配:excel表中vlookup函数使用方法将一表引到另一表 一.将几个学生的籍贯匹配出来‘ 二.使用查找与引用函数 vlookup 三. ...

  4. Jmeter----读取excel表中的数据

    Jmeter 读取excel数据使用的方法是使用CSV Data Set Config参数化,之后使用BeanShell Sampler来读取excel表中的数据 第一步.查看所需的接口都要哪些字段和 ...

  5. 复制excel表,往excel表中写入数据

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...

  6. 【PHP】将EXCEL表中的数据轻松导入Mysql数据表

    在网络上有不较多的方法,在此介绍我已经验证的方法. 方法一.利用EXCEL表本身的功能生成SQL代码 ①.先在“phpmyadmin”中建立数据库与表(数据库:excel,数据表:excel01,字段 ...

  7. 将EXCEL表中的数据轻松导入Mysql数据表

    转载自:http://blog.163.com/dielianjun@126/blog/static/164250113201042310181431/ 在网络上有不较多的方法,在此介绍我已经验证的方 ...

  8. 将Excel表中的数据导入MySQL数据库

    原文地址: http://fanjiajia.cn/2018/09/26/%E5%B0%86Excel%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E5% ...

  9. PLSQL导入Excel表中数据

     PL/SQL 和SQL Sever导入excel数据的原理类似,就是找到一个导入excel数据的功能项,按照步骤走就是了.下面是一个些细节过程,希望对像我这样的菜鸟有帮助.  www.2cto.co ...

随机推荐

  1. python3爬虫之Urllib库(一)

    上一篇我简单说了说爬虫的原理,这一篇我们来讲讲python自带的请求库:urllib 在python2里边,用urllib库和urllib2库来实现请求的发送,但是在python3种在也不用那么麻烦了 ...

  2. Fliptile POJ - 3279 (开关问题)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16483   Accepted: 6017 Descrip ...

  3. BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)

    解题心得: 1.读清楚题意,本题的题意是有多个'r'(起点),多个r多个bfs比较最短的时间即可,但是hdoj的数据比较水,直接一个起点就行了,迷宫里有多个守卫,如果在路途中遇到守卫会多花费一个时间点 ...

  4. datetime模块详解

    时间运算 >>> datetime.datetime.now() #当前年月 时间datetime.datetime(2018, 2, 13, 16, 12, 6, 850532)& ...

  5. ElasticSearch学习笔记(一)-- 查询索引分词

    # 查看所有索引 GET _cat/indices # 创建一个索引 PUT /test_index # 插入一条数据(指定id)PUT /test_index/doc/ { "userna ...

  6. C#入门篇-3:数据类型及转换

    using System; using System.Text; using System.Collections; using System.Collections.Generic; //003 查 ...

  7. 6、CSS基础 part-4

    1.CSS 定位属性 CSS 定位属性允许你对元素进行定位. 属性 描述 position 把元素放置到一个静态的.相对的.绝对的.或固定的位置中. top 定义了一个定位元素的上外边距边界与其包含块 ...

  8. 学习python3之路的第一个小代码-----------9*9乘法表

    这个编写的简单,用两个循环迭代就行.下面就是我写的编码以及输出的结果 1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 i = 1 5 6 while i ...

  9. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  10. [python][oldboy][函数篇][1]名称空间

    名称空间:存储名字的空间,分为三种,内置空间,全局空间,局部空间 名称可以是:变量名,函数名,类名等 当遇到一个名字时,首先在自己空间找,再到自己外的空间找 比如 test.py print f # ...