简体   繁体   English

如何在工作表中的标题信息中加载SSIS中的Excel文件的多个工作表

[英]How to load multiple sheets of an Excel File in SSIS with header information in the sheets

I have a file with 2 sheets in it. 我有一个2张纸的文件。 It has the header information in it. 它具有标题信息。 I tried foreach loop container to load the data Error i got is: [SSIS.Pipeline] Error: "Excel Source" failed validation and returned validation status "VS_NEEDSNEWMETADATA". 我尝试过foreach循环容器加载数据我得到的错误是:[SSIS.Pipeline]错误:“ Excel Source”验证失败,并返回了验证状态“ VS_NEEDSNEWMETADATA”。

I tried removing the header row manually from the sheets and ran foreach loop container and it worked perfectly fine. 我尝试手动从工作表中删除标题行,然后运行foreach循环容器,它的工作原理非常好。

But in my requirement i will be getting the header row followed by blank row in each sheet. 但在我的要求下,我将在每张纸上获得标题行,后跟空白行。 How do i do in this case. 在这种情况下,我该怎么办。 I believe we need to use script task to to eliminate header and followed null row from the file and read the rest of the records. 我相信我们需要使用脚本任务来消除文件头和空行,然后读取其余记录。

My problem is i am bad at c# code logic. 我的问题是我不擅长C#代码逻辑。

Your help is much appreciated. 非常感谢您的帮助。 Thank you, swathi 谢谢你,swathi

The following Script Task will delete the top 2 rows from every worksheet in the file (you'll need to create the variable 'ExcelFilePath' in SSIS and pass that in to the task, along with 'System::TaskName'): 以下脚本任务将从文件中每个工作表中删除前2行(您需要在SSIS中创建变量“ ExcelFilePath”,并将其与“ System :: TaskName”一起传递给任务):

public void Main()
{
    MainTask();

    GC.Collect();
    GC.WaitForPendingFinalizers();
}

private void MainTask()
{
    xl.Application xlApp = null;
    xl.Workbook excelFile = null;
    string excelFilePath = Dts.Variables["User::ExcelFilePath"].Value.ToString();
    string thisTask = Dts.Variables["System::TaskName"].Value.ToString();

    try
    {
        xlApp = new xl.Application();

        excelFile = xlApp.Workbooks.Open(excelFilePath);

        xlApp.DisplayAlerts = false;

        foreach (xl.Worksheet ws in excelFile.Worksheets)
        {
            ws.Rows["1:2"].EntireRow.Delete();
        }

        xlApp.DisplayAlerts = true;

        excelFile.Save();
        excelFile.Close();

        xlApp.Quit();

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

    catch (Exception ex)
    {
        Dts.Events.FireError(0, thisTask, ex.Message, String.Empty, 0);

        if (excelFile != null) excelFile.Close(SaveChanges:false);
        if (xlApp != null) xlApp.Quit();
    }
}

You will need to add references to 'COM' > 'Microsoft Excel [version number] Object Library' (whichever version you have) and '.NET' > 'Microsoft.CSharp'. 您将需要添加对“ COM”>“ Microsoft Excel [版本号]对象库”(无论您使用的版本)和“ .NET”>“ Microsoft.CSharp”的引用。 You'll then need to declare using xl = Microsoft.Office.Interop.Excel; 然后,您需要using xl = Microsoft.Office.Interop.Excel;进行声明using xl = Microsoft.Office.Interop.Excel; in your 'Namespaces' region. 在“命名空间”区域中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM