如何将三个不同的 Excel 文件加载到不同的表中

How to load three Different Excel files into different Tables

我有三个 Excel 文件,它们每周更新一次,名称包括更新后的 Date.xlsx。 我需要使用 SSIS 包将这三个 Excel 文件加载到我的三个表中。 我还需要使其自动化,因为我必须为此安排工作。

如何自动执行特定 Excel 的 selection 并加载到特定 Table。 例如

  1. workanalysis_21032015.xlsx
  2. analytics_21032015.xlsx
  3. googleprobes_21032015.xlsx

我需要这个 Excels 数据加载到三个不同的 Tables 因为这些 Excel 更新新名称为 (1.workanalysis_28032015.xlsx) 特别是文件夹每个星期。我需要 select 工作分析并将数据转储到 Table,其余两个相同。

如果您想知道如何将 Excel 文件自动导入(SSIS 包)到 SQL 服务器,这里有一个很好的分步指南和屏幕截图: https://www.simple-talk.com/sql/ssis/moving-data-from-excel-to-sql-server---10-steps-to-follow/

你必须做三遍。复制粘贴是你的朋友:)

关于考虑日期和文件夹结构的逻辑,我建议编写一个 C# 脚本任务。这是一个错误的评论示例(抱歉,commets 在挪威语中,我不关心翻译所以我删除了它们)脚本找到最新的 xml-文件的名称,该文件使用非常具体的命名约定包括数据时间,就像您在 ftp 服务器上的数据时间,然后将其写入名为 User:newFile 的 SSIS 变量。 xml 文件的名称为:Something_YYYYMMDD.xml,因此 fileIsNew 函数中的逻辑。

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text;
using System.Globalization;

namespace ST_59d80f6857bc4a6197af798be478f308.csproj
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
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
    public string[] GetDirectory()
    {
        StringBuilder result = new StringBuilder();

        System.Net.FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create("ftp://someftpserver.com");
        requestDir.Method = WebRequestMethods.Ftp.ListDirectory;

        requestDir.Credentials = new System.Net.NetworkCredential("User", "password");
        FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();
        StreamReader readerDir = new StreamReader(responseDir.GetResponseStream());

        string line = readerDir.ReadLine();
        while (line != null)
        {
            result.Append(line);
            result.Append("\n");
            line = readerDir.ReadLine();
        }

        result.Remove(result.ToString().LastIndexOf('\n'), 1);
        responseDir.Close();
        return result.ToString().Split('\n');
    }

    public bool fileIsNew(string file, string newestFile)
    {
        if (file.EndsWith(".xml", System.StringComparison.CurrentCultureIgnoreCase) && file.Length >= 11)
        {
            decimal test;
            if(decimal.TryParse(file.Substring(file.Length - 12, 8), out test))
            {
                if (Convert.ToInt32(file.Substring(file.Length - 12, 8)) > Convert.ToInt32(newestFile.Substring(newestFile.Length - 12, 8)))
                {
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }

    public void Main()
    {
        string newestFile = "19900101.xml"; 

        foreach (string file in GetDirectory())
        {
            if (fileIsNew(file, newestFile))
            {
                newestFile = file;
                // TEST!!!!
                // MessageBox.Show(newestFile);
            }

        }
        Dts.Variables["User::newFile"].Value = newestFile;

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

我想你可能需要做类似的事情:)

对每个循环使用一个。 正在为该实例处理的文件名 will/can 存储在一个变量中,仅采用文件名的字符串部分(您可以使用脚本任务)。将验证放在优先约束中(例如@var = "workanalysis"),然后将其传递到所需的特定目的地。