zoukankan      html  css  js  c++  java
  • 在asp.net webfrom 中上传execl (读取单个sheet的数据)

    protected void Button2_Click(object sender, EventArgs e)
    {
    if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
    {
    Response.Write("<script>alert('请您选择Excel文件')</script> ");
    return;//当无文件时,返回
    }
    string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
    if (IsXls != ".xls" && IsXls != ".xlsm" && IsXls != ".xlsx")
    {
    Response.Write("<script>alert('只可以选择Excel文件')</script>");
    return;//当选择的不是Excel文件时,返回
    }
    string filename = FileUpload1.FileName; //获取Execle文件名 DateTime日期函数
    string savePath = Server.MapPath(("~\Files\") + filename);//Server.MapPath 获得虚拟服务器相对路径
    FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
    DataSet ds = ExcelSqlConnection(savePath, filename); //调用自定义方法
    if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
    {
    Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示
    }
    else
    {
    DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组
    for (int i = 0; i < dr.Length; i++)
    {
    //前面除了你需要在建立一个“~\Files\”的文件夹外,其他的都不用管了,你只需要通过下面的方式获取Excel的值,然后再将这些值用你的方式去插入到数据库里面
    string SKU = dr[i][0].ToString();
    string UPC = dr[i][1].ToString();
    string PRODUCT = dr[i][2].ToString();
    string KIND = dr[i][3].ToString();
    string PRODUCTDATE = dr[i][4].ToString();
    Decimal EMAIL1 = Decimal.Parse(dr[i][5].ToString());
    Decimal EMAIL2 = Decimal.Parse(dr[i][6].ToString());
    Decimal EMAIL3 = Decimal.Parse(dr[i][7].ToString());
    int PIECE = int.Parse(dr[i][8].ToString());
    float WEIGHT = float.Parse(dr[i][9].ToString());
    string ckdm = dr[i][10].ToString();
    try
    {
    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(Connectionstring()))
    {
    conn.Open();
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.Connection = conn;
    string sql = @"insert into TBifaceSKU (SKU,UPC,PRODUCT,KIND,PRODUCTDATE,EMAIL1,EMAIL2,EMAIL3,PIECE,WEIGHT,ckdm)
    VALUES('" + SKU + "','" + UPC + "','" + PRODUCT + "','" + KIND + "','" + PRODUCTDATE + "'," + EMAIL1 + "," + EMAIL2 + "," + EMAIL3 + "," + PIECE + "," + WEIGHT + ",'" + ckdm + "') ";
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
    }

    }
    catch (Exception ex)
    {
    Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
    }

    }
    Response.Write("<script>alert('Excle表导入成功!');</script>");
    }
    }

    #region 连接Excel 读取Excel数据 并返回DataSet数据集合
    /// <summary>
    /// 连接Excel 读取Excel数据 并返回DataSet数据集合
    /// </summary>
    /// <param name="filepath">Excel服务器路径</param>
    /// <param name="tableName">Excel表名称</param>
    /// <returns></returns>
    public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)
    {
    string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
    OleDbConnection ExcelConn = new OleDbConnection(strCon);
    try
    {
    string strCom = string.Format("SELECT * FROM [Sheet1$]");
    ExcelConn.Open();
    OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
    DataSet ds = new DataSet();
    myCommand.Fill(ds, "[" + tableName + "$]");
    ExcelConn.Close();
    return ds;
    }
    catch
    {
    ExcelConn.Close();
    return null;
    }
    }
    #endregion

  • 相关阅读:
    ORALE 误删表 Flashback 恢复表
    C# SAP Connector .NET Framework 4.5 版本下载
    C# VS 调试报错:未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项
    【转载】亚信防毒墙网络版卸载
    CSS 相邻兄弟选择器
    如何衡量一个人的 JavaScript 水平
    this软绑定
    RequireJS
    模块机制
    Javascript 数组循环遍历之forEach
  • 原文地址:https://www.cnblogs.com/hbh123/p/5130881.html
Copyright © 2011-2022 走看看