简体   繁体   English

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

[英]Update the format of an Excel file in SSIS script task

I have an SSIS package which runs an SQL script and puts the results into an Excel file;我有一个运行 SQL 脚本并将结果放入 Excel 文件的 SSIS 包; the file at the beginning has just a header row with bolded column names.开头的文件只有一个带有粗体列名称的标题行。

What I have found is that, no matter how my data is formatted in my data flow tasks, my numbers end up going into the Excel file as text.我发现,无论我的数据在我的数据流任务中如何格式化,我的数字最终都会作为文本进入 Excel 文件。

Could somebody please help me with a script task that will update the columns O and P to be displayed correctly as currency, with the £ symbol.有人可以帮我完成一个脚本任务,该任务将更新列OP以正确显示为货币,并带有 £ 符号。

This is how they look after SSIS imports the data:这是他们在 SSIS 导入数据后的处理方式:

在此处输入图片说明

This is how I require them to look:这就是我要求他们看的方式:

在此处输入图片说明

You should use Mcirosot.Office.Interop.Excel.dll within a script task to change the column number format as following:您应该在脚本任务中使用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);

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM