excel导入数据库

cs页面



using System;

using System.Collections.Generic;

using System.Data;

using System.Data.OleDb;

using System.IO;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {



    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (FU.PostedFile == null || FU.PostedFile.FileName == "")

        {

            Response.Write("<script>alert('请选择上传的数据文件!')</script>");

            return;

        }

        string names = Path.GetExtension(FU.PostedFile.FileName).ToLower();

        if (names.Replace(".", "") != "xlsx")

        {

            Response.Write("请上传execl类型文件!");

            return;

        }

        string namess = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + names;

        FU.PostedFile.SaveAs(Server.MapPath("picture/"+namess));



        string paths = Server.MapPath(namess);



        paths = paths.Replace("\\", "/");

        DataTable dt = ExcelToDS(paths, "LAWUSER");

        bool successful = true;

        string errormsg = "";

        foreach (DataRow row in dt.Rows)

        {

            if (row["姓名"].ToString().Trim().Equals(""))

            {

                continue;

            }

            string id=row["编号"].ToString();

            string name = row["姓名"].ToString();

            string tel = row["联系电话"].ToString();

            string sql = "insert into AA values('"+id+"','"+name+"','"+tel+"')";

            try

            {

                int i = SqlHelper1.ExecuteNonQuery(sql);

                if (i > 0)

                {

                    Response.Write("<script>alert('插入成功!')</script>");

                }

            }

            catch (Exception ex)

            { 

             

            }

        }

    }



    #region 把excel表中的数据读入到dateset表中

    private DataTable ExcelToDS(string Path, string ExtandName)

    {

        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;'";

        OleDbConnection conn = new OleDbConnection(strConn); conn.Open();

        string strExcel = "";

        OleDbDataAdapter myCommand = null; DataSet ds = null;

        strExcel = "select * from [" + ExtandName + "$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet();

        myCommand.Fill(ds, "table1"); return ds.Tables[0];

    }

    #endregion

}
aspx页面

、<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>



<!DOCTYPE html>



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:FileUpload ID="FU" runat="server" />

        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    </div>

    </form>

</body>

</html>

 

你可能感兴趣的:(Excel)