通过C++中的JavaScript导入HTML时删除行结束后的多余空格
Contents
[
Hide
]
请使用HtmlLoadOptions.deleteRedundantSpaces属性,并将其设置为true,以删除换行标签后的所有多余空格。默认情况下,此属性为false,在输出Excel文件时会保留多余空格。
设置 HTMLLoadOptions.deleteRedundantSpaces 属性为 false 和 true 的效果
以下截图显示了将此属性设置为false和true的效果。

在导入HTML时删除换行后的多余空格
###编程示例
以下示例代码展示了 HtmlLoadOptions.deleteRedundantSpaces 属性的用法。请将其设置为 true 或 false,以获得上方截图所示的输出。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Delete Redundant Spaces While Importing From HTML</title>
</head>
<body>
<h1>Delete Redundant Spaces While Importing From HTML</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, HtmlLoadOptions, LoadFormat } = 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 () => {
// Sample Html containing redundant spaces after <br> tag
const html = "<html> <body> <table> <tr> <td> <br> This is sample data <br> This is sample data<br> This is sample data</td> </tr> </table> </body> </html>";
// Convert Html to byte array
const encoder = new TextEncoder();
const byteArray = encoder.encode(html);
// Set Html load options and keep precision true
const loadOptions = new HtmlLoadOptions(LoadFormat.Html);
loadOptions.deleteRedundantSpaces = true;
// Create workbook from stream with Html load options
const stream = byteArray;
const workbook = new Workbook(stream, loadOptions);
// Access first worksheet
const sheet = workbook.worksheets.get(0);
// Auto fit the sheet columns
sheet.autoFitColumns();
// Saving the workbook and provide download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputDeleteRedundantSpacesWhileImportingFromHtml.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>