导入Excel表中的数据
第一步:转换导入的文件
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表中的数据的更多相关文章
- oracle数据库中导入Excel表格中的数据
1.点击[工具]-->[ODBC 导入器],如图: 2.在导入器里选择第一个[来自ODBC的数据],用户名/系统DSN-->填写[Excel Files],输入用户名和密码,点击 [连接] ...
- 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型
导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...
- vlookup函数基本使用--如何将两个Excel表中的数据匹配;excel表中vlookup函数使用方法将一表引到另一表
vlookup函数基本使用--如何将两个Excel表中的数据匹配:excel表中vlookup函数使用方法将一表引到另一表 一.将几个学生的籍贯匹配出来‘ 二.使用查找与引用函数 vlookup 三. ...
- Jmeter----读取excel表中的数据
Jmeter 读取excel数据使用的方法是使用CSV Data Set Config参数化,之后使用BeanShell Sampler来读取excel表中的数据 第一步.查看所需的接口都要哪些字段和 ...
- 复制excel表,往excel表中写入数据
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...
- 【PHP】将EXCEL表中的数据轻松导入Mysql数据表
在网络上有不较多的方法,在此介绍我已经验证的方法. 方法一.利用EXCEL表本身的功能生成SQL代码 ①.先在“phpmyadmin”中建立数据库与表(数据库:excel,数据表:excel01,字段 ...
- 将EXCEL表中的数据轻松导入Mysql数据表
转载自:http://blog.163.com/dielianjun@126/blog/static/164250113201042310181431/ 在网络上有不较多的方法,在此介绍我已经验证的方 ...
- 将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% ...
- PLSQL导入Excel表中数据
PL/SQL 和SQL Sever导入excel数据的原理类似,就是找到一个导入excel数据的功能项,按照步骤走就是了.下面是一个些细节过程,希望对像我这样的菜鸟有帮助. www.2cto.co ...
随机推荐
- A1042 Shuffling Machine (20)
1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...
- FZU 2082 过路费(树链剖分)
树链剖分模板题. FZU炸了,等交上去AC了再贴代码.
- Codeforces Round #461 (Div. 2) A. Cloning Toys
A. Cloning Toys time limit per test 1 second memory limit per test 256 megabytes Problem Description ...
- 动态规划:最长上升子序列(二分算法 nlogn)
解题心得: 1.在数据量比较大的时候n^2会明显超时,所以可以使用nlogn 的算法,此算法少了双重循环,用的lower_bound(二分法). 2.lis中的数字并没有意义,仅仅是找到最小点lis[ ...
- 源码级强力分析hadoop的RPC机制
分析对象: hadoop版本:hadoop 0.20.203.0 必备技术点: 1. 动态代理(参考 :http://weixiaolu.iteye.com/blog/1477774 )2. Java ...
- day09 threading, paramiko, queue 模块
1 模拟ssh 2 锁 内部锁,程序锁,信号量 3 多线程 4 简单消息队列 先来看模拟ssh ,python 的强大之处就是因为有很多模块,可以很简单的完成复杂的事情,今天我们用paramiko ...
- mof格式的文件怎么打开?用什么工具?
托管对象格式 (MOF) 文件是创建和注册提供程序.事件类别和事件的简便方法.在 MOF 文件中创建类实例和类定义后,可以对该文件进行编译.有关更多信息,请参见编译托管对象格式 (MOF) 文件.编译 ...
- Careercup - Microsoft面试题 - 5672369481842688
2014-05-12 06:27 题目链接 原题: Find the max height of a binary tree. 题目:计算二叉树的最大高度. 解法:最大高度?高度不就是最深的叶子节点到 ...
- StartWith 测试
var clientConfiguration = GetConfiguration("couchbase.json"); ClusterHelper.Initialize(cli ...
- mysql数据库增、删、改、查等基本命令
测试环境:windows7 64位 mysql.exe.Navicat Lite for MySQL.mysql 5.0.18 mysql数据库的基本结构: 数据库(database)包含多个表(ta ...