Export Worksheet to DataTable

25 views
Skip to first unread message

Fábio Rehm

unread,
Feb 5, 2009, 2:45:58 PM2/5/09
to excellibrary
Hy guys,

First I'd like to say that I'm very pleased that I've found this
library.
I was having some trouble reading data using Microsoft Jet because the
Worksheets that I have to work on has lots of blank / null values in
the first rows, that represents a problem as discussed in
http://blog.lab49.com/archives/196.
Since I couldn't manage to get it to work and coulnd't use Interoping
on my ISP, I was about to give up on it and start to work with plain
text (.CSV) until I found this library.

To stop me from rewriting everything again, I created a simple method
on Worksheet class that exports data as a DataTable.
public DataTable ToDataTable()
{
// Creates DataTable from a Worksheet
// All values will be treated as Strings
DataTable dt = new DataTable();
int rowCount = Cells.LastRowIndex;
int columnCount = Cells.LastColIndex;

// Extract columns
for (int i = 0; i <= columnCount; i++)
dt.Columns.Add(Cells[0, i].StringValue, typeof
(String));

// Extract data
for (int currentRowIndex = 1; currentRowIndex <= rowCount;
currentRowIndex++)
{
DataRow dr = dt.NewRow();
for (int currentColumnIndex = 0; currentColumnIndex <=
columnCount; currentColumnIndex++)
dr[currentColumnIndex] = Cells[currentRowIndex,
currentColumnIndex].StringValue;
dt.Rows.Add(dr);
}

return dt;
}
I know that this is not the purpose of the library but maybe someone
else can benefit from this. Fell free to include it on the library if
you want.


Keep up with the good work,
--
Fábio Rehm

PS: Sorry about my english, since it is not my native language (I'm
writting from Brazil) you may find something "weird" in the message.
Just let me know if you don't understand something.

Jerry

unread,
Feb 6, 2009, 7:39:58 PM2/6/09
to excellibrary
Hi

Thanks for sharing your code.

I will include them into codebase later

Jerry.

On Feb 6, 6:45 am, Fábio Rehm <fgr...@gmail.com> wrote:
> Hy guys,
>
> First I'd like to say that I'm very pleased that I've found this
> library.
> I was having some trouble reading data using Microsoft Jet because the
> Worksheets that I have to work on has lots of blank / null values in
> the first rows, that represents a problem as discussed inhttp://blog.lab49.com/archives/196.
Reply all
Reply to author
Forward
0 new messages