在Excel顶部插入记录

问题描述 投票:3回答:1

嗨,我有这个C#代码,它运作良好:

string fileFullPath = Dts.Variables["User::ExcelFileFullPath"].Value.ToString();

            //Create Excel Connection
            ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 12.0 XML;HDR=NO\";";
            OleDbConnection cnn = new OleDbConnection(ConStr);

            //Get Sheet Name
            cnn.Open();
            DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            foreach (DataRow drSheet in dtSheet.Rows)
            {
                if (drSheet["TABLE_NAME"].ToString().Contains("$"))
                {
                    if (drSheet["TABLE_NAME"].ToString().Contains("Survey"))
                    {
                        sheetname = drSheet["TABLE_NAME"].ToString();



                        OleDbCommand myCommand = new OleDbCommand();
                        string sql = null;


                        myCommand.Connection = cnn;
                        sql = "Insert into [" + sheetname + "] values('5','e')";
                        myCommand.CommandText = sql;
                        myCommand.ExecuteNonQuery();
                        cnn.Close();
                    }

                }
            }

但是这段代码在excel文件的末尾插入记录,我需要做的是在第一行插入该记录(A1:B1)

有什么建议吗?

c# excel ssis oledb script-task
1个回答
1
投票

您需要指定工作表名称旁边的范围:

sql = "Insert into [" + sheetname + "A1:B1] values('5','e')"

可以在以下问题中找到更多其他信息:

如果您正在寻找有关使用OLEDB对EXCEL执行SELECT,INSERT,UPDATE操作的详细文章,请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.