How to assign object values to datatable in vb.net ?

Here is my vb.code. i want to loop through object svalue  and assign values to datatable. How to do this ?




 Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        string workbookPath = @"c:\temp\SomeWorkBook.xlsx";
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(workbookPath , Type.Missing, false, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        xlApp.Visible = false;
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        object svalue = xlWorkSheet.UsedRange.Value2;

Open in new window

Varshini SAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Is it an option to just load the data into a DataSet using ADO.NET as opposed to Excel Automation?
Try

    Dim cnn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Temp\SomeWorkBook.xlsx';Extended Properties=Excel 8.0;")
    Dim cmd As New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", cnn)
    Dim data As New System.Data.DataSet
    cmd.Fill(data)

    'data now has the Excel data. you can loop through the datatable and do what you want with it.
    'marshalling calls to fetch the data cell by cell wil be unnecessarily expensive

Catch ex As Exception
    Console.WriteLine(ex.ToString)
End Try

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Varshini SAuthor Commented:
Yes. You are correct.

i have converted your code vb.net. But it showing an below error when executing following line


System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=@'C:\\Temp\\SomeWorkBook.xlsx';Extended Properties=Excel 8.0;");


Error:  Format of the initialization string does not conform to specification starting at index 34.


My Code:

  System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=@'C:\\Temp\\SomeWorkBook.xlsx';Extended Properties=Excel 8.0;");
             System.Data.OleDb.OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", cnn);
            System.Data.DataSet data = new System.Data.DataSet();
            cmd.Fill(data);

Open in new window

Rgonzo1971Commented:
Hi,

pls try

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        Array value;
        value =  xlWorkSheet.UsedRange.Value;
        var iFila = value.GetLongLength(0);
        var iCol = value.GetLongLength(1);

        var dt = new DataTable();
        //AddColumns
        for (int c = 1; c < iCol; c++)
            dt.Columns.Add(c.ToString(), typeof(string));

        for (int j = 1; j < iCol; j++)
        {
            // create a DataRow using .NewRow()
            DataRow row = dt.NewRow();

            // iterate over all columns to fill the row
            for (int i = 1; i < iFila; i++)
            {
                row[i] = value.GetValue(i, j);
            }

            dt.Rows.Add(row);
        }

Open in new window

Regards
MlandaTCommented:
System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Temp\SomeWorkBook.xlsx';Extended Properties=Excel 8.0;");


You syntax is slightly incorrect. Try this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.