在输出的HTML中单独导出工作表CSS,使用JavaScript via C++。
Contents
[
Hide
]
可能的使用场景
Aspose.Cells for JavaScript 通过 C++ 提供在将 Excel 文件转换为 HTML 时单独导出工作表 CSS 的功能。请使用 HtmlSaveOptions.exportWorksheetCSSSeparately 属性实现此目的,并在将 Excel 文件保存为 HTML 格式时将其设置为 true。
在输出 HTML 中单独导出工作表 CSS
以下示例代码创建了一个Excel文件,在单元格B5中添加了红色文字,然后使用HtmlSaveOptions.exportWorksheetCSSSeparately属性将其保存为HTML格式。请参考代码生成的输出HTML。里面包含了stylesheet.css作为示例代码的输出结果。
示例代码
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Export Worksheet CSS Separately Example</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, Color, 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 () => {
// Create a new workbook
const wb = new Workbook();
// Access first worksheet
const ws = wb.worksheets.get(0);
// Access cell B5 and put value inside it
const cell = ws.cells.get("B5");
cell.value = "This is some text.";
// Set the style of the cell - font color is Red
const st = cell.style;
st.font.color = Color.Red;
cell.style = st;
// Specify html save options - export worksheet css separately
const opts = new HtmlSaveOptions();
opts.exportWorksheetCSSSeparately = true;
// Save the workbook in html
const outputData = wb.save(SaveFormat.Html, opts);
const blob = new Blob([outputData], { type: 'text/html' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputExportWorksheetCSSSeparately.html';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download HTML File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook saved as HTML. Click the download link to get the file.</p>';
});
</script>
</html>
将单个工作簿表导出到 HTML
当使用 Aspose.Cells for JavaScript 通过 C++ 将包含多个工作表的工作簿转换为 HTML 时,会创建一个 HTML 文件及包含 CSS 和多个 HTML 文件的文件夹。当在浏览器中打开此 HTML 文件时,标签是可见的。对于单个工作表的工作簿,也需要实现相同的行为。当转换为 HTML 时,早期不会为单工作表工作簿创建单独的文件夹,只会创建一个 HTML 文件。此类 HTML 文件在浏览器中打开时不会显示标签。MS Excel 也会为单工作表创建适当的文件夹和 HTML,因此使用 Aspose.Cells API 实现了同样的行为。可以从以下链接下载示例文件,用于下面的示例代码:
示例代码
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Save Workbook as HTML Example</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, EncodingType, 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 resultDiv = document.getElementById('result');
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by loading the selected file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Specify HTML save options
const options = new HtmlSaveOptions();
// Set optional settings (converted from setters to properties)
options.encoding = EncodingType.UTF8;
options.exportImagesAsBase64 = true;
options.exportGridLines = true;
options.exportSimilarBorderStyle = true;
options.exportBogusRowData = true;
options.excludeUnusedStyles = true;
options.exportHiddenWorksheet = true;
// Save the workbook in HTML format with specified HtmlSaveOptions
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 = 'outputSampleSingleSheet.htm';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download HTML File';
resultDiv.innerHTML = '<p style="color: green;">Workbook saved to HTML. Click the download link to get the file.</p>';
});
</script>
</html>