第一步:转换导入的文件

  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. TCP报文格式,TCP的三次握手和四次挥手&hosts文件

    1.TCP报文格式 TCP报头中的源端口号和目的端口号同IP数据报中的源IP与目的IP唯一确定一条TCP连接 序号(4字节=32位): 37 59 56 75 用来标识TCP发端向TCP收端发送的数据 ...

  2. WTForm

    Flask-WTForm: from flask import Flask,render_template,request,redirect from wtforms.fields import co ...

  3. 可实现一键分享到多个平台(微信,微博,qq空间,人人等)

    友推是一款是面向移动应用的SDK分享组件,提供给开发者集成使用.通过友推,开发者可以轻松集成社会化分享功能,同时创建及管理推荐好友使用您应用的推荐奖励活动,用户推荐好友安装使用您的应用即可获得推荐奖励 ...

  4. 51、如何提取android代码中的字符串为系统资源文件 (I18N)

    工具:android studio 步骤1:找到要转为资源文件的字符串并选中,同时按下option+enter,弹出菜单,我们选中extract string resource 步骤2:在弹窗中输入你 ...

  5. 【4Sum】cpp

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  6. WordCount by Java

    WordCount by Java 软测第二周作业 该项目github地址如下: https://github.com/YuQiao0303/WordCount 一.概述 项目WordCount的需求 ...

  7. ubuntu16.04中docker安装curl拒绝连接问题

    在Ubuntu16.04中安装docker ce,安装步骤按照官网说明https://docs.docker.com/engine/installation/linux/docker-ce/ubunt ...

  8. [转]手写数字识别错误NameError: name 'mnist' is not defined

    转自:https://blog.csdn.net/coder_Gray/article/details/78562382 在Tensorflow上进行mnist数字识别实例时,出现如下错误 NameE ...

  9. java读取文件(更新jdk7及jdk8)

    以字节的方式读取: InputStream inputStream = new FileInputStream(file); int temp = -1; StringBuilder sb = new ...

  10. 用jquery动态生成的元素绑定事件

    $(document).on("click",".class a",function(){ })