Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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
如何仅使用OLEDB C#SSI在excel文件中创建现有excel工作表(表)的新副本_C#_Excel_Ssis_Oledb - Fatal编程技术网

如何仅使用OLEDB C#SSI在excel文件中创建现有excel工作表(表)的新副本

如何仅使用OLEDB C#SSI在excel文件中创建现有excel工作表(表)的新副本,c#,excel,ssis,oledb,C#,Excel,Ssis,Oledb,我正在尝试创建excel模板的副本,该模板当前已存在于正在处理和插入数据的excel文档中。该代码设计用于从表中获取数据,将其插入Excel文件的特定单元格中,这些单元格上面有突出显示的列,以便对其进行格式化。我正在使用SSIS脚本任务迭代表中的每一行数据。插入数据后,我将尝试为要插入的下一行数据创建模板的副本 //Foreach record set of data in table //Insert data into the template into the specific cells

我正在尝试创建excel模板的副本,该模板当前已存在于正在处理和插入数据的excel文档中。该代码设计用于从表中获取数据,将其插入Excel文件的特定单元格中,这些单元格上面有突出显示的列,以便对其进行格式化。我正在使用SSIS脚本任务迭代表中的每一行数据。插入数据后,我将尝试为要插入的下一行数据创建模板的副本

//Foreach record set of data in table
//Insert data into the template into the specific cells
//Create new sheet for next iteration of data to be inserted
//end script
我只能使用System.Data.OleDb方法/库,因为此时UAT和PROD服务器无法接受其他库。所以算法是这样设置的。脚本在Foreach循环中,因此

我希望它能够创建当前excel工作表的副本,并将数据插入其中

using (OleDbConnection ExcelConnection = new OleDbConnection(ExcelLocationConnectionString))
{

    ExcelConnection.Open();
    DataTableCollection TablesInFile = ExcelConnection.GetSchema("Tables").DataSet.Tables;

    using (OleDbCommand InsertDataCommand = new OleDbCommand())
    {
        InsertDataCommand.Connection = ExcelConnection;
        DataTable TableToInsertDataInto = TablesInFile[TablesInFile.Count];//Index could be out of range

        if (YestAccountBranch == TodayAccountBranch)           
            AccountBranchChange = "No changes have been made to Account Branch.";
        
        if (YestAccountType == TodayAccountType)           
            AccountTypeChange = "No changes have been made to Account Type.";
        
        if (YestCostCenter == TodayCostCenter)            
            CostCenterChange = "No changes have been made to Cost Center.";
        
        InsertDataSQL = "INSERT INTO [" + TableToInsertDataInto.ToString() + "$B5:F5:I5:L5:P5:T5:Y5:F2] VALUES('" + AccountNumber + "','" +
                    "" + CostCenterChange + "','" + AccountTypeChange + "','" + AccountBranchChange + "','" + TransactionLimit +
                    "','"+ DailyCumulativeLimit +"','" + BLCompanyID + "/" + ClientName + "','" + DateTime.Now.ToShortDateString().ToString() + "')";

        InsertDataCommand.CommandText = InsertDataSQL;
        InsertDataCommand.ExecuteNonQuery();

    }

    //Create table in excel sheet
    using (OleDbCommand CreateTableCommand = new OleDbCommand())
    {
        CreateTableCommand.Connection = ExcelConnection;

        string tableName = "AccountChange" + TablesInFile.Count.ToString() + 1;
        CreateTableCommand.CommandText = $"CREATE TABLE [{tableName}]";

    }
        
}

用另一个问题全盘替换一个旧问题并不能保证获得更好的接受,而且往往会导致相反的结果:社区对可以被视为绕过正常问题生命周期的尝试产生强烈反应。另外,你要冒着真正的风险,把新帖子偏离主题,恐怕你在这里的尝试并没有什么进步。我已经锁定了帖子,以阻止这里不断发生的回退战。请注意,版主们不得不不断地清理掉那些完全站在我们立场错误的评论。请记住,我们要求每个人都以文明的方式行事。