private void dao_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = @"execl文件|*.xls;*.xlsx";
string name;
if (ofd.ShowDialog() == DialogResult.OK)
{
name = ofd.FileName;
input(name);
}
}
private void input(string fileName)
{
List<string> list = new List<string>();
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" +
"Data Source=" + fileName + ";" +
"Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [sheet1$]", strConn);/*execl中sheet1页中数据*/
DataTable dt = new DataTable();
DataSet ds = new DataSet();
try
{
da.Fill(ds);
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
if (this.dClass.GetDataTable(string.Concat(new string[]{"select * from Luku where cPanhao = '",
dt.Rows[i][0].ToString(),
"'"})).Rows.Count > 0)
{
//MessageBox.Show("入库已存在托盘号:"+dt.Rows[i][0].ToString());
}
else
{
list.Add(string.Concat(new object[]
{
"insert into Luku(cPanhao, cName, date, cGongl, iBiao)values('",
dt.Rows[i][0].ToString(),
"','",
dt.Rows[i][1].ToString(),
"','",
dt.Rows[i][2].ToString(),
"','",
dt.Rows[i][3].ToString(),
"','",
0,
"')"
}));
}
}
if (this.dClass.ExecuteNonQueryByTransaction(list) && list.Count > 0)
{
}
}
catch (Exception err)
{
MessageBox.Show("操作失败!" + err.ToString());
}
}
C#将execl导入到sql server 数据库中
最新推荐文章于 2020-11-25 16:11:54 发布