asp.net操作ExcelテーブルデータはSQL Serverデータベースに導入されます。


コードは全部貼り付けられています。主にExcelテーブルのデータはデータベースのデータタイプと一致します。
ここのExcel表のフィールドは以下の通りです。
名前、性別、クラス、学級番号、初期パスワード
SQL Server表tb_Usersのフィールドは。
RealName、Sex、InClass、Question、Answer





    


    <form id="form1" runat="server">
    <div>
        <fileupload id="FileUpload1" runat="server"/>
        <button id="Button1" runat="server" onclick="Button1_Click" text="Button"/>
    </div>
    </form>


</code></pre> 
  <br/> 
  <pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using USTC;
using System.Drawing;

public partial class TEST_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //      
        string filePath = this.FileUpload1.PostedFile.FileName;
        if (filePath != "")
        {
            if (filePath.Contains("xls"))//        
            {
                InputExcel(filePath);
            }
            else
            {
                Response.Write("            Excel  !  !");
            }
        }

        else
        {
            Response.Write("         ,     !  !");
        }
    }

    private void InputExcel(string pPath)
    {
         string conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + pPath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
        OleDbConnection oleCon = new OleDbConnection(conn);
        oleCon.Open();
        string Sql = "select * from [Sheet1$]";
        OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, oleCon);
        DataSet ds = new DataSet();
        mycommand.Fill(ds, "[Sheet1$]");
        oleCon.Close();
        int count = ds.Tables["[Sheet1$]"].Rows.Count;
        for (int i = 0; i < count; i++)
        {
            string tRealName, tSex, tInClass, tQuestion, tAnswer;
            tRealName = ds.Tables["[Sheet1$]"].Rows[i]["  "].ToString().Trim();
            tSex = ds.Tables["[Sheet1$]"].Rows[i]["  "].ToString().Trim();
            tInClass = ds.Tables["[Sheet1$]"].Rows[i]["  "].ToString().Trim();
            tQuestion = ds.Tables["[Sheet1$]"].Rows[i]["  "].ToString().Trim();
            tAnswer = ds.Tables["[Sheet1$]"].Rows[i]["    "].ToString().Trim();
            string excelsql = "insert into tb_Users(RealName, Sex, InClass,Question,Answer) values ('" + tRealName + "','" + tSex + "','" + tInClass + "','" + tQuestion + "','" + tAnswer + "')";
            try
            {
                //   SQL Server 
                DM dm = new DM();
                dm.execsql(excelsql);
                Response.Write("<script language="javascript">Alert('      !');window.location='Default.aspx'</script>");
            }
            catch(Exception)
            {
                Response.Write("<script language="javascript">Alert('      !');window.location='Default.aspx'</script>");
            }
        }
    }
}</code></pre> 
  <br/> 
  <br/> 
   
 </div> 
</div>
                            </div>
                        </div>