怎么用C#读取excel第一个工作表名?

解决方案 »

  1.   

    通过OleDb连接,把excel文件作为数据源来读取(这里是fill进dataset,也可以返回OleDbDataReader来逐行读,数据较快)
      

  2.   

    获得excel对象后  sheet[0]应该是第一个表名称了
      

  3.   

    查询EXCEL:
    http://topic.csdn.net/u/20090424/10/4012f164-f557-4b06-83b0-16cde29d57f3.html
      

  4.   

    http://www.programfan.com/club/showtxt.asp?id=138851
    查询工作表名~
      

  5.   

            /// <summary>
            /// 读取Excel中表的名称
            /// </summary>
            /// <param name="excelFile"></param>
            /// <returns></returns>
            public String[] GetExcelSheetNames(string excelFile)
            {            OleDbConnection objConn = null;
                System.Data.DataTable dt = null;
                try
                {
                    //此连接可以操作.xls与.xlsx文件  
                    string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
                    objConn = new OleDbConnection(strConn);
                    objConn.Open();
                    dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return null;
                    }
                    String[] excelSheets = new String[dt.Rows.Count];
                    int i = 0;
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[i] = row["TABLE_NAME"].ToString();
                        i++;
                    }
                    return excelSheets;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (objConn != null)
                    {
                        objConn.Close();
                        objConn.Dispose();
                    }
                    if (dt != null)
                    {
                        dt.Dispose();
                    }
                }
            }这个方法可以读取Excel中的工作表的名称,但得出的名称被排序了,我想要的是第一个工作表的名称。
      

  6.   

    Microsoft.Office.Interop.Excel.Application obj = default(Microsoft.Office.Interop.Excel.Application);
    Microsoft.Office.Interop.Excel.Workbook objWB = default(Microsoft.Office.Interop.Excel.Workbook);
    string FirstSheetName = null;
    obj = (Microsoft.Office.Interop.Excel.Application)Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application", string.Empty);
    objWB = obj.Workbooks.Open(filepath, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,  
    Type.Missing,Type.Missing, Type.Missing, Type.Missing,  
    Type.Missing, Type.Missing,Type.Missing, Type.Missing);
    FirstSheetName = ((Microsoft.Office.Interop.Excel.Worksheet)objWB.Worksheets[1]).Name;
    objWB.Close(Type.Missing, Type.Missing, Type.Missing);
    objWB = null;
    obj.Quit();
    obj = null;
      

  7.   

            public static string GetExcelFirstTableName0(string ExcelFile)
            {
                string tableName = null;
                if (System.IO.File.Exists(ExcelFile))
                {
                    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +
                      "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + ExcelFile))
                    {
                        conn.Open();
                        DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        tableName = dt.Rows[0][2].ToString().Trim();
                    }
                }
                return tableName;
            }
      

  8.   

      Excel.Application excel = new Excel.ApplicationClass();
                    Excel._Workbook xBk = excel.Workbooks.Add("\\路径\\EXCEL名称.xls");
                    Excel._Worksheet xSt = (Excel._Worksheet)xBk.Worksheets[1];
                  第一个工作表名=  xSt.Name
      

  9.   

    +1注意这个
    string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\book1.xls;Extended Properties=Excel 8.0" ;
    不同版本的链接字符串不一样的,,,