开发者

how to read data column headers and data of each cell in Excel Using c#

开发者 https://www.devze.com 2023-03-30 07:42 出处:网络
I have an excel sheet similar to : I want to read data columns header:All,col1,col2,col3,col4,col5 and get all cell data.for example cell in Row = 2 and column 2 = 0

I have an excel sheet similar to :

how to read data column headers and data of each cell in Excel Using c#

I want to read data columns header:All,col1,col2,col3,col4,col5

and get all cell data.for example cell in Row = 2 and column 2 = 0

I currently write this code :

OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT top 5 * FROM " + excelSheets[j], connString);
DataTable fooData = new DataTable();
dbAdapter.Fill(fooData);
foreach (DataRow row in fooData.Rows)
{
    Response.Write(row[0].ToString());
    //Response.Write(row[1].ToString());
}

but it return just a data table with 1 column and just row 1..5 text.

How I can do this?

Please say answer without using Linq to Excel Provider and Open Xml.

Edit 1:

string file_name = "C:\\Book1.xlsx";
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file_name + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
        objConn = new OleDbConnection(connString);
        objConn.Open();
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        if (dt == null)
        {
            Response.Write("Not Exist");
        }

        String[] excelSheets = new String[dt.Rows.Count];
        int i = 0;

        foreach (DataRow row in dt.Rows)
        {
            excelSheets[i] = row["TABLE_NAME"].ToString();
            i++;
        }

        // Loop through all of the sheets if you want too...
        for (int j = 0; j < excelSheets.Length; j++)
        {
            OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT top 100 * FROM " + excelSheets[j], connString);
            DataTable fooData = new DataTable();
            dbAdapter.Fill(fooData);
            foreach (DataRow row in fo开发者_Go百科oData.Rows)
            {
                Response.Write(row[0].ToString());
            }

        }


You get the column name for the first column for example by fooData.Columns[0].ColumnName - see http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx

EDIT:

Change the SELECT to SELECT * FROM AND use Fill (0, 5, new DataTable[] { fooData }).


"SELECT * FROM [" +  excelSheets[j] + "$A1:C5]"

Try this one. It should return all of the cells from A1 to C5.


The driver that you selected is for Excel 2007 ("Provider=Microsoft.ACE.OLEDB.12.0" in your connectionString). Is your Excel file 2007 or 2010?


For Only data column header for logic is here:

                 string filePath = "C:\\Book1.xlsx";
                string connString = string.Empty;
                if (path.EndsWith(".xlsx"))
                {
                    //2007 Format
                    connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'", path);
                }
                else
                {
                    //2003 Format
                    connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'", path);
                }
                using (OleDbConnection con = new OleDbConnection(connString))
                {
                    using (OleDbCommand cmd = new OleDbCommand())
                    {
                        //Read the First Sheet
                        cmd.Connection = con;
                        con.Open();
                        DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        con.Close();
                        string firstSheet = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

                        //Read the Header Row
                        cmd.CommandText = "SELECT top 1 * From [" + firstSheet + "]";
                        using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                        {
                            DataTable HeaderColumns = new DataTable();
                            da.SelectCommand = cmd;
                            da.Fill(HeaderColumns);
                            List<string> Filedata = new List<string>();
                            foreach (DataColumn column in HeaderColumns.Columns)
                            {
                                string columnName = HeaderColumns.Rows[0][column.ColumnName].ToString();


                                Filedata.Add(columnName);

                                ViewBag.Data = Filedata;
                            }
                        }
                    }
                }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号