更改单元格对齐方式并保持现有格式
Contents
[
Hide
]
可能的使用场景
有时,你想更改多个单元格的对齐方式,同时想保持现有格式。Aspose.Cells for JavaScript通过C++允许你使用StyleFlag.alignments(boolean)方法做到这一点。如果你将其设置为true,对齐的更改将生效,否则不生效。请注意,StyleFlag对象作为参数传递给实际应用格式的Range.applyStyle(Style, StyleFlag)方法。
更改单元格对齐方式并保留现有格式
以下示例代码加载了示例Excel文件(67338585.xlsx),创建范围并将其在水平和垂直方向上居中对齐,并保持现有格式不变。以下屏幕截图比较了示例Excel文件和输出Excel文件(67338586.xlsx),并显示了所有单元格的现有格式相同,只是单元格现在在水平和垂直方向上都居中对齐。

示例代码
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Change Cells Alignment and Keep Existing Formatting</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, StyleFlag, TextAlignmentType, 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');
const resultDiv = document.getElementById('result');
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();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Create cells range
const range = worksheet.cells.createRange("B2:D7");
// Create style object
const style = workbook.createStyle();
// Set the horizontal and vertical alignment to center using property assignments
style.horizontalAlignment = TextAlignmentType.Center;
style.verticalAlignment = TextAlignmentType.Center;
// Create style flag object
const flag = new StyleFlag();
flag.alignments = true; // Set style flag alignments true
// Apply style to range of cells
range.applyStyle(style, flag);
// Save the workbook in XLSX format and provide download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputChangeCellsAlignmentAndKeepExistingFormatting.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
resultDiv.innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>