程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#讀Csv文件、Execl並導入SqlServer數據庫

C#讀Csv文件、Execl並導入SqlServer數據庫

編輯:C#入門知識

ReadExcelFiles

//讀文件源代碼;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace CC
{
 public   class ReadExcel
    {
        private string FileName;
        private string FilsPath;
        /// <summary>
        /// 構造函數文件所在路徑
        /// </summary>
        /// <param name="Path">文件路徑(Execl文件為完整的路徑)</param>
        public ReadExcel(string Path)//構造
        {
            FileName = Path;
            //FilsPath = Files;
        }
        /// <summary>
        /// 構造函數:文件所在路徑
        /// </summary>
        /// <param name="Path">文件路徑(為Csv文件所在的文件夾路徑)</param>
        /// <param name="Files">讀Csv文件時的文件名</param>
        public ReadExcel(string Path, string Files)//構造
        {
            FileName = Path;
            FilsPath = Files;
        }
        OleDbConnection OleCon = new OleDbConnection();
        OleDbCommand OleCmd = new OleDbCommand();
        OleDbDataAdapter OleDa = new OleDbDataAdapter();

        #region 讀取Execl文件返回DataSet
        /// <summary>
        /// 讀取Execl文件返回DataSet
        /// </summary>
        /// <returns>返回結果集</returns>
        public DataSet Execl()
        {
            DataSet Myds = new DataSet();
            OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+FileName+";Extended Properties=Excel 8.0;";
            OleCon.Open();
            OleCmd.CommandText="select * from [sheet1$]";
            OleCmd.Connection = OleCon;
            OleDa.SelectCommand = OleCmd;
            try
            {
                OleDa.Fill(Myds, "Execl");
                return Myds;
            }
            catch
            {
                return Myds;
            }
            finally
            {
                OleCon.Close();
                OleCmd.Dispose();
                OleDa.Dispose();
                OleCon.Dispose();
            }

           
        }
        #endregion

        #region 讀取Csv文件返回DataSet
        /// <summary>
        ///  讀取Csv文件返回DataSet
        /// </summary>
        /// <returns>Csv內容</returns>
        public DataSet Csv()
        {
            DataSet CsvData = new DataSet();
            OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=Text;FMT=Delimited;HDR=YES;";
            OleCon.Open();
            OleCmd.Connection = OleCon;
            OleCmd.CommandText = "select * From " + FilsPath ;
            OleDa.SelectCommand=OleCmd;
            try
            {
                OleDa.Fill(CsvData, "Csv");
                return CsvData;
            }
            catch
            {
                return CsvData;
            }
            finally
            {
                OleCon.Close();
                OleCmd.Dispose();
                OleDa.Dispose();
          &

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved