2013-08-03 116 views
0

我想在messagebox中显示excel工作表名称,但好像工作表不工作,有人请帮助我。使用C#获取Excel工作表工作表名称列表

我厌倦了添加以下内容,但它没有显示任何内容。

string test = excelsheet.tostring(); 
messagebox.show(test); 

我会非常感谢您的回复。

在此先感谢

/* 
    Microsoft SQL Server Integration Services Script Task 
    Write scripts using Microsoft Visual C# 2008. 
    The ScriptMain is the entry point class of the script. 
*/ 

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.Data.Odbc; 
using System.Data.OleDb; 
using System.IO; 

namespace ST_2c44bffc2aef4e1295daeafee32354d2.csproj 
{ 
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] 
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
    { 

     #region VSTA generated code 
     enum ScriptResults 
     { 
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
     }; 
     #endregion 

     /* 
     The execution engine calls this method when the task executes. 
     To access the object model, use the Dts property. Connections, variables, events, 
     and logging features are available as members of the Dts property as shown in the following examples. 

     To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value; 
     To post a log entry, call Dts.Log("This is my log text", 999, null); 
     To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true); 

     To use the connections collection use something like the following: 
     ConnectionManager cm = Dts.Connections.Add("OLEDB"); 
     cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"; 

     Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. 

     To open Help, press F1. 
    */ 

     public void Main() 
     { 
      GetExcelSheetNames_2007(@"\\AUSSMBSSOBI\Files\Public\Reports\APOS_Dashboard.xlsm"); 
      Dts.TaskResult = (int)ScriptResults.Success; 
     } 

     public string[] GetExcelSheetNames_2007(string excelFile) 
     { 
      { 

       OleDbConnection objConn = null; 

       System.Data.DataTable dt = null; 



       try 
       { 

        FileInfo fileInfo = new FileInfo(excelFile); 

        String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "D:\\ETL\\Sample.xlsx" + ";Extended Properties=\"Excel 12.0 XML;HDR=YES\";"; 

        objConn = new OleDbConnection(connString); 

        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++; 

        } 



        for (int j = 0; j < excelSheets.Length; j++) 
        { 

        } 

        return excelSheets; 



       } 

       catch (Exception ex) 
       { 

        MessageBox.Show(ex.Message); 

        return null; 

       } 

       finally 
       { 

        if (objConn != null) 
        { 

         objConn.Close(); 

         objConn.Dispose(); 

        } 

        if (dt != null) 
        { 

         dt.Dispose(); 

        } 
        Dts.TaskResult = (int)ScriptResults.Success; 

       } 

      } 

     } 
    } 
} 
+0

任何错误消息? –

+0

没有错误,但我知道sample.xlsx有表名称,但我没有看到消息框被填充表名称,即使我添加字符串测试= excelsheet.tostring();的MessageBox.show(测试); – user1570210

+0

添加我正在使用SSIS脚本任务组件 – user1570210

回答

0

你excelSheets varaible是一个字符串数组,请尝试以下操作:

foreach(String str in excelSheets) 
{ 
    messagebox.show(str); 
} 
+0

嗨阿德尔,我试过把这添加到我的代码,但它显示每个表名3次迭代。 (int j = 0; j user1570210

+0

只需添加我包含的代码,在代码中删除for循环,因为foreach已经循环遍历表单。 –

+0

你是伟大的人...感谢和萨拉姆兄弟 – user1570210

0

为什么您使用OLE?您可以使用VSTO并将您的excelsheet命名为如下示例:

var excelApp = new Microsoft.Office.Interop.Excel.Application(); 
excelApp.Visible = true; 
var excelWorkbook = excelApp.Workbooks.Add(); 
Worksheet excelSheet = excelWorkbook.Sheets.Add(); 
excelSheet.Name = "My sheetname"; 
+0

在我们的Prod服务器上,我们没有安装Excel。 – user1570210

+0

您不需要安装Excel。只有VSTO(Visual Studio Tools for Office)是必需的:[link] http://msdn.microsoft.com/en-us/office/hh133430.aspx [/ link] –

相关问题