Asp.Net Excelテーブルのインポート

4699 ワード

フロントページ:
<div align="center" style="background-color:Beige">
<table style="width: 576px; border-collapse: separate; text-align: center">
<tr><td>Excel  SQL   </td></tr>
<tr>
<td style="text-align: left">
<asp:FileUpload ID="FileUpload1" runat="server" Width="305px" />
<asp:Button ID="btnToLead" runat="server" OnClick="Button1_Click" Text="  SQL" /></td>
</tr>
<tr>
<td>
<asp:GridView ID="gvExcelEdu" runat="server" Height="64px" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="  " />
<asp:BoundField DataField="userName" HeaderText="   " />
<asp:BoundField DataField="passWord" HeaderText="  " />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>

バックグラウンドコード:リファレンスの追加に注意
using System.Data.OleDb;
using System.Data.SqlClient;
string strConn = "Data Source=192.168.0.8;DataBase=BR_BMS;Uid=DP;Pwd=dp"; //  SQL   

    //    
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection(strConn);
        cn.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select * from Emp_Admin", cn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "Admin");
        this.gvExcelEdu.DataSource = ds.Tables["Admin"];
        this.gvExcelEdu.DataKeyNames = new string[] { "id" };
        this.gvExcelEdu.DataBind();
    }
//   EXCEL       DATASET
public DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
odda.Fill(ds, table);
return ds;
 }
// Excel    
protected void Button1_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")
{
   Response.Write("<script>alert('     Excel  ')</script>");
    return;//      Excel   ,  
        }
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;              //  Execle     DateTime    
string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath            
FileUpload1.SaveAs(savePath);                        //SaveAs                
DataSet ds = ExecleDs(savePath, filename);           //       
DataRow[] dr = ds.Tables[0].Select();            //    DataRow  
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
    Response.Write("<script>alert('Excel    ,   !')</script>");   // Excel    ,       
}
else
{
     for (int i = 0; i < dr.Length; i++)
{
string id = dr[i]["  "].ToString();//          
string userName = dr[i]["   "].ToString();
string passWord = dr[i]["  "].ToString();//   excel  【     ,      】
string sqlcheck = "select count(*) from Emp_Admin where userName='" + userName + "'And passWord='" + passWord + "'";  //        
SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (count < 1)
{
    string insertstr = "insert into Emp_Admin (userName,passWord) values('" + userName + "','" + passWord + "')";
    SqlCommand cmd = new SqlCommand(insertstr, cn);
try
{
    cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex)       //    
{
      Response.Write("<script>alert('    :" + ex.Message + "')</script>");
}
}
else
 {
       Response.Write("<script>alert('    !    ');location='default.aspx'</script></script> ");
       continue;
}
}
       Response.Write("<script>alert('Excle     !');location='default.aspx'</script>");
}
        cn.Close();
}