NET 使用OLEDB导入Excel数据

简介: NET 使用OLEDB导入Excel数据
/**
 *
 *在本章节中主要讲解的是如何使用OLEDB将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) 
            { 
            } 
        }
相关文章
|
2月前
|
SQL 缓存 easyexcel
面试官问10W 行级别数据的 Excel 导入如何10秒处理
面试官问10W 行级别数据的 Excel 导入如何10秒处理
198 0
|
2月前
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
|
1月前
|
SQL 存储 数据库
excel导入sql数据库
将Excel数据导入SQL数据库是一个相对常见的任务,可以通过多种方法来实现。以下是一些常用的方法: ### 使用SQL Server Management Studio (SSMS) 1
|
18天前
|
easyexcel Java API
SpringBoot集成EasyExcel 3.x:高效实现Excel数据的优雅导入与导出
SpringBoot集成EasyExcel 3.x:高效实现Excel数据的优雅导入与导出
59 1
|
5天前
|
JSON JavaScript 数据格式
vue 电子表格Excel的上传导入、导出下载、读取本地Excel、json转Excel
vue 电子表格Excel的上传导入、导出下载、读取本地Excel、json转Excel
7 0
|
2月前
|
存储 API C#
C# 实现格式化文本导入到Excel
C# 实现格式化文本导入到Excel
|
2月前
|
easyexcel 数据库
【EasyExcel】第一篇:动态导入excel,生成对应数据库表
【EasyExcel】第一篇:动态导入excel,生成对应数据库表
|
2月前
|
数据库
关于用NPOI导入Excel
关于用NPOI导入Excel
|
2月前
|
数据库
如何把Excel导入到数据库中
如何把Excel导入到数据库中
24 0
|
2月前
|
easyexcel 数据库
公司大佬对excel导入、导出的封装,那叫一个秒啊
封装公司统一使用的组件的主要目标是为了简化开发人员的调用流程,避免各个项目组重复集成和编写不规范的代码。文中提到对阿里EasyExcel进行了二次封装,提供了导入和导出功能,并支持模板的导入和导出。此外,还处理了读取数据与实际保存数据不一致的情况,通过提供自定义转换器来解决。
172 0