最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#讀取excel內(nèi)容內(nèi)容示例分享

 更新時(shí)間:2014年03月06日 12:06:00   作者:  
這篇文章主要介紹了c#讀取excel內(nèi)容內(nèi)容示例,要求Excel需是.xls格式,需要的朋友可以參考下

1、Excel 需是.xls 格式
2、添加引用Microsoft.Office.Interop.Excel.dll

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;

namespace ReadExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"D:\TransferPlant\111.xls";
            DataTable dt = ExcelToDataSet(fileName);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Console.WriteLine(dt.Rows[i][0].ToString());
                }
            }
        }

        static public DataTable ExcelToDataSet(string filename)
        {
            string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+filename+";Extended Properties=Excel 8.0";
            OleDbConnection conn = new OleDbConnection(strCon);
            conn.Open();
            //返回Excel的架構(gòu),包括各個(gè)sheet表的名稱(chēng),類(lèi)型,創(chuàng)建時(shí)間和修改時(shí)間等 
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串?dāng)?shù)組
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            OleDbDataAdapter myCommand = null;
            DataTable dt = new DataTable();
            //從指定的表明查詢數(shù)據(jù),可先把所有表明列出來(lái)供用戶選擇
            string strExcel = "select * from [" + strTableNames[0] + "]";
            myCommand = new OleDbDataAdapter(strExcel, strCon);
            myCommand.Fill(dt);

            return dt;
        }
    }
}

相關(guān)文章

最新評(píng)論

甘肃省| 娄底市| 诸城市| 龙胜| 兰溪市| 江口县| 唐海县| 安徽省| 楚雄市| 遵化市| 宁津县| 正阳县| 通城县| 云南省| 朝阳县| 海安县| 米易县| 建始县| 滁州市| 巴彦县| 七台河市| 遂昌县| 宁河县| 定日县| 汝州市| 北宁市| 永平县| 驻马店市| 朝阳市| 禄丰县| 泗水县| 广灵县| 万山特区| 高安市| 盈江县| 醴陵市| 库车县| 武城县| 湄潭县| 曲阳县| 南昌市|