如何使用c#读取Excel中每个单元格的数据列标题和数据

时间:2011-08-23 16:44:05

标签: c# excel c#-4.0 excel-2007

我有一张类似于:

的Excel表格

excel screenshot

我想读取数据列标题:All,col1,col2,col3,col4,col5

并获取所有单元格数据。例如,Row = 2和第2列= 0

中的单元格

我目前正在编写此代码:

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());
}

但它只返回一个包含1列的数据表,只返回行1..5文本。

我怎么能这样做?

请在不使用Linq to Excel Provider和Open Xml的情况下说出答案。

编辑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 fooData.Rows)
            {
                Response.Write(row[0].ToString());
            }

        }

4 个答案:

答案 0 :(得分:1)

您可以通过fooData.Columns[0].ColumnName获取第一列的列名称 - 请参阅http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx

修改

SELECT更改为SELECT * FROM并使用Fill (0, 5, new DataTable[] { fooData })

答案 1 :(得分:0)

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

试试这个。它应该将所有单元格从A1返回到C5。

答案 2 :(得分:0)

您选择的驱动程序适用于Excel 2007(connectionString中的“Provider = Microsoft.ACE.OLEDB。 12 .0”)。您的Excel文件是2007还是2010?

答案 3 :(得分:0)

仅适用于逻辑的数据列标题:

                 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;
                            }
                        }
                    }
                }