zoukankan      html  css  js  c++  java
  • C#中获取Excel文件的第一个表名(转)

         excel文件中第一个表名的缺省值是sheet1$, 但有时也会被改变为其他名字. 如果需要在C#中使用OleDb读写Excel文件, 就需要知道这个名字是什么. 以下代码就是实现这个功能的:

    using System;
    using System.IO;
    using System.Data;
    using System.Data.OleDb;

    namespace Skyiv.Ben.Util
    {
      sealed class Pub
      {
            public static string GetExcelFirstTableName(string excelFileName)
            {
                string tableName = null;
                if (File.Exists(excelFileName))
                {
                  using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet."+
                    "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFileName))
                  {
                      conn.Open();
                      DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                      tableName = dt.Rows[0][2].ToString().Trim();
                  }
              }
                return tableName;
            }
        }
    }

    以下是测试程序:

    namespace Skyiv.Ben.Test
    {
      using Skyiv.Ben.Util;
     
      class MainTest
      {
        static void Main(string [] args)
        {
          foreach (string s in args)
            Console.WriteLine("[{0}] => [{1}]", s, Pub.GetExcelFirstTableName(s));
        }
      }
    }

  • 相关阅读:
    2003系统IIS上传文件不能超过200K的解决方案
    ASP从编辑框中获取图片路径
    ASP 编码转换大全 UTF8、GB2312、二进制、十进制代码、十六进制
    解决IE6、IE7、IE8样式不兼容问题
    py2exe setup.py
    Python to 2bit
    python访问ACCESS
    Pamie Web自动化
    Perl 笔记
    常用工具全盗版 汗颜了
  • 原文地址:https://www.cnblogs.com/yangxiaohu1/p/1342319.html
Copyright © 2011-2022 走看看