简体   繁体   中英

Reading data from excel sheet in c#, loading data into a strings for each column in a row

I am working on reading data from excel sheet. My table will look like this.

        AAAA            bbbbb              cccc
        1                2                  3
        4                5                  6
        --------------------------------------



        --------------------------------------

       data           data                  data

I want to loop though each and every row and save each value at each cell in a seperate string.

Here as per my table,

I want to store,

    string column1= 1;
    string cloumn2=2;
    string clomnn3= 3;

I want to repeat this for all rows. My excel will have only three columns.

I have tried this code

        //Reading Excel file
        string datafilename = @"D:\Book2.xls";
        string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + datafilename + ";" + "Extended Properties=Excel 12.0;"; 
        OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
        //fetching excel data into DataTable 
        System.Data.DataTable _dtDataFile = new System.Data.DataTable();
        ArrayList abcd = new ArrayList();
        myCommand.Fill(_dtDataFile);

        foreach (DataRow row in _dtDataFile.Rows)
        {
            Response.Write(row[i]);

        } 

The output i got is

    12346............datadatadata

I am getting all data in a single string, i want to store data in seperate strings for each row.

Can any one suggest, how to do this.

Thank you.

You're getting all your data in one string because you're writing it all straight to the response stream. You need to either add markup as Arion suggested, or actually add these values to some data structure. Also, have you thought about using the Excel Object Library ? It might be a bit cleaner, plus you can iterate the native structure of the file directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM