首页 > 解决方案 > 如何修改 C# OpenXML 生成的 Excel [Content_Types].xml 文件

问题描述

我正在使用OpenXML SDK库在 C# 中生成 Excel 文件。我注意到生成的内容文件 ([Content_Types].xml) 与 Excel 在保存文件时生成的内容不同。

由于某种原因,我的 OpenXML 生成的文件缺少

<Default ContentType="application/xml" Extension="xml"/>

元素并放置

<Default Extension="xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />

而是元素。

OpenXML 生成的文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
<Default Extension="xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />
<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml" />
<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml" />
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
</Types>

我想实现以下目标:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="bin" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings"/>
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
</Types>

这也是一个缺失的元素,我想添加

<Default Extension="bin" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings"/>

有没有使用 OpenXML 库访问内容类型的方法?我想避免手动解包 xlsx 文件并修改 [Content_Types].xml 的源的方法

标签: c#excelopenxmlcontent-typeopenxml-sdk

解决方案


最简单的解决方案是使用具有所需内容类型的适当 Excel 工作簿作为起点。

使用 Open XML SDK,您可以从 Excel 模板 (.xslt) 创建工作簿或克隆现有的 Excel 工作簿(.xlsx、.xlsm)。Clone()您可以通过在现有SpreadsheetDocument对象上调用方法或将数据读入 a MemoryStream,打开该SpreadsheetDocumentMemoryStream然后在最后将其保存到不同的文件(或数据库或其他任何文件)来克隆工作簿。

以下示例从模板(或工作簿)创建 Excel 工作簿:

SpreadsheetDocument doc = SpreadsheetDocument.CreateFromTemplate("Template.xlst");

如果您查看该CreateFromTemplate()方法是如何实现的,您会发现它总是克隆工作簿或模板。对于模板,它会更改文档类型(使用ChangeDocumentType()方法),以便您始终获得工作簿而不是模板。因此,如果您的入门模板或工作簿存储在文件系统中,这将是最直接的方法。

根据“模板”的存储方式和位置,还有其他方法可以实现相同的结果。但是,您通常会使用模板或工作簿数据生成一个Stream(例如,a MemoryStream),然后打开SpreadsheetDocumentStream.


推荐阅读