【问题标题】:handling dynamic excel sheets处理动态excel表
【发布时间】:2014-05-04 22:14:34
【问题描述】:

我有一个案例,我需要导入一个 Excel 文件,在数据库中有两张表。我正在使用 SSIS 包。问题是,我可以通过设置表达式使 Excel 工作表动态化,但 Excel 工作簿中的工作表也在更改名称。我怎样才能使工作表名称更加动态。 我曾尝试在我的 DEV 代码中使用 Microsoft.Office.InterOp.excel,但 PROD 上没有安装 excel。有人可以为我解决这个问题。

提前致谢。

【问题讨论】:

    标签: c# excel dynamic ssis


    【解决方案1】:

    尝试添加类似于以下脚本的内容,可以在Code Spot - Dynamic Sheet Name in SSIS Excel Spreadsheet Imports 找到。它不需要在机器上安装 Excel。

    string excelFile = null;
    string connectionString = null;
    OleDbConnection excelConnection = null;
    DataTable tablesInFile = null;
    int tableCount = 0;
    DataRow tableInFile = null;
    string currentTable = null;
    int tableIndex = 0;
    string[] excelTables = null;
    
    excelFile = Dts.Variables["User::ExcelFile"].Value.ToString();
    
    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties=Excel 8.0";
    
    excelConnection = new OleDbConnection(connectionString);
    
    excelConnection.Open();
    tablesInFile = excelConnection.GetSchema("Tables");
    
    tableCount = tablesInFile.Rows.Count;
    excelTables = new string[tableCount];
    
    foreach (DataRow tableInFile_loopVariable in tablesInFile.Rows)
    {
    tableInFile = tableInFile_loopVariable;
    currentTable = tableInFile["TABLE_NAME"].ToString();
    excelTables[tableIndex] = currentTable;
    tableIndex += 1;
    }
    }
    
    Dts.Variables["User::SheetName"].Value = excelTables[0];
    
    Dts.TaskResult = (int)ScriptResults.Success;
    

    【讨论】:

      【解决方案2】:

      我正在使用 SQL 脚本将 Excel 文件按原样加载到临时表中,然后使用 SSIS / T-SQL 对其进行处理,它相对快速且可靠。可自行下载驱动,此方法无需office安装。

      /*Drop table if exists*/
      IF OBJECT_ID(’table_1', 'U') IS NOT NULL
      EXEC ('DROP TABLE table_1')
      
      /*Load using access driver, can probably work with excel too.*/
      select *
      into [table_1]
      from openrowset('MSDASQL'
                     ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)}'
                     ,'select * from 'D:\folder\file.csv' ')
      

      【讨论】:

        猜你喜欢
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-11
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多