C#web项目excel导入数据到数据库


一个Excel导入到数据库的Demo和一个winFrom的Excel的导入,简单好用。一个Excel导入到数据库的Demo和一个winFrom的Excel的导入,简单好用。一个Excel导入到数据库的Demo和一个winFrom的Excel的导入,简单好用。
资源截图
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using WebApplication1;

namespace excel
{
    /// 
    /// export 的摘要说明
    /// 

    public class export : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = “text/plain“;
            HttpFileCollection httpFileCollection = context.Request.Files;
            if (httpFileCollection.Count == 0)
            {
                context.Response.Write(“请选择你要上传的excel文件“);
                context.Response.End();
            }
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings[“ConnectionString“]?.ConnectionString;
            if (string.IsNullOrWhiteSpace(connStr))
            {
                context.Response.Write(“数据库链接字符串没有配置“);
                context.Response.End();
            }

            foreach (string name in httpFileCollection)
            {
                string path =context.Server.MapPath(“~“) + @“upload“ + Guid.NewGuid() + httpFileCollection[name].FileName;
                httpFileCollection[name].SaveAs(path);
                var helper = new ExcelHelper(path);
                DataTable dt = helper.ExcelToDataTable(“Sheet1“ true);
                if (dt == null)
                {
                    context.Response.Write(“上传的excel文件格式不对“);
                    context.Response.End();
                }
                //对手机号去重
                DataTable newtable = dt.AsDataView().ToTable(true);
                using (SqlConnection conn = new SqlConnection(connStr))
                {

                    conn.Open();
                    foreach (DataRow row in newtable.Rows)
                    {
                        string commandStr = “INSERT INTO t_stu_info(namephoneremark) VALUES(@name@phone@remark)“;
                        SqlCommand sqlCommand = new SqlCommand();
                        sqlCommand.Connection = conn;
                        sqlCommand.CommandText = commandStr;
                        sqlCommand.CommandType = CommandType.Text;
                        SqlParameter[] sqlParameter ={
                                    new SqlParameter(“@name“row[“name “]==null?““:row[“name “].ToString())
                                    new SqlParameter(“@phone“row[“phone“]==null?““:row[“phone“].ToString())
                                    new SqlParameter(“@remark“row[“remark“]==null?““:row[“remark“].ToString())
                                    };
                        sqlCommand.Parameters.AddRange(sqlParameter);
                        sqlCommand.ExecuteNonQuery();
                    }

                    //using (SqlBulkCopy sqlBulkCopy = new System.Data.SqlClient.SqlBulkCopy(connStr SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.UseInternalTran

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-03-06 09:09  excel
     目录           0  2019-03-06 09:07  excel.vs
     目录           0  2019-03-06 09:07  excel.vsconfig
     文件       86088  2019-03-06 09:08  excel.vsconfigapplicationhost.config
     目录           0  2019-03-06 09:07  excel.vsexcel
     目录           0  2019-03-06 09:08  excel.vsexcelv15
     文件       75264  2019-03-06 10:46  excel.vsexcelv15.suo
     目录           0  2019-03-06 09:08  excel.vsexcelv15Server
     目录           0  2019-03-06 09:08  excel.vsexcelv15Serversqlite3
     文件           0  2019-03-06 09:08  excel.vsexcelv15Serversqlite3db.lock
     文件        4096  2019-03-06 09:08  excel.vsexcelv15Serversqlite3storage.ide
     文件       32768  2019-03-06 10:29  excel.vsexcelv15Serversqlite3storage.ide-shm
     文件     1693352  2019-03-06 10:29  excel.vsexcelv15Serversqlite3storage.ide-wal
     目录           0  2019-03-06 09:07  excel.vsexcelv15sqlite3
     文件     1060864  2019-03-06 09:03  excel.vsexcelv15sqlite3storage.ide
     目录           0  2019-03-06 10:29  excelexcel
     目录           0  2019-03-06 09:13  excelexcelFiles
     文件       25177  2019-03-06 09:13  excelexcelFiles2318bde6-f11d-48b3-bad4-74cdd363241d.xlsx
     文件       78861  2019-03-06 09:13  excelexcelFiles学生.xml
     文件          89  2019-03-05 16:56  excelexcelGlobal.asax
     文件         335  2019-03-05 16:56  excelexcelGlobal.asax.cs
     目录           0  2019-03-06 09:07  excelexceljavascript
     目录           0  2019-03-06 09:07  excelexceljavascriptlayui-v2.4.5
     目录           0  2019-03-06 09:07  excelexceljavascriptlayui-v2.4.5layui
     目录           0  2019-03-06 09:07  excelexceljavascriptlayui-v2.4.5layuicss
     文件       69524  2018-11-02 10:36  excelexceljavascriptlayui-v2.4.5layuicsslayui.css
     文件        9885  2018-11-02 10:36  excelexceljavascriptlayui-v2.4.5layuicsslayui.mobile.css
     目录           0  2019-03-06 09:07  excelexceljavascriptlayui-v2.4.5layuicssmodules
     文件        1063  2018-11-02 10:36  excelexceljavascriptlayui-v2.4.5layuicssmodulescode.css
     目录           0  2019-03-06 09:07  excelexceljavascriptlayui-v2.4.5layuicssmoduleslaydate
     目录           0  2019-03-06 09:07  excelexceljavascriptlayui-v2.4.5layuicssmoduleslaydatedefault
............此处省略382个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)