2

I have some problems when I try to read specific rows of an Excel document.

My implementation is here: Reading Excel in c# where some columns are empty. How you can see, the entire Excel is taken into a DataTable.

Now I want get a specific range of n value:

enter image description here

I think is the DataTable the problem... I think that maybe I should to obtain another DataTable... maybe using another different query?

1 Answer 1

1

It seems that you are using a fixed template so you can solve using different queries on different ranges.

To select a particular range you can use this query:

string query = "SELECT * FROM [YourSheet$B58:D70]";

If you know where a range starts but not the number of rows you can use this syntax:

string query = "SELECT * FROM [YourSheet$B58:D]";

Using HDR=NO in your connection string and changing starting row you could use this query to simplify your next operations:

SELECT [F1] AS Compagnia,
    [F2] AS Agenzia,
    [F3] AS DataSinistro
FROM [YourSheet$B59:D]

Remember you can also use WHERE to filter your results or exclude empty rows; i.e.:

SELECT [F1] AS Compagnia,
    [F2] AS Agenzia,
    [F3] AS DataSinistro
FROM [YourSheet$B59:D]
WHERE [F3] IS NOT NULL
Sign up to request clarification or add additional context in comments.

1 Comment

We are close... In my template, columns are distant from each other, so this is my query: "SELECT [compagnia], [agenzia-esercizio-numero], [data sinistro] FROM [" + sheet + "$B58:DY1000]" ... 1000 is the limit (without this, continues to read to the infinite...). Sadly, the GridView loads all rows, also if empties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.