gpt4 book ai didi

excel - Excel OpenXML 中的命名范围

转载 作者:行者123 更新时间:2023-12-02 02:15:54 25 4
gpt4 key购买 nike

我正在尝试使用 OpenXML 在 Excel 中创建命名范围。我可以在 DefinedNames 集合中添加 DefinedName,但这似乎没有任何作用。我注意到 ExtendedFileProperties 中的一个位置保存了范围名称,一个名为“TitlesOfParts”的结构。我尝试在其中添加一个条目,但这会导致 excel 抛出错误并且未创建命名范围。这是我正在使用的代码:

public void AddNamedRange(string pNamedRangeRef, string pNamedRangeName)
{
DefinedName _definedName = new DefinedName() { Name = pNamedRangeName, Text = pNamedRangeRef };
_workbook.Descendants<DocumentFormat.OpenXml.Spreadsheet.DefinedNames>().First().Append(_definedName);
DocumentFormat.OpenXml.VariantTypes.VTLPSTR _t = new DocumentFormat.OpenXml.VariantTypes.VTLPSTR() { Text = pNamedRangeName };
_spreadsheet.ExtendedFilePropertiesPart.Properties.TitlesOfParts.VTVector.Append(_t);
_spreadsheet.ExtendedFilePropertiesPart.Properties.TitlesOfParts.VTVector.Size++;
}

最佳答案

使用Open XML SDK 2.0 Productivity Tool for Microsoft Office,定义全局/工作簿范围的命名范围非常简单:

DefinedNames definedNamesCol = new DefinedNames();    //Create the collection
DefinedName definedName = new DefinedName()
{ Name = "test", Text="Sheet1!$B$2:$B$4" }; // Create a new range
definedNamesCol.Append(definedName); // Add it to the collection

workbook.Append(definedNamesCol); // Add collection to the workbook

关于excel - Excel OpenXML 中的命名范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7279383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com