• 转:C# 导入EXCEL到sql server数据库(Web开发)


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using My_Class;
    using System.Data.SqlClient;
    using System.IO;
    public partial class daoru : System.Web.UI.Page
    {
        
        
         
    protected static string lj = "";
        
         
    protected static string getErrMsg = "";
         
    protected void Page_Load(object sender, EventArgs e)
         {
         }
         
    protected void Button1_Click(object sender, EventArgs e)
         {
             
    string filePath = "";
             
    if (FJ.PostedFile.FileName == "")
             {
                 Response.Write(
    "<script language=javascript>alert('请选择要上传的文件!');</script>");
                 
    return;
             }
             
    else
             {
                 filePath 
    = FJ.PostedFile.FileName;//取得文件路径
                 string sql_excel = "select 序号,单位名称,企业人员分布,姓名,性别,出生日期,身份证号,工作时间,工龄,个人身份,薪酬,学历,技术等级名称,级别,工种,持证上岗情况, 技术岗位人员所占比例,取得资格证书时间,颁发证书单位 from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=" + filePath + "',ygxxb$)";
                 DataSet ds 
    = new DataSet();
                 ds 
    = conn.executeQuery(sql_excel);
                 DataTable dt 
    = new DataTable();
                 dt 
    = ds.Tables[0];
                 GridView1.DataSource 
    = dt;
                 GridView1.DataBind();
                 
    if (ds.Tables[0].Rows.Count != 0)
                 {
                     
    string sql = "";
                     
    //////////////链接数据库////////////////////////////
                     SqlConnection myConnection = new SqlConnection();
                     
    string strConn = System.Configuration.ConfigurationSettings.AppSettings["sqlserver"];
                     myConnection.ConnectionString 
    = strConn;
                     SqlCommand da 
    = new SqlCommand();
                     
    try
                     {
                         
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                         {
                             sql 
    = "insert into ygxx(qy_id,bm_id,name,sex,birthday,card,work_time,gongling,grsf,xc,culture,dj_name,zd_id,gz_id,sfcz,szbl,qdzg_time,bfdw,xh) values ('" + ds.Tables[0].Rows[i]["单位名称"].ToString() + "','" + ds.Tables[0].Rows[i]["企业人员分布"].ToString() + "','" + ds.Tables[0].Rows[i]["姓名"].ToString() + "','" + ds.Tables[0].Rows[i]["性别"].ToString() + "','" + ds.Tables[0].Rows[i]["出生日期"].ToString() + "','" + ds.Tables[0].Rows[i]["身份证号"].ToString() + "','" + ds.Tables[0].Rows[i]["工作时间"].ToString() + "','" + ds.Tables[0].Rows[i]["工龄"].ToString() + "','" + ds.Tables[0].Rows[i]["个人身份"].ToString() + "','" + ds.Tables[0].Rows[i]["薪酬"].ToString() + "','" + ds.Tables[0].Rows[i]["学历"].ToString() + "','" + ds.Tables[0].Rows[i]["技术等级名称"].ToString() + "','" + ds.Tables[0].Rows[i]["级别"].ToString() + "','" + ds.Tables[0].Rows[i]["工种"].ToString() + "','" + ds.Tables[0].Rows[i]["持证上岗情况"].ToString() + "','" + ds.Tables[0].Rows[i]["技术岗位人员所占比例"].ToString() + "','" + ds.Tables[0].Rows[i]["取得资格证书时间"].ToString() + "','" + ds.Tables[0].Rows[i]["颁发证书单位"].ToString() + "','" + ds.Tables[0].Rows[i]["序号"].ToString() + "')";
                             da 
    = new SqlCommand(sql, myConnection);
                             
    if (da.Connection.State.ToString() == "Closed")
                             {
                                 da.Connection.Open();
                             }
                             da.ExecuteNonQuery();
                         }
                     }
                     
    catch (Exception ex)
                     {
                         getErrMsg 
    = ex.Message.ToString();
                         Response.Write(ex.Message.ToString());
                     }
                     
    finally
                     {
                         da.Connection.Close();
                         da 
    = null;
                     }
                     
    if (getErrMsg == "" || getErrMsg == null)
                     {
                         Page.ClientScript.RegisterStartupScript(Page.GetType(), 
    """<script language='Javascript'>alert('导入成功!')</script>");
                         
    return;
                     }
                     
    else
                     {
                         Page.ClientScript.RegisterStartupScript(Page.GetType(), 
    """<script language='Javascript'>alert('导入失败!')</script>");
                         
    return;
                     }
                 }
             }
         }
         
    protected void LinkButton1_Click(object sender, EventArgs e)
         {
             Response.Redirect(
    "http://" + Request.ServerVariables["HTTP_HOST"+ Request.ApplicationPath.ToString() + "/module" + "/FileFlow.XLS");//取得文件路径
         }



    下面是将数据读入到dataset

    public static DataSet CreateDataSource(string filePath)
            {
                
    try
                {
                    
    string strConn;

                    strConn 
    = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    
    "Data Source=" + filePath + ";" +
                    
    "Extended Properties=\"Excel 8.0;IMEX=1;\"";//连接

                    OleDbConnection conn 
    = new OleDbConnection(strConn);

                    conn.Open();

                    System.Data.DataTable schemaTable 
    = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);

                    
    string tableName = schemaTable.Rows[0][2].ToString().Trim();//得到excel表名

                    OleDbDataAdapter myCommand 
    = new OleDbDataAdapter("SELECT * FROM [" + tableName + "]", strConn);//查找excel数据

                    DataSet myDataSet 
    = new DataSet();//定义数据

                    myCommand.Fill(myDataSet);

                    conn.Close();

                    
    return myDataSet;                
                }
                
    catch
                {
                    
    throw;
                }
            }
  • 相关阅读:
    Android AndroidManifest 清单文件以及权限详解!【转】
    java的几个版本以及jre,jdk等概念——【转载】JDK、Java SE、Java EE、Java ME我该选
    eclipse中的两种Jre 及 Jre与Jdk的区别
    LAMP_yum安装
    CentOS7修改主机名(hostname)
    Linux下U盘挂载
    mount命令
    vmware虚拟机无法连接网络
    shell中awk printf的用法
    scp命令
  • 原文地址:https://www.cnblogs.com/wantingqiang/p/1307352.html
Copyright © 2020-2023  润新知