Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excel中读取下拉列表的值并添加到数据表C#_C#_Asp.net_Excel_Oledbconnection - Fatal编程技术网

如何在excel中读取下拉列表的值并添加到数据表C#

如何在excel中读取下拉列表的值并添加到数据表C#,c#,asp.net,excel,oledbconnection,C#,Asp.net,Excel,Oledbconnection,您好,我面临以下问题: 我有一个excel文件,其中有一些带有下拉列表的单元格。我试图读取excel并将其添加到datatable中,以便操作数据。我已设法读取excel并将其添加到数据表中,但我的方法读取带有dropdownlist的单元格的所有值,而不是仅读取选定的值。例如,我给了一个单元格三个值(1,2,3),该单元格选择了值1。我的方法读取excel并将不同行中的所有值添加到数据表中。所以它加了1,2,3。 您可以在下面看到我的代码: protected void ddlRevi

您好,我面临以下问题: 我有一个excel文件,其中有一些带有下拉列表的单元格。我试图读取excel并将其添加到datatable中,以便操作数据。我已设法读取excel并将其添加到数据表中,但我的方法读取带有dropdownlist的单元格的所有值,而不是仅读取选定的值。例如,我给了一个单元格三个值(1,2,3),该单元格选择了值1。我的方法读取excel并将不同行中的所有值添加到数据表中。所以它加了1,2,3。 您可以在下面看到我的代码:

    protected void ddlReviewStatus_SelectedIndexChanged(object sender, EventArgs e)
{
    string date = DateTime.Now.ToShortDateString();
    string finalDate = date.Replace("/", "");
    string fileName = "test.xlsx";

    DropDownList ddlReviewStatus = sender as DropDownList;
    GridViewRow row = (GridViewRow)ddlReviewStatus.NamingContainer;
    string companyName = ((Label)row.FindControl("lblCompName")).Text;

    string filePath = ReportTemplatesPath + "\\" + companyName + "\\" + finalDate + "\\" + fileName;

    if (ddlReviewStatus.SelectedItem.Text.ToUpper() == "DONE")
    {
        string extension = Path.GetExtension(fileName);
        string conStr = "";
        switch (extension)
        {
            case ".xls": //Excel 97-03
                conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"]
                         .ConnectionString;
                break;
            case ".xlsx": //Excel 07
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"]
                          .ConnectionString;
                break;
        }

        conStr = String.Format(conStr, filePath,"Yes");
        OleDbConnection connExcel = new OleDbConnection(conStr);
        OleDbCommand cmdExcel = new OleDbCommand();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        cmdExcel.Connection = connExcel;

        //Get the name of First Sheet
        connExcel.Open();
        DataTable dtExcelSchema;
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
        connExcel.Close();

        connExcel.Open();
        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
        oda.SelectCommand = cmdExcel;
        oda.Fill(dt);
        connExcel.Close();

        //Bind Data to GridView
        GridView1.Caption = Path.GetFileName(filePath);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    else
    {

    }
}