英文:
How to change document author of an excel file when exporting using jquery datatable
问题
Datatable 是一个允许我们格式化、搜索和排序 HTML 表格的库。它还允许您下载各种格式的文档。我以前使用过一些其他的 Excel 库,它们允许您更新 Excel 的元数据或一些其他属性。在我的情况下,我只是想看看在下载之前是否有一种方法可以更改 Excel 文档的作者:
我确实阅读了它们的按钮导出文档,但它并没有显示在任何地方更改作者的选项。我只是想知道是否有一种自定义此导出以添加该信息的方法。
英文:
Datatable is a library that allows us to format, search, sort HTML tables. It also allows you to download various format of documents. I have used some other excel libraries in the past which allows you to update excel metadata or some other properties. In my case I am just trying to see if there is a way to change the author of the excel document before downloading it:
I did read their button export documentation and it does not show an option to change the author anywhere. I was just wondering if there is a way to customize this export to add that information there.
答案1
得分: 2
要向导出的Excel文件中添加“作者”(以及其他相关信息),您需要对包含在Excel(.xlsx)文档中的底层XML文件进行一些低级编辑。
要访问底层.xlsx文档结构,您可以使用DataTables的customize
选项:
buttons: [
{
extend: 'excel',
title: '',
customize: function ( xlsx ) {
// 在这里进行编辑
}
}
]
您可以通过解压缩.xlsx文件自行查看.xlsx文档的结构。
“作者”信息存储在名为“core.xml”的文件中,位于名为“docProps”的目录中。默认情况下,DataTables生成的Excel文件不包括这个文件(或其所在的目录),因此我们需要创建它们:
const coreXmlStr =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<dc:creator>John Smith;Jane Jones</dc:creator>' +
'<cp:lastModifiedBy>John Smith</cp:lastModifiedBy>' +
'<dcterms:created xsi:type="dcterms:W3CDTF">2023-06-14T17:51:33Z</dcterms:created>' +
'<dcterms:modified xsi:type="dcterms:W3CDTF">2023-06-14T17:53:15Z</dcterms:modified>' +
'</cp:coreProperties>';
const parser = new window.DOMParser();
const coreXmlDoc = parser.parseFromString( coreXmlStr, 'text/xml' );
xlsx.docProps = {};
xlsx.docProps['core.xml'] = coreXmlDoc;
coreXmlStr
是一个包含各种信息字段的XML字符串。您可以在其中放入您想要的任何值。我的示例中有两个作者:
John Smith;Jane Jones
JavaScript将此XML字符串解析为实际的XML文档,然后将其添加到docProps
目录中的新core.xml
文件中。xlsx
对象表示包含的Excel文件。
我们还需要设置一些额外的关系元数据,以便Excel实际使用这些信息:
var contentTypes = xlsx['[Content_Types].xml'];
$('Types', contentTypes).append('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
var rels = xlsx._rels['.rels'];
$('Relationships', rels).append('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
总体的代码如下:
$(document).ready(function() {
$('#myTable').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'excel',
title: '',
customize: function ( xlsx ) {
const coreXmlStr =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<dc:creator>John Smith;Jane Jones</dc:creator>' +
'<cp:lastModifiedBy>John Smith</cp:lastModifiedBy>' +
'<dcterms:created xsi:type="dcterms:W3CDTF">2023-06-14T17:51:33Z</dcterms:created>' +
'<dcterms:modified xsi:type="dcterms:W3CDTF">2023-06-14T17:53:15Z</dcterms:modified>' +
'</cp:coreProperties>';
const parser = new window.DOMParser();
const coreXmlDoc = parser.parseFromString( coreXmlStr, 'text/xml' );
xlsx.docProps = {};
xlsx.docProps['core.xml'] = coreXmlDoc;
var contentTypes = xlsx['[Content_Types].xml'];
$('Types', contentTypes).append('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
var rels = xlsx._rels['.rels'];
$('Relationships', rels).append('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
}
}
]
} );
});
在导出的Excel文件中,您将看到如下内容:
英文:
To add "Author" (and other related information) to your exported Excel file, you need to perform some low-level edits on the underlying XML files contained in your Excel (.xlsx
) document.
To access the underlying .xlsx
document structure, you use this DataTables customize
option:
buttons: [
{
extend: 'excel',
title: '',
customize: function ( xlsx ) {
...
}
You can see for yourself what the structure of a .xlsx
document is by unzipping it.
The Author
information is stored in a file named core.xml
in a directory named docProps
. By default, a DataTables Excel file does not include this file (or its containing directory), so we need to create them:
const coreXmlStr =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<dc:creator>John Smith;Jane Jones</dc:creator>' +
'<cp:lastModifiedBy>John Smith</cp:lastModifiedBy>' +
'<dcterms:created xsi:type="dcterms:W3CDTF">2023-06-14T17:51:33Z</dcterms:created>' +
'<dcterms:modified xsi:type="dcterms:W3CDTF">2023-06-14T17:53:15Z</dcterms:modified>' +
'</cp:coreProperties>';
const parser = new window.DOMParser();
const coreXmlDoc = parser.parseFromString( coreXmlStr, 'text/xml' );
xlsx.docProps = {};
xlsx.docProps['core.xml'] = coreXmlDoc;
The coreXmlStr
is a string of XML containing various information fields. You can put whatever values you want in these. My example has two authors:
John Smith;Jane Jones
The JavaScript parses this string of XML into an actual XML document, and then adds it to a new core.xml
file in a docProps
directory. The xlsx
object represents the containing Excel file.
We also need to set up some additional relationship metadata so Excel actually uses this information:
var contentTypes = xlsx['[Content_Types].xml'];
$( 'Types', contentTypes ).append( '<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>' );
var rels = xlsx._rels['.rels'];
$( 'Relationships', rels ).append( '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>' );
The overall code:
$(document).ready(function() {
$('#myTable').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'excel',
title: '',
customize: function ( xlsx ) {
const coreXmlStr =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<dc:creator>John Smith;Jane Jones</dc:creator>' +
'<cp:lastModifiedBy>John Smith</cp:lastModifiedBy>' +
'<dcterms:created xsi:type="dcterms:W3CDTF">2023-06-14T17:51:33Z</dcterms:created>' +
'<dcterms:modified xsi:type="dcterms:W3CDTF">2023-06-14T17:53:15Z</dcterms:modified>' +
'</cp:coreProperties>';
const parser = new window.DOMParser();
const coreXmlDoc = parser.parseFromString( coreXmlStr, 'text/xml' );
xlsx.docProps = {};
xlsx.docProps['core.xml'] = coreXmlDoc;
var contentTypes = xlsx['[Content_Types].xml'];
$( 'Types', contentTypes ).append( '<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>' );
var rels = xlsx._rels['.rels'];
$( 'Relationships', rels ).append( '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>' );
}
}
]
} );
});
In the exported Excel file, we see this:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论