zoukankan      html  css  js  c++  java
  • C# 读取Excel数据到DataTabel

    View Code
    /// <summary>
    /// 根据excel的文件的路径提取其中表的数据
    /// </summary>
    /// <param name="Path">Excel文件的路径</param>
    private void GetDataFromExcelWithAppointSheetName(string Path)
    {
    //连接串
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
    OleDbConnection conn
    = new OleDbConnection(strConn);

    conn.Open();

    //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等
    DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });

    //包含excel中表名的字符串数组
    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();

    //从指定的表明查询数据,可先把所有表明列出来供用户选择
    string strExcel = "select * from [" + strTableNames[0] + "]";
    myCommand
    = new OleDbDataAdapter(strExcel, strConn);
    dt
    = new DataTable();
    myCommand.Fill(dt);

    dataGridView1.DataSource
    = dt; //绑定到界面
  • 相关阅读:
    282. Expression Add Operators
    281. Zigzag Iterator
    280. Wiggle Sort
    How Not to Crash #2: Mutation Exceptions 可变异常
    Moving Swiftly
    How to Use updateConstraints
    Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中
    Where-To-Put-The-Auto-Layout-Code
    iOS five years[转]
    ResponderChain note
  • 原文地址:https://www.cnblogs.com/Mr0909/p/2048022.html
Copyright © 2011-2022 走看看