使用Asp.net将Excel导入DataTable

时间:2016-04-12 01:47:21

标签: c# sql asp.net excel import

当我使用excel表导入数据表时,同时需要导入SID,CREATE_DATETIME,MODIFY_DATETIME,CREATOR_SID(actorSId)......

但是在导入之后我只是获得了excel数据。那么如何同时导入呢?

result is after import excel

我的代码在这里;

 private void BindData(string strConn)
{
    OleDbConnection objConn = new OleDbConnection(strConn);
    objConn.Open();

    // Get the data table containg the schema guid.  
    DataTable dt = null;
    dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    objConn.Close();

    //ISystemId SID

    if (dt.Rows.Count > 0)
    {
         // Bind the sheets to the Grids  
        foreach (DataRow row in dt.Rows)
        {
            DataTable dt_sheet = null;

            dt_sheet = getSheetData(strConn, row["TABLE_NAME"].ToString());

        }
    }
}

private DataTable getSheetData(string strConn, string sheet)
{
    string sid = Convert.ToString(new SystemId());
    string Ctime = Convert.ToDateTime(DateTime.Now).ToString("yyyyMMddhhmmss");
    string Mtime = Convert.ToDateTime(DateTime.Now).ToString("yyyyMMddhhmmss");
    string actorSId = Convert.ToString(WebPage.CurrentSysUserSId);
    string query = "select * from [" + sheet + "]";
    OleDbConnection objConn;
    OleDbDataAdapter oleDA;
    DataTable dt = new DataTable();
    dt.Columns.Add("SID", typeof(string));
    dt.Columns.Add("CREATE_DATETIME", typeof(string));
    dt.Columns.Add("MODIFY_DATETIME", typeof(string));
    dt.Columns.Add("CREATOR_SID", typeof(string));
    dt.Columns.Add("MODIFIER_SID", typeof(string));
    dt.Columns.Add("MARK_DELETED", typeof(string));
    dt.Columns.Add("ENABLED", typeof(string));
    dt.Columns.Add("SORT", typeof(int));

    objConn = new OleDbConnection(strConn);
    objConn.Open();
    oleDA = new OleDbDataAdapter(query, objConn);
    oleDA.Fill(dt);

    objConn.Close();
    oleDA.Dispose();
    objConn.Dispose();
    return dt;
}

那么,谁能帮帮我?

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。

更改以下代码而不是明确工作。

 private DataTable getSheetData(string strConn, string sheet)
    {
        //string sid = Convert.ToString(new SystemId());
        string Ctime = Convert.ToDateTime(DateTime.Now).ToString("yyyyMMddhhmmss");
        string Mtime = Convert.ToDateTime(DateTime.Now).ToString("yyyyMMddhhmmss");
        string actorSId = Convert.ToString(WebPage.CurrentSysUserSId);
        string query = "select * from [" + sheet + "]";
        OleDbConnection objConn;
        OleDbDataAdapter oleDA;
        DataTable dt = new DataTable();
        dt.Columns.Add("SID", typeof(string));
        dt.Columns.Add("CREATE_DATETIME", typeof(string));
        dt.Columns.Add("MODIFY_DATETIME", typeof(string));
        dt.Columns.Add("CREATOR_SID", typeof(string));
        dt.Columns.Add("MODIFIER_SID", typeof(string));
        dt.Columns.Add("MARK_DELETED", typeof(string));
        dt.Columns.Add("ENABLED", typeof(string));
        dt.Columns.Add("SORT", typeof(int));

        objConn = new OleDbConnection(strConn);
        objConn.Open();
        DataSet dataSet = new DataSet();
        oleDA = new OleDbDataAdapter(query, objConn);
        oleDA.Fill(dt);
        foreach (DataRow dr in dt.Rows) // search whole table
        {
            for (int i = 0; i < dt.Rows.Count - 1; i++)
                if (dt.Rows.Count > i)
                {
                    string sid = Convert.ToString(new SystemId());
                    var row = dt.Rows[i];
                    row["SID"] = sid;
                    row["CREATE_DATETIME"] = Ctime;
                    row["MODIFY_DATETIME"] = Mtime;
                    row["CREATOR_SID"] = actorSId;
                    row["MODIFIER_SID"] = 0;
                    row["MARK_DELETED"] = "N";
                    row["ENABLED"] = "Y";
                    row["SORT"] = 1;
                }
        }
        objConn.Close();
        oleDA.Dispose();
        objConn.Dispose();
        return dt;
    }