如何克服自定义文档属性大小限制 [英] How to overcome custom document property size limit

查看:96
本文介绍了如何克服自定义文档属性大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将长字符串(特定于文档)保存到Excel文档中. 由于Office.Core.CustomDocumentProperty.value的长度限制仅为255个字符,因此请提供有关如何克服此限制的建议,或建议其他将数据存储在Excel文档中的方法.

I need to save long (,document specific) string to an Excel document. Since the length limit for Office.Core.CustomDocumentProperty.value is only 255 char, please advise on how to overcome this limit, or suggesting other ways to store data in an Excel document.

(据我回忆,一个单元格公式只能存储255个字符,因此这不是可行的解决方案.)

(To my recollection, a cell formula can only store 255 char, so this is not a workable solution.)

推荐答案

只需将您的值拆分为多个属性.这样的事情会起作用.

Just split your value into multiple properties. Something like this would work.

private static void WriteCustomDocumentProperty(Workbook workbook, string name, string value)
{
    dynamic customDocumentProperties = workbook.CustomDocumentProperties;
    var numParts = value.Length/255 + (value.Length%255 != 0 ? 1 : 0);
    for (var i = 0; i < numParts; ++i)
    {
        var part = value.Substring(i*255, Math.Min(255, value.Length - i*255));
        customDocumentProperties.Add(name + "." + i, false, MsoDocProperties.msoPropertyTypeString, part);
    }
    customDocumentProperties.Add(name + ".Count", false, MsoDocProperties.msoPropertyTypeNumber, numParts);
}

private static string ReadCustomDocumentProperty(Workbook workbook, string name)
{
    dynamic customDocumentProperties = workbook.CustomDocumentProperties;
    var numParts = Convert.ToInt32(customDocumentProperties[name + ".Count"].Value);
    var value = new StringBuilder();
    for (var i = 0; i < numParts; ++i)
        value.Append(customDocumentProperties[name + "." + i].Value);
    return value.ToString();
}

根据字符串的大小,这可能会很慢.更好的选择可能是使用自定义XML部件". (我强烈建议将命名空间"urn:custom-storage:XXX"更改为唯一且专有的名称,以免运行其他使用相同技术编写的软件.)

Depending on the size of your strings, this may be very slow. A better option might be to use Custom XML Parts. (I highly recommend changing the namespace "urn:custom-storage:XXX" to something unique and proprietary, lest you run afoul of another software written using this same technique.)

private static void WriteCustomDocumentProperty(Workbook workbook, string name, string value)
{
    var ns = "urn:custom-storage:" + name;
    var document = new XDocument(new XElement(XName.Get("custom-storage", ns), value));
    var xmlValue = document.ToString();
    workbook.CustomXMLParts.Add(xmlValue);
}

private static string ReadCustomDocumentProperty(Workbook workbook, string name)
{
    var ns = "urn:custom-storage:" + name;
    var parts = workbook.CustomXMLParts.SelectByNamespace(ns);
    switch (parts.Count)
    {
        case 0:
            return null;
        case 1:
            return XDocument.Parse(parts[1].XML).Root.Value;
        default:
            throw new ApplicationException("Duplicate part in workbook.");
    }
}

这篇关于如何克服自定义文档属性大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆