次のスクリプトタスクと一緒に、あなたはSSISで変数「ExcelFilePath」を作成し、タスクへのそれを渡す必要があります(ファイル内のすべてのワークシートから、トップ2行を削除します'システム:: TaskName:指定'):
public void Main()
{
MainTask();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void MainTask()
{
xl.Application xlApp = null;
xl.Workbook excelFile = null;
string excelFilePath = Dts.Variables["User::ExcelFilePath"].Value.ToString();
string thisTask = Dts.Variables["System::TaskName"].Value.ToString();
try
{
xlApp = new xl.Application();
excelFile = xlApp.Workbooks.Open(excelFilePath);
xlApp.DisplayAlerts = false;
foreach (xl.Worksheet ws in excelFile.Worksheets)
{
ws.Rows["1:2"].EntireRow.Delete();
}
xlApp.DisplayAlerts = true;
excelFile.Save();
excelFile.Close();
xlApp.Quit();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.Events.FireError(0, thisTask, ex.Message, String.Empty, 0);
if (excelFile != null) excelFile.Close(SaveChanges:false);
if (xlApp != null) xlApp.Quit();
}
}
あなたは 'COM' に参照を追加する必要があります> 'Microsoft Excelの[バージョン番号]オブジェクトライブラリ'(あなたが持っている方のバージョン)と '.NET'>「マイクロソフト。Cシャープ'。次に、 '名前空間'領域にusing xl = Microsoft.Office.Interop.Excel;
を宣言する必要があります。