OleDb connection to Excel; how do I select fixed width, unbounded height?

前端 未结 5 698
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 09:14

I\'m using OleDb to select data from excel spreadsheets. Each spreadsheet can contain many small tables, and possibly furniture like titles and labels. So it might look like

5条回答
  •  死守一世寂寞
    2021-01-13 10:07

    Couple possible solutions:

    1. Put your tables on separate worksheets, then simply query the whole worksheet.
    2. Give each table in Excel a name (in Excel 2007, select the table, right-click, and choose Name a range...), then in your query, use this name instead of "Sheet1$B14:D65535".

    Hope that helps.

    EDIT

    Here's a third idea:

    I'm not sure what you're using to query your database, but if your query engine supports variables (like Sql Server, for example) you could store the result of...

    SELECT COUNT(*) FROM NameOfServer...Sheet1$

    ...in a variable called @UsedRowCount, that will give you the number of rows actually used in the worksheet. So, @UsedRowCount = LastRowUsed - InitialBlankRows.

    You might then be able to use string concatenation to replace "65535" with @UsedRowCount + @InitialBlankRows. You would have to set @InitialBlankRows to a constant (in your example, it would be 3, since the heading row of the first table is located at Row 4).

提交回复
热议问题