简体   繁体   English

从 c# 中的 excel sheet 中读取数据,将数据加载到一行中每一列的字符串中

[英]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.我正在从 excel 表中读取数据。 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.我的 excel 将只有三列。

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我得到的output是

    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.您将所有数据都放在一个字符串中,因为您将其全部直接写入响应 stream。您需要按照 Arion 的建议添加标记,或者将这些值实际添加到某些数据结构中。 Also, have you thought about using the Excel Object Library ?另外,您是否考虑过使用Excel Object 库 It might be a bit cleaner, plus you can iterate the native structure of the file directly.它可能会更干净一些,而且您可以直接迭代文件的本机结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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