Set Excel Named Ranges via C#?

空扰寡人 提交于 2019-12-20 04:25:14

问题


I'm trying to replicate this Access VBA code using C#, but am unable to do so. Wondering if anyone else has tried this before and can help.

oWB.Worksheets("Signoff").Range("rgSignOffRecTemplate").Value = g_TemplatePath & "Signoff_Rec.XLT"

rgSignOffRecTemplate is a "Defined Name" in the Excel template that I'm trying to write to.

Many thanks for your help.


回答1:


    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
        Excel.Name oName;
        Excel.Range oRange;

        //'using name
        oName = ExcelWorkbook1.Globals.ThisWorkbook.Names.Item("rgSignOffRecTemplate", missing, missing);
        oName.RefersToRange.Value2 = "here";

        //'using range
        oName = this.Names.Item("rgSignOffRecTemplate", missing, missing);
        oRange = oName.RefersToRange;
        oRange.Value2 = "here i am";

        //'direct access
        this.Names.Item("rgSignOffRecTemplate", missing, missing).RefersToRange.Value2 = "here i am again";

        DisplayWorkbookNames();

    }

    private void DisplayWorkbookNames() {
        for (int i = 1; i <= this.Names.Count - 1; i++) {
            Globals.Sheet1.Range["A" + i.ToString(), missing].Value2 = this.Names.Item(i, missing, missing);
        }
    }


来源:https://stackoverflow.com/questions/2416727/set-excel-named-ranges-via-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!