日期:2014-05-17  浏览次数:20455 次

Execl导入功能请教。急急急急急急!!
将Execl表中的数据导入到DataTable或List中在页面显示,点击确认后在保存到数据库

------解决方案--------------------
C# code

    private DataSet xsldata(string filepath, int type)
    {
        //HDR标识是否有表头,IMEX标识格式是文本
        string strCon = "";
        if (type == 1)   //Execl2007、2010版本
            strCon = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
        else    //Execl2003版本
            strCon = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
        System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
        string strCom = "SELECT * FROM [Sheet1$]";
        Conn.Open();
        System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
        DataSet ds = new DataSet();
        myCommand.Fill(ds, "ds");
        Conn.Close();
        return ds;
    }