简体   繁体   English

SSIS 到 Excel - Excel 公式没有自动运行?

[英]SSIS to Excel - Excel Formulas not running automatically?

I have an SSIS package I created that currently has the following steps:我创建了一个 SSIS 包,目前有以下步骤:

1 - File System Task - Copy an Excel File (.xlsx) to use as a template for the output of the SSIS package. 1 - 文件系统任务 - 复制 Excel 文件 (.xlsx) 以用作 SSIS 包输出的模板。 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. Excel 文件有 3 个选项卡 - 其中一个没有数据,但具有引用第二个和第三个选项卡上的值的公式。 This is the page the End User looks at (let's call it end_user).这是最终用户查看的页面(我们称之为 end_user)。

2 - Data Flow Task - OLEDB Source -> Data Conversion -> Excel Destination (Tab 2, lets call this raw_data). 2 - 数据流任务 - OLEDB 源 -> 数据转换 -> Excel 目标(选项卡 2,我们称之为 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. 3 - 电子邮件任务 - 尚未实施,但将在产品的最后阶段出现,以使其完全自动化。

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.包运行完成后,我打开 Excel 文件,数据已写入 raw_data,但 end_user 上没有任何显示。 Here is what I have already tried, to resolve this in Excel:这是我已经尝试过的,以在 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.]确保为 raw_data 和 end_user [即数字、货币、短日期等] 中的每一列选择了适当的数据类型
  • Tried setting all data field types to General尝试将所有数据字段类型设置为常规
  • Using a different Excel file as a template.使用不同的 Excel 文件作为模板。
  • 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.将公式复制到记事本中,用随机数代替 Excel 中的公式,保存并关闭文件,重新打开,然后重新输入公式。
  • Deleting the SSIS Package (.dtsx) and make a new one.删除 SSIS 包 (.dtsx) 并创建一个新包。
  • Creating an Excel macro to force formula updates at the time the file is opened.创建 Excel 宏以在打开文件时强制更新公式。

     Private Sub Workbook_Open() Application.Calculation = xlCalculationAutomatic End Sub
  • Running a Repair on Office.在 Office 上运行修复。

  • Press Ctrl + Alt + F9 in Excel.在 Excel 中按Ctrl + Alt + F9 ( 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.虽然Ctrl + Alt + F9选项使文件按预期显示,但它不是一个可接受的解决方案,因为我们都知道最终用户希望一切都完美运行。 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.所以即使这个问题已经超过 3 年了,我还是希望我的解决方案可以帮助其他人。

I created a Script Task, with the following C# code (You'll need only to edit TheNameOfYourExcelConnection.xls).我创建了一个脚本任务,使用以下 C# 代码(您只需要编辑 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

    }
}

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

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