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月前
|
存储 Java easyexcel
招行面试:100万级别数据的Excel,如何秒级导入到数据库?
本文由40岁老架构师尼恩撰写,分享了应对招商银行Java后端面试绝命12题的经验。文章详细介绍了如何通过系统化准备,在面试中展示强大的技术实力。针对百万级数据的Excel导入难题,尼恩推荐使用阿里巴巴开源的EasyExcel框架,并结合高性能分片读取、Disruptor队列缓冲和高并发批量写入的架构方案,实现高效的数据处理。此外,文章还提供了完整的代码示例和配置说明,帮助读者快速掌握相关技能。建议读者参考《尼恩Java面试宝典PDF》进行系统化刷题,提升面试竞争力。关注公众号【技术自由圈】可获取更多技术资源和指导。
|
3月前
|
前端开发
实现Excel文件和其他文件导出为压缩包,并导入
实现Excel文件和其他文件导出为压缩包,并导入
50 1
|
5月前
|
SQL C# 数据库
EPPlus库的安装和使用 C# 中 Excel的导入和导出
本文介绍了如何使用EPPlus库在C#中实现Excel的导入和导出功能。首先,通过NuGet包管理器安装EPPlus库,然后提供了将DataGridView数据导出到Excel的步骤和代码示例,包括将DataGridView转换为DataTable和使用EPPlus将DataTable导出为Excel文件。接着,介绍了如何将Excel数据导入到数据库中,包括读取Excel文件、解析数据、执行SQL插入操作。
EPPlus库的安装和使用 C# 中 Excel的导入和导出
|
4月前
|
SQL XML 关系型数据库
入门指南:利用NHibernate简化.NET应用程序的数据访问
【10月更文挑战第13天】NHibernate是一个面向.NET的开源对象关系映射(ORM)工具,它提供了从数据库表到应用程序中的对象之间的映射。通过使用NHibernate,开发者可以专注于业务逻辑和领域模型的设计,而无需直接编写复杂的SQL语句来处理数据持久化问题。NHibernate支持多种数据库,并且具有高度的灵活性和可扩展性。
72 2
|
6月前
|
开发框架 .NET 数据库连接
闲话 Asp.Net Core 数据校验(三)EF Core 集成 FluentValidation 校验数据例子
闲话 Asp.Net Core 数据校验(三)EF Core 集成 FluentValidation 校验数据例子
119 1
|
7月前
|
开发框架 JSON 前端开发
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
|
6月前
|
关系型数据库 MySQL Windows
MySQL数据导入:MySQL 导入 Excel 文件.md
MySQL数据导入:MySQL 导入 Excel 文件.md
|
6月前
|
开发框架 前端开发 算法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
112 0
|
6月前
|
开发框架 .NET API
分享一个 ASP.NET Web Api 上传和读取 Excel的方案
分享一个 ASP.NET Web Api 上传和读取 Excel的方案
173 0

热门文章

最新文章