How to Insert Rows to Table Object Inside an Excel Sheet?

后端 未结 4 1932
死守一世寂寞
死守一世寂寞 2021-02-19 04:46

I have difficulties trying to insert rows into an existing table object. Here is my code snippet:

string connectionString = \"Provider=Microsoft.ACE.OLEDB.12.0;D         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 05:24

    Try this

    private void GetExcelSheets(string FilePath, string Extension, string isHDR)
    {
    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;
    }
    
    //Get the Sheets in Excel WorkBoo
    conStr = String.Format(conStr, FilePath, isHDR);
    OleDbConnection connExcel = new OleDbConnection(conStr);
    OleDbCommand cmdExcel = new OleDbCommand();
    OleDbDataAdapter oda = new OleDbDataAdapter();
    cmdExcel.Connection = connExcel;
    connExcel.Open();
    
    //Bind the Sheets to DropDownList
    ddlSheets.Items.Clear(); 
    ddlSheets.Items.Add(new ListItem("--Select Sheet--", ""));    
    ddlSheets.DataSource=connExcel
             .GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    ddlSheets.DataTextField = "TABLE_NAME";
    ddlSheets.DataValueField = "TABLE_NAME";
    ddlSheets.DataBind();
    connExcel.Close();
    txtTable.Text = "";
    lblFileName.Text = Path.GetFileName(FilePath);
    Panel2.Visible = true;
    Panel1.Visible = false;
    }
    

提交回复
热议问题