0

我想导入具有动态列的 excel 文件(列将因文件而异)。
情况是
1 。在第一行它有表名和日期。
2. 第二行包含列名
3. 第三行包含需要导入的数据。
4. 此列(第二行标题)将根据表格而有所不同。
请参阅示例 excel 格式。

----------
Table Name                  Exported Date: 09_01_2012
----------
Col1   Col2  Col3  Col4     Col5               Col6
----------
2      3     4     0        8/27/2012 13:04    0
4      3     4as   0        8/27/2012 13:04    0
8      3     aas   0        8/27/2012 13:04    3
----------

我想根据行号来具体获取。意味着我想拆分标题行和数据行。并根据列名(第二行)将数据行保存到数据库中。

4

1 回答 1

0

我有同样的问题,我已经部分解决了。列的顺序对于此工作很重要。
如果第一行是标题,则可以轻松地将列作为 SQL 导入,并且可以按任意顺序:

SELECT Col1 AS [DBCol1], 
Col2 AS [DBCol2], 
Col3 AS [DBCol3], 
Col4 AS [DBCol4], 
Col5 AS [DBCol5], 
Col6 AS [DBCol6]
FROM SheetName$

如果第一行不是标题,则列的顺序很重要。它可以从 SQL 导入为:

SELECT F1 AS [DBCol1],
F2 AS [DBCol2],
F3 AS [DBCol3],
F4 AS [DBCol4],
F5 AS [DBCol5],
F6 AS [DBCol6]
FROM SheetName$
WHERE F2 IS NOT NULL AND F3 IS NOT NULL AND F1 <> 'Col1'

注意列名的不同。我遇到的问题是让第二个导入没有错误。第一个导入使用第一行作为标题,第二个不是。对于第二个工作来说,重要的是您知道空列和标题的第一列是固定名称。您可以尝试使用文件命名约定来更改数据指向的表。
编辑:在从 Excel 导入中找到答案- 标题不在第 1 行

SELECT F1 AS [DBCol1],
F2 AS [DBCol2],
F3 AS [DBCol3],
F4 AS [DBCol4],
F5 AS [DBCol5],
F6 AS [DBCol6]
FROM [SheetName$A3:G65536]


Edit: I finally found the answer that I needed.
First - My entire problem happened because the first row of the spreadsheet was not the header.
The problem I was having had to do with the first column having to be required to be text only. No mixed data types. Any data in that first column had to be forced to be text by pre-pending an apostrophe (') to any numerical cells. This was not necessary if the header row was the first row and the "First row is header" checkbox was checked. Many of the other parts of the solution were required also. The "WHERE" clause was necessary but the array selection (A3:G65536) was not.

于 2013-04-17T18:00:34.787 回答