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

时间:2018-10-25 11:18:51

标签: c# openxml 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”。

1 个答案:

答案 0 :(得分:1)

您需要从ExtendedProperties获取基础属性,然后设置其Application属性。以下示例设置了Application和AppVersion:

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();
}

发电机属性

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

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