具有日期值的 Excel 单元格无法通过 SSIS 正确导入

发布于 2024-12-09 15:48:11 字数 555 浏览 0 评论 0原文

我有一个由外部自动化流程生成的 Excel '97 电子表格,我想通过 SSIS 将其导入到 SQL [2008 R2] 表中。我正在导入的列在某些单元格中包含文本值,在其他单元格中包含日期值。为了连接到 Excel 数据,我有一个“Excel Source”数据流源,其中 OpenRowset 属性已设置为特定列范围:Sheet1$A1:A100。我直接在 Excel 源之后添加了一个网格数据查看器,以便我可以查看“原始”结果。

执行包时,我查看数据查看器结果,所有包含日期​​的单元格都显示为 NULL。奇怪的是,文本字段和数字字段表现良好并在数据查看器中正确显示。如果我在日期值前面放置一个单引号(撇号),以便将日期视为文本,则可以正确导入。如果我右键单击 Excel 中的日期字段之一并转到“设置单元格格式...”,它们都会显示为“日期”。

这些 Excel 文件是由我无法控制的过程自动生成的,并且我无法手动编辑每个文件以使它们正确导入。在 Excel 源的外部列部分中,该列显示的数据类型为“Unicode 字符串 [DT_WSTR]”,因此我认为它只会将其作为文本导入。

我非常感谢任何关于如何正确导入这些日期值的建议。先感谢您!

I have an Excel '97 spreadsheet that is generated by an external automated process that I would like to import into a SQL [2008 R2] table via SSIS. The column I am importing contains text values in some cells and date values in other cells. To connect to the Excel data, I have an "Excel Source" Data Flow Source, where the OpenRowset property has been set to a specific column range: Sheet1$A1:A100. I have added a grid Data Viewer directly after the Excel Source so that I can view the "raw" results.

When the package is executed, I review the Data Viewer results and all of the cells that contained a date come through as NULLs. Oddly, the text fields and number fields come through fine and show up correctly in the Data Viewer. If I put a single quotation mark (apostrophe) in front of the date value so that it treats the date as text, it imports properly. If I right-click on one of the date fields in Excel and go to "Format Cells...", they are all displayed as "Date".

These Excel files are being generated automatically by a process that I can't control, and I can't manually edit each file to get them to import properly. Within the External Columns portion of the Excel Source, the column is shown with a DataType of "Unicode string [DT_WSTR]", so I would think it would just import it as text.

I would GREATLY appreciate any suggestions on how I might get these date values to import properly. Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-12-16 15:48:11

我不知道SSIS中是否有一个“好”的方法来处理这种情况,但我可以想到一些或多或少丑陋的想法。为了增加黑客性:

  1. 要求向您提供这些文件的任何人使用更适合数据库的格式(而不是列的数据类型随行变化的格式)。
  2. 如果您提前知道哪些行将包含文本,哪些行将包含日期,您可以尝试多次打开电子表格,每次更改范围以使数据类型保持一致。
  3. 您可以在 Excel 中手动打开电子表格并将其另存为文本文件;然后将文本文件提供给您的 SSIS 包。所有日期都将转换为文本(根据您的区域设置)。
  4. 您可以编写一个 Windows 脚本来在 Excel 中打开电子表格并将其另存为文本文件,然后将该文本文件提供给您的 SSIS 包。
  5. 您可以编写一个 SSIS 脚本任务,使用 Excel 自动化模型打开电子表格并以任何适当的方式处理每个单元格。
  6. 您可以推出自己的 Excel 连接管理器,将其配置为将特定范围内的所有单元格视为文本。 (嗯。这可能会超越黑客行为并进入牦牛剃毛。)

我认为#1是最好的长期解决方案,但听起来它不会很快发生。也就是说,将电子表格保存为文本至少会使 SSIS 可以轻松处理它,并且这可能足以克服眼前的障碍。

I don't know if there is a "good" way to handle this situation in SSIS, but I can think of a few more-or-less ugly ideas. In order of increasing hackiness:

  1. Ask whoever provides these files to you to use a more database-friendly format (instead of one where the column's data type changes from row to row).
  2. If you know ahead of time which rows will contain text and which will contain dates, you could try opening the spreadsheet multiple times, changing the ranges each time so that the data type is consistent.
  3. You could manually open the spreadsheet in Excel and save it as a text file; then feed the text file to your SSIS package. All the dates will be converted to text (based on your locale).
  4. You could write a Windows script to open the spreadsheet in Excel and save it as a text file, then feed the text file to your SSIS package.
  5. You could write an SSIS Script Task that used the Excel automation model to open the spreadsheet and handle each cell in whatever the appropriate manner might be.
  6. You could roll your own Excel connection manager that could be configured to treat all cells in a particular range as text. (Hmmm. This may be getting past hacking and into yak shaving.)

I think #1 is the best long-term solution, but it doesn't sound like it'll happen quickly. That said, saving the spreadsheet as text will at least make it something SSIS can handle easily, and that might be enough to get over the immediate hurdle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文