在SSIS中使用C#脚本任务使Excel超链接可点击

发布于 2025-01-15 21:49:32 字数 2232 浏览 3 评论 0原文

我有一个 SSIS 包,它通过数据流任务用数据填充现有的 Excel(模板)。我遇到的问题是,我正在动态创建工作表,并希望添加从主页到该工作簿中相应工作表的超链接。

我将 =Hyperlink 引用添加到数据流任务中的相应工作表中,它执行得很好,但链接(尽管列类型为“常规”)实际上并未显示为超链接,直到我实际单击单元格,然后看起来不错。

输入图片此处描述

我想我可以使用 C# 脚本任务将列翻转为“文本”,然后返回“常规”,也许这会使其正确读取,但没有运气。有什么想法可以模拟我列中每个单元格的双击吗?

这是我到目前为止所拥有的,它可以将列转换为常规并将文本包装在另一列中,但我不知道如何使该公式正确显示为超链接。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

#endregion

namespace ST_2bdf93d5542441248076f053703d32c9
{

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    public void Main()
    {
        string inputFile = (string)Dts.Variables["RecommendationFileName"].Value;
        Excel.Application ExcelApp = new Excel.Application();
        Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(inputFile);
        ExcelApp.Visible = true;

        Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)ExcelWorkbook.Worksheets.get_Item(2);
        xlWorkSheetFocus.Activate();
        xlWorkSheetFocus.Select(Type.Missing);
        Excel.Range usedRange = xlWorkSheetFocus.UsedRange;


        xlWorkSheetFocus.Columns[5].NumberFormat = ""; //Make column 5 type General
        xlWorkSheetFocus.Columns[4].WrapText = true; //Wrap text in column 4


        ExcelWorkbook.Save();

        GC.Collect();
        GC.WaitForPendingFinalizers();

        ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
        Marshal.FinalReleaseComObject(ExcelWorkbook);

        ExcelApp.Quit();
        Marshal.FinalReleaseComObject(ExcelApp);
    }
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
}
}

I have an SSIS package which is populating an existing excel (template) with data via a Data Flow Task. The issue I am having is that I am dynamically creating worksheets and want to add a Hyperlink from the main page to the appropriate worksheet in that workbook.

I add the =Hyperlink reference to the appropriate sheet in the data flow task and it carries through just fine but the link (despite the column type being 'General') is not actually showing as a Hyperlink until I actually click in the cell, then it looks good.

enter image description here

I thought I could use a C# script task to flip the column to 'Text' and then back to 'General' and maybe that would make it read properly but no luck. Any ideas how I can simulate a double click in each of the cells in my column?

Here is what I have so far, which works for converting the column to general and wrapping text in another column but I am at a loss as to how to make this formula display properly as a Hyperlink.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

#endregion

namespace ST_2bdf93d5542441248076f053703d32c9
{

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    public void Main()
    {
        string inputFile = (string)Dts.Variables["RecommendationFileName"].Value;
        Excel.Application ExcelApp = new Excel.Application();
        Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(inputFile);
        ExcelApp.Visible = true;

        Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)ExcelWorkbook.Worksheets.get_Item(2);
        xlWorkSheetFocus.Activate();
        xlWorkSheetFocus.Select(Type.Missing);
        Excel.Range usedRange = xlWorkSheetFocus.UsedRange;


        xlWorkSheetFocus.Columns[5].NumberFormat = ""; //Make column 5 type General
        xlWorkSheetFocus.Columns[4].WrapText = true; //Wrap text in column 4


        ExcelWorkbook.Save();

        GC.Collect();
        GC.WaitForPendingFinalizers();

        ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
        Marshal.FinalReleaseComObject(ExcelWorkbook);

        ExcelApp.Quit();
        Marshal.FinalReleaseComObject(ExcelApp);
    }
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2025-01-22 21:49:32

看起来我找到了一个可行的解决方案,使用文本到列,选择不存在的分隔符“@”,并将结果写入同一列/单元格。猜测可能有更好的方法,但它给了我一个可点击的链接。

xlWorkSheetFocus.get_Range("E6", ("E6" +
  xlWorkSheetFocus.UsedRange.Rows.Count)).TextToColumns(Type.Missing,
  Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
  Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, true,
  Type.Missing, Type.Missing, false, true, Type.Missing,
  "@", Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Well looks like I found a workable solution using text to columns, picking a delimiter "@" which doesn't exists, and writing the results to the same column/cell. Guessing there might be a better way but it gets me a clickable link.

xlWorkSheetFocus.get_Range("E6", ("E6" +
  xlWorkSheetFocus.UsedRange.Rows.Count)).TextToColumns(Type.Missing,
  Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
  Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, true,
  Type.Missing, Type.Missing, false, true, Type.Missing,
  "@", Type.Missing, Type.Missing, Type.Missing, Type.Missing);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文