Excel转HTML 使用PresentationPreference选项实现更好的布局,采用C++中的JavaScript
Contents
[
Hide
]
Aspose.Cells 提供了一个有用的 HtmlSaveOptions.presentationPreference 属性,供开发者在将 Microsoft Excel 文件保存为 HTML 或 MHT 格式时渲染更好的布局。该属性的默认值为 false。建议将其设置为 true,以获得更吸引人的 Excel 报表展示效果。
请查看下面的示例代码,演示如何根据展示偏好渲染Excel报告中的HTML文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Export Excel to HTML with Presentation Preference</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, HtmlSaveOptions, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate the Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Create HtmlSaveOptions object
const options = new HtmlSaveOptions();
// Set the Presentation preference option (converted from setPresentationPreference)
options.presentationPreference = true;
// Save the Excel file to HTML with specified option
const outputData = workbook.save(SaveFormat.Html, options);
const blob = new Blob([outputData], { type: 'text/html' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outPresentationlayout1.out.html';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download HTML File';
document.getElementById('result').innerHTML = '<p style="color: green;">HTML file created successfully! Click the download link to get the result.</p>';
});
</script>
</html>