简体   繁体   English

使用SSIS脚本任务读取Excel单元格值

[英]Read Excel cell values with SSIS script task

I am trying to read an Excel file via a SSIS ScriptTask to check for certain cell values in that worksheet. 我试图通过SSIS ScriptTask读取Excel文件,以检查该工作表中的某些单元格值。

In the code example you can see that the strSQL is set to " H4:H4 " to only read one cell. 在代码示例中,您可以看到strSQL设置为“ H4:H4 ”以仅读取一个单元格。 This cell can only have a true or false value. 此单元格只能具有true或false值。 Since I also need to check for a certain string value in B1 I wanted to extend this version. 由于我还需要检查B1中的某个字符串值,我想扩展此版本。

  string filePath = "c:\\test\\testBoolean.XLSX";
  string tabName = "testSheet$";
  string strSQL = "Select * From [" + tabName + "H4:H4]";
  String strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                  + filePath + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\";";
  OleDbConnection cn = new OleDbConnection(strCn);
  int iCnt = 0;
  OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, cn);
  DataSet ds = new DataSet();
  objAdapter.Fill(ds, tabName);
  DataTable dt = ds.Tables[tabName];

  foreach (DataRow row in dt.Rows)
  {
      iCnt = iCnt + 1;
      // some processing....
  }

What I don't understand is why I get a boolean value with the above strSQL statement or with any statment containing the same row number like so: 我不明白的是为什么我用上面的strSQL语句或任何包含相同行号的statment得到一个布尔值,如下所示:

string strSQL = "Select * From [" + tabName + "F4:H4]";

Debug-Output: 调试输出:

row.ItemArray[2]    false   object {bool}

But when I set a different range like this one: 但是当我设置一个像这样的不同范围时:

string strSQL = "Select * From [" + tabName + "F1:H4]";

I loose the recognition of the bool value: 我放弃了对bool值的认识:

row.ItemArray[2]   "FALSE"  object {string}

I'd much rather like to use the bool value for other processing tasks. 我更愿意将bool值用于其他处理任务。

How can I fix this in addition to also reading the B2 value? 除了阅读B2值之外,我该如何解决这个问题呢?

Your connection string specified IMEX=1 , which tells the driver to treat intermixed data types as text. 您的连接字符串指定IMEX=1 ,它告诉驱动程序将混合数据类型视为文本。 (See the "Usage Considerations" section of the MSDN article Excel Connection Manager .) (请参阅MSDN文章Excel连接管理器的“使用注意事项”部分。)

Thus, when you specified a single row 因此,当您指定单行时

string strSQL = "Select * From [" + tabName + "F4:H4]";

there was only one possible data type for the third column, and the driver was able to correctly infer it. 第三列只有一种可能的数据类型,驱动程序能够正确推断它。 However, when you specified multiple rows 但是,当您指定多行时

string strSQL = "Select * From [" + tabName + "F1:H4]";

and any value in the range H1:H4 was not a bool , the driver translated all values in that column to string s. 并且H1:H4中的任何值都不是bool ,驱动程序将该列中的所有转换为string s。

Assuming that you do in fact have mixed data types in column H and only care about the values in two particular cells, the simplest solution is to query each cell individually. 假设事实上你确实在H列中有混合数据类型并且只关心两个特定单元格中的值,最简单的解决方案是单独查询每个单元格。 See Import a single Excel cell into SSIS for some ideas on how to do that. 请参阅将单个Excel单元格导入SSIS以获取有关如何执行此操作的一些想法。

I would clone most of the code to produce two separate SELECT statements to query the two different cells you are after with separate SQL statements. 我将克隆大部分代码以生成两个单独的SELECT语句,以使用单独的SQL语句查询您所使用的两个不同单元格。

Actually I would probably go further and shred the whole script into SSIS components eg Execute SQL Tasks or Data Flow Tasks. 实际上我可能会更进一步,将整个脚本分解为SSIS组件,例如执行SQL任务或数据流任务。

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

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