Question

I want to import the excel file which has dynamic columns(columns will be vary file to file).
Situation is
1 .In the first row it has table name and date.
2. in second row it contains the column names
3. From the third row it contains the data which need to import.
4. This columns(second row header) will vary based on the tables.
Refer to the sample excel format.

----------
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
----------

I want to get specifically based on the row number. means i want to split the header rows and data rows. and save data rows into database based on the column names(second row).

Was it helpful?

Solution

I have the same problem which I have partially solved. The order of the columns is important for this to work.
If the first row is a header, the columns can be imported easily as SQL and can be in any order:

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

and if the first row is not the header the order of the columns is important. It can be imported from SQL as:

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'

Note the difference in the column names. The problem I'm having is getting the second one to import without errors. The first import is using the first row as header, the second is not. What is important for the second one to work is that you have known empty columns and the first column of the header to be a fixed name. You might try using a file naming convention to change which table the data is pointed to.
Edit: Found an answer at Importing from Excel - Header is not on row 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top