首页 > 代码库 > asp.net mvc Dateset读取Excel数据

asp.net mvc Dateset读取Excel数据

//处理Excel
//读取Excel
[NonAction]
        public static DataSet ExcelToDS(string Path)
        {
       //根据情况选择合适的连接字符,参考msdn
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
            //发布后开启此注释
            //string strConn = "Provider=Microsoft.ACE.OLEDB.12.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 [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");
            return ds;
        }

 

             //业务处理
       var data = http://www.mamicode.com/new DataSet(); //使用上述操作Excel的方法读取数据 var execl = ExcelToDS(Session["xlsPath"].ToString()); data= execl.Clone(); foreach (DataRow item in execl.Tables[0].Rows) { //检测是否末行 try { //表格中第一行存放编号,当无法转换为整型数时则表示数据异常并抛出 Convert.ToInt32(item[0]); data.Tables[0].Rows.Add(item.ItemArray); } catch (Exception) { break; } } //返回指定的view return View(finalInfo);

 

asp.net mvc Dateset读取Excel数据