如何使用 NPOI 更改 app.xml (XSSF) 中的应用程序?

How to change Application in app.xml (XSSF) with NPOI?

解压缩使用 NPOI 生成的 .xlsx 我注意到 NPOI 在 docProps/app.xml 中将自己设置为 "Application" 并且还将 "Generator" 添加到 docProps/custom .xml.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
    <Application>NPOI</Application>
    <AppVersion>123.456</AppVersion>
</Properties>

如何编辑 app.xml 中的申请信息?

我只找到 CorePropertiesCustomPropertiesExtendedProperties,但没有找到 "AppProperties"。

您需要从 ExtendedProperties 获取基础属性,然后设置它的 Application 属性。以下示例设置应用程序和应用程序版本:

static void Main(string[] args)
{
    XSSFWorkbook workbook = new XSSFWorkbook();
    ISheet sheet1 = workbook.CreateSheet("Sheet1");

    POIXMLProperties props = workbook.GetProperties();

    //get the underlying properties (of type NPOI.OpenXmlFormats.CT_ExtendedProperties)
    var underlyingProps = props.ExtendedProperties.GetUnderlyingProperties();
    //now set the properties (excuse the vanity!)
    underlyingProps.Application = "petelids";
    underlyingProps.AppVersion = "1.0";

    FileStream sw = File.Create("test.xlsx");
    workbook.Write(sw);
    sw.Close();
}

发电机属性

CustomProperties (custom.xml) 中更改 Generator 的示例。

CustomProperties customProperties = properies.CustomProperties;
customProperties.AddProperty("Generator", "petelids");
customProperties.AddProperty("Generator Version", "1.0");