分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

.net 导入excel数据

发布时间:2023-09-06 01:44责任编辑:顾先生关键词:excel
using System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.IO; using System.Text; using System.Web; using System.Web.UI; private DataTable ?xsldata() ????????{ ???????????if(fuload.FileName == "") ????????????{ ????????????????lbmsg.Text = "请选择文件"; ????????????????return null; ????????????} ????????????string fileExtenSion; ????????????fileExtenSion = Path.GetExtension(fuload.FileName); ????????????if(fileExtenSion.ToLower() != ".xls" && fileExtenSion.ToLower() != ".xlsx") ????????????{ ????????????????lbmsg.Text = "上传的文件格式不正确"; ????????????????return null; ????????????} ????????????try ????????????{ ????????????????string FileName = "App_Data/" + Path.GetFileName(fuload.FileName); ????????????????if(File.Exists(Server.MapPath(FileName))) ????????????????{ ????????????????????File.Delete(Server.MapPath(FileName)); ????????????????} ????????????????fuload.SaveAs(Server.MapPath(FileName)); ????????????????//HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES ????????????????string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=‘Excel 8.0;HDR=Yes;IMEX=1;‘"; ????????????????string connstr2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=\"Excel 12.0;HDR=YES\""; ????????????????OleDbConnection conn; ????????????????if(fileExtenSion.ToLower() == ".xls") ????????????????{ ????????????????????conn = new OleDbConnection(connstr2003); ????????????????} ????????????????else ????????????????{ ????????????????????conn = new OleDbConnection(connstr2007); ????????????????} ????????????????conn.Open(); ????????????????string sql = "select * from [Sheet1$]"; ????????????????OleDbCommand cmd = new OleDbCommand(sql, conn); ????????????????DataTable dt = new DataTable(); ????????????????OleDbDataReader sdr = cmd.ExecuteReader(); ??????????????????dt.Load(sdr); ????????????????sdr.Close(); ????????????????conn.Close(); ????????????????//删除服务器里上传的文件 ????????????????if(File.Exists(Server.MapPath(FileName))) ????????????????{ ????????????????????File.Delete(Server.MapPath(FileName)); ????????????????} ????????????????return dt; ????????????} ????????????catch(Exception e) ????????????{ ????????????????return null; ????????????} ????????} ??????????protected void Btn_Export_Excel_To_DB_Click(object sender, EventArgs e) ????????{ ????????????try{ ??????????????????DataTable dt = xsldata(); ??????????????????//dataGridView2.DataSource = ds.Tables[0]; ????????????????int errorcount = 0;//记录错误信息条数 ????????????????int insertcount = 0;//记录插入成功条数 ??????????????????int updatecount = 0;//记录更新信息条数 ??????????????????string strcon = "server=localhost;database=database1;uid=sa;pwd=sa"; ????????????????SqlConnection conn = new SqlConnection(strcon);//链接数据库 ????????????????conn.Open(); ??????????????????for(int i = 0; i < dt.Rows.Count; i++) ????????????????{ ????????????????????string Name = dt.Rows[i][0].ToString();//dt.Rows[i]["Name"].ToString(); "Name"即为Excel中Name列的表头 ????????????????????string Sex = dt.Rows[i][1].ToString(); ????????????????????int Age = Convert.ToInt32(dt.Rows[i][2].ToString()); ????????????????????string Address = dt.Rows[i][3].ToString(); ????????????????????if(Name != "" && Sex != "" && Age != 0 && Address != "") ????????????????????{ ????????????????????????SqlCommand selectcmd = new SqlCommand("select count(*) from users where Name=‘" + Name + "‘ and Sex=‘" + Sex + "‘ and Age=‘" + Age + "‘ and Address=" + Address, conn); ????????????????????????int count = Convert.ToInt32(selectcmd.ExecuteScalar()); ????????????????????????if(count > 0) ????????????????????????{ ????????????????????????????updatecount++; ????????????????????????} ????????????????????????else ????????????????????????{ ????????????????????????????SqlCommand insertcmd = new SqlCommand("insert into users(Name,Sex,Age,Address) values(‘" + Name + "‘,‘" + Sex + "‘," + Age + ",‘" + Address + "‘)", conn); ????????????????????????????insertcmd.ExecuteNonQuery(); ????????????????????????????insertcount++; ????????????????????????} ????????????????????} ????????????????????else ????????????????????{ ????????????????????????errorcount++; ????????????????????} ????????????????} ????????????????Response.Write((insertcount + "条数据导入成功!" + updatecount + "条数据重复!" + errorcount + "条数据部分信息为空没有导入!")); ????????????} ????????????catch(Exception ex) ????????????{ ??????????????} ????????}

.net 导入excel数据

原文地址:https://www.cnblogs.com/Can-daydayup/p/8489579.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved