我有一个 HTML 页面,其中包含一个 Google Charts 仪表板,我正在尝试使用 javascript 库将其导出到 .docx 文件。我目前尝试使用 Export2Doc 库:
<script>
function Export2Doc(element, filename = '') {
var preHtml =
"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
var postHtml = "</body></html>";
var html = preHtml + document.getElementById(element).innerHTML + postHtml;
var blob = new Blob(['\ufeff', html], {
type: 'application/msword'
});
// Specify link url
var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
// Specify file name
filename = filename ? filename + '.doc' : 'document.doc';
// Create download link element
var downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, filename);
} else {
// Create a link to the file
downloadLink.href = url;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
document.body.removeChild(downloadLink);
}
</script>
<button class="button" onclick="Export2Doc('exportContent', 'Monthly Report');">Word</button>
和导出的文件名exportContent
在哪里。<div>
Monthly Report
我还在导出之前将图表转换为图像。
var columnChartB = new google.visualization.ChartWrapper({
'chartType': 'ColumnChart',
'containerId': 'chart2',
'options': {
'width': 400,
'height': 400,
'allowHtml': true,
'titleTextStyle': {
color: 'grey', // any HTML string color ('red', '#cc00cc')
fontName: 'Roboto', // i.e. 'Times New Roman'
fontSize: '14', // 12, 18 (don't specify px)
bold: false, // true or false
},
vAxis: {
gridlines: {
count: 0
},
textPosition: 'none',
baselineColor: 'transparent'
},
title: 'Number of Projects',
legend: 'none',
colors: ['#FFD700']
},
'view': {
'columns': [1, 2, {
calc: "stringify",
sourceColumn: 2,
type: "string",
role: "annotation"
}]
}
});
// Chart Above as Image
google.visualization.events.addListener(columnChartB, 'ready', function () {
html2canvas($('#chart2').get(0)).then( function (canvas) {
var data = canvas.toDataURL('image/png', 0.9);
var src = encodeURI(data);
document.getElementById('chart2_image').src = src;
});
});
但是,仅显示图例和轴,而不显示条和/或列。(见下面的例子)
我究竟做错了什么?