c# - 在 SSIS 脚本任务中更新 Excel 文件的格式

标签 c# excel vb.net ssis etl

我有一个 SSIS 包,它运行 SQL 脚本并将结果放入 Excel 文件中;开头的文件只有一个带有粗体列名称的标题行。

我发现,无论我的数据在数据流任务中如何格式化,我的数字最终都会以文本形式进入 Excel 文件。

有人可以帮助我完成一个脚本任务,该任务将更新列 OP 以正确显示为货币,并带有 £ 符号。

这是他们在 SSIS 导入数据后的样子:

enter image description here

这就是我要求它们看起来的样子:

enter image description here

最佳答案

您应该在脚本任务中使用 Mcirosot.Office.Interop.Excel.dll 来更改列号格式,如下所示:

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

string inputFile = @"D:\Test.xlsx";

Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(inputFile);
ExcelApp.Visible = false;
//O index is 15
ExcelWorksheet.Columns[15].NumberFormat = "£#,##0.00";
//P index is 16
ExcelWorksheet.Columns[16].NumberFormat = "£#,##0.00";
ExcelWorkbook.Save();

GC.Collect();
GC.WaitForPendingFinalizers();

ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(ExcelWorkbook);

ExcelApp.Quit();
Marshal.FinalReleaseComObject(ExcelApp);

引用文献

关于c# - 在 SSIS 脚本任务中更新 Excel 文件的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60543651/

相关文章:

c# - 如何制定标准(C# 与 VB.NET)

c# - 预编译一个非常大的正则表达式

javascript - Response.write(脚本) 不起作用。

Excel 不发布形状的创建名称

html - 如何使用 INLINE CSS 将 Excel 电子表格导出到 HTML 表格?

excel - 删除所选区域中的评论

c# - 字段正在更新为 NULL 值,即使它被排除在属性绑定(bind)之外

c# - TreeNode 右键单击​​选项

c# - 我误解了 PetaPoco.IgnoreAttribute 吗?

c# - Entity Framework : Can DB Contexts Have Foreign Keys Across Different Schemas?