Compilation Error in SSIS script Task
Dear Expert,
I have gotten the script to change the data type of the excel using C#. The code is working when I use the console app in visual studio. But its not working when using SSIS script task in visual studio. Its have the compilation error. I'm not sure what causes this. Appreciate if anyone able to help on this. I have provide code below for your reference.
using System; using System.Collections.Generic; using System.Data; using Excel = Microsoft.Office.Interop.Excel; namespace ConsoleAppTest { class Program { static void Main(string[] args) { // Specify the path to your Excel file string excelFilePath = @"C:\Documents\TEMP\SAMPLE_FILE.xlsx"; // Initialize Excel application Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Open(excelFilePath); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1]; // Assuming first sheet // Define a dictionary to map column name to their respective formats Dictionary<string, string> columnFormats = new Dictionary<string, string>() { {"TOTALDUE","0.00"}, {"AGING_PREM","0.00"},
}; // Loop through each column name foreach (var kvp in columnFormats) { string columnName = kvp.Key; string format = kvp.Value; // Find the column index of the current column name int columnIndex = FindColumnIndex(worksheet, columnName); // If the column is found and there is data in the column, format it as numeric if (columnIndex != -1) { // Get the entire column range Excel.Range columnRange = worksheet.Columns[columnIndex]; // Filter out blank cells Excel.Range nonBlackCells = columnRange.SpecialCells(Excel.XlCellType.xlCellTypeConstants, Excel.XlSpecialCellsValue.xlTextValues); // If there are non-blank cells in the column if (nonBlackCells != null) { // Apply numeric format to the column nonBlackCells.Value = worksheet.Application.Evaluate("IFERROR(VALUE(" + columnRange.Address + "), " + columnRange.Address + ")"); nonBlackCells.NumberFormat = format; // Customize as needed } } } // Save changes and close Excel workbook.Save(); workbook.Close(); excelApp.Quit(); } // Helper method to find the column index by column name private static int FindColumnIndex(Excel.Worksheet worksheet, string columnName) { //find the column index of the exact column name match in the first row foreach (Excel.Range cell in worksheet.Rows[1].Cells) { if (cell.Value2 != null && cell.Value2.ToString().Equals(columnName, StringComparison.OrdinalIgnoreCase)) { return cell.Column; } } // Column not found return -1; } }
}