简体   繁体   中英

SSIS to Excel - Excel Formulas not running automatically?

I have an SSIS package I created that currently has the following steps:

1 - File System Task - Copy an Excel File (.xlsx) to use as a template for the output of the SSIS package. The Excel file has 3 tabs - one that has no data in it, but has formulas that reference values on the second and third tabs. This is the page the End User looks at (let's call it end_user).

2 - Data Flow Task - OLEDB Source -> Data Conversion -> Excel Destination (Tab 2, lets call this raw_data).

3 - Email Task - Hasn't been implemented yet, but will be here at the final stage of the product, to make this fully automated.

Upon the package finishing being run, I open the Excel file and the data has been written to raw_data, but nothing shows up on end_user. Here is what I have already tried, to resolve this in Excel:

  • File -> Options -> Formulas -> Automatic (where it already was). And I tried every other choice as well (Automatic except for data tables, Manual, Manual with Recalculate workbook before saving, and back to Automatic).
  • Ensured the appropriate data types were selected for each column in raw_data, and end_user [ie Number, Currency, Short Date, etc.]
  • Tried setting all data field types to General
  • Using a different Excel file as a template.
  • Copying the formulas into Notepad, putting a random number in place of the formula in Excel, save and close the file, reopen, and put the formula back in.
  • Deleting the SSIS Package (.dtsx) and make a new one.
  • Creating an Excel macro to force formula updates at the time the file is opened.

     Private Sub Workbook_Open() Application.Calculation = xlCalculationAutomatic End Sub
  • Running a Repair on Office.

  • Press Ctrl + Alt + F9 in Excel. ( This makes the formulas recalculate, and does make the values show up the way they are supposed to. )

While the Ctrl + Alt + F9 option makes the file display as intended, it is not an acceptable solution, as we all know how end users want everything to work perfectly. Does anyone have any suggestions, experience with this happening, or any other insight? Anything helpful is greatly appreciated!

I had an identical situation. So even though this question is over 3 years old, I'm hoping my solution may help others down the line.

I created a Script Task, with the following C# code (You'll need only to edit TheNameOfYourExcelConnection.xls).

The script will connect to the spreadsheet, iterate over every cell in every sheet, and replace every occurrence of "=" with itself. In effect not changing the formulae, but forcing them to be recalculated.

#region Namespaces
using System;
using Microsoft.SqlServer.Dts.Runtime;


//add reference C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
using Excel = Microsoft.Office.Interop.Excel;
#endregion

namespace ST_10cafd32acda4eb1b677ebbdd2eb1286
{

    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

        public void Main()
        {
            Excel.Application excelApp = new Excel.Application();
            ConnectionManager cm = Dts.Connections["TheNameOfYourExcelConnection.xls"];
            string filepath = cm.Properties["ExcelFilePath"].GetValue(cm).ToString();

            excelApp.DisplayAlerts = false;
            Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filepath, 1, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, null, false);
            try
            {
                Excel.Sheets excelWorkSheet = excelWorkbook.Sheets;
                foreach (Excel.Worksheet work in excelWorkSheet)
                {
                    work.Cells.Replace("=", "=");

                }
                excelWorkbook.Save();
                excelWorkbook.Close();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                excelApp.Quit();
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }

        #region ScriptResults declaration
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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