Compilation Error in SSIS script Task

jn93 671 Reputation points
2024-03-06T09:06:19.43+00:00

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;
    }
}

}

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,503 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,582 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,687 questions
{count} votes