使用 SSIS 将 Excel 个工作表从工作簿移动到另一个工作簿
Moving Excel Sheets From Workbook to Another Using SSIS
我有代码应该将 excel 模板 sheet 移动到新的工作簿中(见下文)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace CodeCall
{
public class Excel
{
public static void Main()
{
ApplicationClass app = new ApplicationClass();
Workbook curWorkBook = null;
Workbook destWorkbook = null;
Worksheet workSheet = null;
Worksheet newWorksheet = null;
Object defaultArg = Type.Missing;
try
{
// Copy the source sheet
curWorkBook = app.Workbooks.Open("filepath1", defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
workSheet = (Worksheet)curWorkBook.Sheets[1];
workSheet.UsedRange.Copy(defaultArg);
// Paste on destination sheet
destWorkbook = app.Workbooks.Open("filepath2", defaultArg, false, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
newWorksheet = (Worksheet)destWorkbook.Worksheets.Add(defaultArg, defaultArg, 6, defaultArg);
newWorksheet.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
}
catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show(exc.Message);
}
finally
{
if (curWorkBook != null)
{
curWorkBook.Save();
curWorkBook.Close(defaultArg, defaultArg, defaultArg);
}
if (destWorkbook != null)
{
destWorkbook.Save();
destWorkbook.Close(defaultArg, defaultArg, defaultArg);
}
}
app.Quit();
}
}
}
当我 运行 代码时,我收到此错误消息:
Error: Cannot load script for execution.
我已经尝试过将我的脚本代码复制并粘贴到新任务中。
有什么想法吗?
我解决了。
我需要将脚本入口点添加到代码
[SSISScriptTaskEntryPoint]//Attribute]
public partial class ScriptMain : VSTARTScriptObjectModelBase
去掉"Excel"publicclass
从 "Public Static Void Main()" 中获取代码,将其粘贴到新的脚本任务模板中并相应地更新命名空间来修复它。
我有代码应该将 excel 模板 sheet 移动到新的工作簿中(见下文)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace CodeCall
{
public class Excel
{
public static void Main()
{
ApplicationClass app = new ApplicationClass();
Workbook curWorkBook = null;
Workbook destWorkbook = null;
Worksheet workSheet = null;
Worksheet newWorksheet = null;
Object defaultArg = Type.Missing;
try
{
// Copy the source sheet
curWorkBook = app.Workbooks.Open("filepath1", defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
workSheet = (Worksheet)curWorkBook.Sheets[1];
workSheet.UsedRange.Copy(defaultArg);
// Paste on destination sheet
destWorkbook = app.Workbooks.Open("filepath2", defaultArg, false, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
newWorksheet = (Worksheet)destWorkbook.Worksheets.Add(defaultArg, defaultArg, 6, defaultArg);
newWorksheet.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
}
catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show(exc.Message);
}
finally
{
if (curWorkBook != null)
{
curWorkBook.Save();
curWorkBook.Close(defaultArg, defaultArg, defaultArg);
}
if (destWorkbook != null)
{
destWorkbook.Save();
destWorkbook.Close(defaultArg, defaultArg, defaultArg);
}
}
app.Quit();
}
}
}
当我 运行 代码时,我收到此错误消息:
Error: Cannot load script for execution.
我已经尝试过将我的脚本代码复制并粘贴到新任务中。
有什么想法吗?
我解决了。
我需要将脚本入口点添加到代码
[SSISScriptTaskEntryPoint]//Attribute] public partial class ScriptMain : VSTARTScriptObjectModelBase
去掉"Excel"publicclass
从 "Public Static Void Main()" 中获取代码,将其粘贴到新的脚本任务模板中并相应地更新命名空间来修复它。