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?

Community
  • 1
  • 1
Gioce90
  • 554
  • 2
  • 10
  • 31

1 Answers1

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
tezzo
  • 10,858
  • 1
  • 25
  • 48
  • 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. – Gioce90 Oct 29 '15 at 11:30