简体   繁体   English

如何在 .NET C# 中使用 Open XML SDK 在 excel 中访问“表格设计”选项卡的“调整表格大小”

[英]How to access 'Resize Table' of 'Table Design' tab in excel with Open XML SDK in .NET C#

I'm trying to write to an existing excel template (.xlsx) and saving it as a new file using Open XML SDK in .NET C#.我正在尝试写入现有的 excel 模板 (.xlsx) 并使用 .NET C# 中的 Open XML SDK 将其保存为新文件。 I have a requirement to change the table design and resize the table programmatically.我需要更改表格设计并以编程方式调整表格大小。 I'm not able to access the 'Resize Table' property using Open XML.我无法使用 Open XML 访问“调整表大小”属性。 Manually we can access it by going to 'Table Design' tab and then selecting ' Resize Table'.手动我们可以通过转到“表格设计”选项卡然后选择“调整表格大小”来访问它。 Please refer to the below screenshot请参考下面的截图在此处输入图像描述

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks!谢谢!

  • First Get Your table By name ( if just one table you can get the first)首先按名称获取您的表(如果只有一个表,您可以获得第一个)
  • then Update the reference ( size of the table) and然后更新参考(表的大小)和
  • save the workSheet.保存工作表。

the table size refrenace is just a string ex.表大小参考只是一个字符串 ex。 A1:H5 A1:H5
you need to change the H5 to H15 to resize the table to add 10 rows more :)您需要将 H5 更改为 H15 以调整表格的大小以再添加 10 行 :)

Just Sample code只是示例代码

 var tableDefinitionPart = worksheetPart.TableDefinitionParts.Where(table=>table.Table.DisplayName== "Your table name").FirstOrDefault();
    // OR var tableDefinitionPart = worksheetPart.TableDefinitionParts.FirstOrDefault();
     if (tableDefinitionPart != null)
      {
         UpdateTableDefinitionPart(tableDefinitionPart, newTotalRows);
      }
            workSheet.Save();

public void UpdateTableDefinitionPart(TableDefinitionPart tableDefinitionPart, uint rowsCount)
 {
        var tableSize = tableDefinitionPart.Table.Reference;
        string newSize = tableSize.UpdateRowsTo(rowsCount);
        tableDefinitionPart.Table.Reference = newSize;
        tableDefinitionPart.Table.AutoFilter.Reference = newSize;
  }

*************************************************************************************
 public static string UpdateRowsTo(this StringValue tableReference, uint rows)
  {
            string result = tableReference.Value.Trim();
            var parts = result.Split(':');
            Regex regex = new Regex("[a-zA-Z]+");
            Match match = regex.Match(parts[1]);
            result = $"{parts[0]}:{match.Value}{rows}";
            return result;
   }

OR
***************************************************************************************
        public static string UpdateRowsTo2(this StringValue tableReference, uint rows)
        {
            string result = tableReference.Value.Trim();
            int index = result.Length - 1;
            while (!string.IsNullOrWhiteSpace(result) && Char.IsDigit(result[--index]))
            {
            }

            result = result.Substring(0, index + 1) + rows;
            return result;
        }

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

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