将Excel数据导入Tables/数据库/实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;


using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;

namespace BLWS.SchoolManagementSys.WebApi.Controllers
{
    /// 
    /// 实现excel导入数据库
    /// 
    public class ExcelController : ApiController
    {
        /// 
        /// 上传Excel文件并将数据存入数据库
        /// 
        /// 文件的本地地址
        /// 
        [HttpGet]
        public IHttpActionResult UploadExcel(string strExcelFileName) {
           string strSheetName = "sheet1";
            //数据源
            string strConn= "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'";
            //Sql语句
            string strExcel = "select * from   [sheet1$]";
            //定义存放的数据表  
            DataSet ds = new DataSet();
            //连接数据源  
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();

            //适配到数据源  
            OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
            adapter.Fill(ds, strSheetName);

            conn.Close();
            return Ok(ds.Tables[strSheetName]);
        }

    }
}

你可能感兴趣的:(将Excel数据导入Tables/数据库/实体)