sql-server - 要求使用 excel 报告中的列数识别报告

标签 sql-server excel ssis etl

我有两个 excel 文件,我想将这些文件导入 SQL 临时表。

第一个excel文件:

 T1      T2      T3     T4  Total
 1,472   1,364   1,422  –   4,258 
-152.6  -152.6  -152.6  –   
 1,958   1,939   1,942  –   
-122.6  -123.7  -122.2  – 

第二个excel文件:
 T1       T2     T3     T4  T5       Total
 1,472   1,364   1,422  –   12.2     4,258 
-152.6  -152.6  -152.6  –   1000.12
 1,958   1,939   1,942  –   50.23
-122.6  -123.7  -122.2  –   185.25

SSIS中是否有任何方法可以根据列数识别文件?我需要根据列号识别报告。

最佳答案

Microsoft.Office.Interop.Excel 命名空间中的对象可以在 C# 脚本任务中使用,如下所示。此示例将文件名和列数输出到 SSIS 对象变量 (User::SSISObjectVariable") 中,该变量可用于在包中应用进一步的逻辑和处理,例如存储在数据库表中或其他。完整文件路径是对象变量中的第一列,列数是第二列。还要确保在脚本中添加对 Microsoft.CSharp 命名空间的引用。对象变量需要包含在 ReadWriteVariables 中。脚本任务的字段,如果源文件夹存储在变量中(如下所示),则将此变量添加到 ReadOnlyVariables field 。

using Microsoft.Office.Interop.Excel;
using System.Data;
using System.IO;
using System.Collections.Generic;

    List<string> excelFileList = new List<string>();
//get source directory
string filePath = Dts.Variables["User::FilePathVariable"].Value.ToString();
DirectoryInfo di = new DirectoryInfo(filePath);

System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("FilePath", typeof(System.String));
dt.Columns.Add("ColumnCount", typeof(System.Int32));

foreach (FileInfo fi in di.EnumerateFiles())
{
    //optional- check file extension and prefix
    if (fi.Extension == ".xls" && fi.Name.StartsWith("Prefix"))
    {
        //get full file path
        excelFileList.Add(fi.FullName);
    }
}

foreach (string excelFile in excelFileList)
{

 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); ;
 Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(excelFile);
 Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];

 int columnCount;

//get number of columns
 columnCount = xlWorksheet.Cells.Find("*", System.Reflection.Missing.Value,
  System.Reflection.Missing.Value, System.Reflection.Missing.Value,
  Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns, Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
  false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Column;

//build data row to hold file path and column count
 DataRow dr = dt.NewRow();
 dr["FilePath"] = excelFile;
 dr["ColumnCount"] = columnCount;

 dt.Rows.Add(dr);

 xlApp.Workbooks.Close();
 xlApp.Quit();

 xlWorkbook = null;
 xlApp = null;
}

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

//populate object variable
Dts.Variables["User::SSISObjectVariable"].Value = dt;

关于sql-server - 要求使用 excel 报告中的列数识别报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55114800/

相关文章:

sql - 将多行的值汇总到一个新列中

sql - 何时使用 GROUPING SETS、CUBE 和 ROLLUP

json - Excel 到 JSON(使用 VBA)土耳其语字符问题

oracle - 使用 Toad 将 Excel 电子表格导入 Oracle

sql-server-2008 - 使用 2 种不同的配置运行 SSIS 包

sql - 存储过程的 WHERE 子句中的条件

vba - 无法访问 Excel VBA 代码

ssis - DFT 中的脚本任务未执行

sql-server - 如何替换ssis包中的unicode字符

c# - 如何使用 C# 将文本框中的十进制值插入数据库