防止在使用 JavaScript 通过 C++ 将隐藏工作表内容导出为 HTML 时被导出
Contents
[
Hide
]
您可以将 Excel 工作簿保存为 HTML。但是,如果工作簿包含隐藏的工作表,则 Aspose.Cells 默认情况下会将隐藏的工作表内容导出到 HTML 输出的 (_files) 目录中,该目录包含诸如工作表、图像、tabstrip.htm、stylesheet.css 等文件。有时,以这种方式导出隐藏的工作表内容是不合适的。例如,如果隐藏的工作表包含不应导出到 _files 目录的图像。
Aspose.Cells for JavaScript 通过 C++ 提供了 HtmlSaveOptions.exportHiddenWorksheet 属性。默认情况下,其设置为 true,隐藏的工作表内容会导出为 HTML。如果将其设置为 false,Aspose.Cells 将不会导出隐藏工作表内容。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - HTML Without Hidden Content</title>
</head>
<body>
<h1>Save Workbook as HTML Without Hidden Worksheet Content</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 Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Create HtmlSaveOptions and do not export hidden worksheet contents
const options = new HtmlSaveOptions();
options.exportHiddenWorksheet = false;
// Save workbook to HTML format using the options
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 = 'HtmlWithoutHiddenContent_out.html';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download HTML File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook saved to HTML without hidden content. Click the download link to get the file.</p>';
});
</script>
</html>