Excel 데이터를 Tables/데이터베이스/엔티티로 가져오기

1461 단어
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]);
        }

    }
}

좋은 웹페이지 즐겨찾기