• Excel读取


    代码
    private static void ToDataBase(DataSet ds, string dbTableName)
    {
    string connectionString = "server=.;database=...;uid=sa;pwd=;";
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
    string values = string.Empty;
    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
    {
    if (i != ds.Tables[0].Columns.Count - 1)
    {
    values
    += "'" + dr[i].ToString().Replace("'", "''") + "'" + ",";
    }
    else
    {
    values
    += "'" + dr[i].ToString().Replace("'", "''") + "'";
    }
    }
    string sql = "insert into [" + dbTableName + "] values(" + values + ")";
    SqlCommand cmd
    = new SqlCommand(sql, conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    }
    }
    }



    private static DataSet ImportExcel(string strFileName)
    {
    if (strFileName.Length < 0) return null;


      
    //HDR有两个值:yes和no.默认是yes,表示第一行是字段名.no表示第一行不是字段名.
      
    //IMEX=1:解决数字与字符混合时,识别不正常的情况.
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
    OleDbConnection conn
    = new OleDbConnection(strConn);
    conn.Open();
    DataTable dtSchema
    = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    conn.Close();

    IList
    <string> tblNames = new List<string>();
    foreach (DataRow dr in dtSchema.Rows)
    {
    tblNames.Add((
    string)dr["TABLE_NAME"]);
    }

    OleDbDataAdapter adapter
    = new OleDbDataAdapter("select * from [" + tblNames[0] + "]", strConn);

    DataSet ds
    = new DataSet();
    try
    {
    adapter.Fill(ds);
    }
    catch (Exception ex)
    {
    System.Console.WriteLine(ex.Message.ToString());
    System.Console.ReadLine();
    }

    return ds;
    }
  • 相关阅读:
    前端优化,注意的一些东西
    php遍历memcache的方法
    mysql增量备份
    CI框架安装
    sphinx:received zerosized searchd response
    SEO思考:逆水行舟 不进则退
    利用GNUstep在windows下编写objectc
    Centos5.3下安装memcached
    想卖网站?我来给你指条明路
    时髦的互联网公司都在用什么技术?
  • 原文地址:https://www.cnblogs.com/rxie/p/1761629.html
Copyright © 2020-2023  润新知