Line Breaks and Text Wrapping
Contents
[
Hide
]
To ensure that text in a cell can be read, explicit line breaks and text wrapping can be applied. Text wrapping turns one line into several in a cell, which explicit line breaks put in breaks exactly where you want them.
To Wrap Text in a Cell
To wrap text in a cell, use the Aspose.Cells.Style.isTextWrapped(boolean) method.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Wrapping Text Example</title>
</head>
<body>
<h1>Wrapping Text 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 } = 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 === 0) {
// No file selected - create a new workbook
}
let wb;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
wb = new Workbook(new Uint8Array(arrayBuffer));
} else {
wb = new Workbook();
}
// Open first Worksheet in the workbook
const ws = wb.worksheets.get(0);
// Get Worksheet Cells Collection
const cell = ws.cells;
// Increase the width of First Column Width
cell.columns.get(0).width = 35;
// Increase the height of first row
cell.rows.get(0).height = 36;
// Add Text to the First Cell
const firstCell = cell.checkCell(0, 0);
firstCell.value = "I am using the latest version of Aspose.Cells to test this functionality";
// Make Cell's Text wrap
const style = firstCell.style;
style.isTextWrapped = true;
firstCell.style = style;
// Save Excel File
const outputData = wb.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'WrappingText.out.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>
To Use Explicit Line Breaks
You can use ‘\n’ in JavaScript to insert explicit line breaks in a cell.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Wrapping Text Example</title>
</head>
<body>
<h1>Wrapping Text 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, Worksheet, Cell, 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');
let wb;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
wb = new Workbook(new Uint8Array(arrayBuffer));
} else {
// No file selected - create a new workbook
wb = new Workbook();
}
// Open first Worksheet in the workbook
const ws = wb.worksheets.get(0);
// Get Worksheet Cells Collection
const cells = ws.cells;
// Increase the width of First Column Width
cells.columns.get(0).width = 35;
// Increase the height of first row
cells.rows.get(0).height = 65;
// Add Text to the First Cell with Explicit Line Breaks
const firstCell = cells.checkCell(0, 0);
firstCell.putValue("I am using\nthe latest version of \nAspose.Cells to \ntest this functionality");
// Make Cell's Text wrap
const style = firstCell.style;
style.isTextWrapped = true;
firstCell.style = style;
// Save Excel File
const outputData = wb.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'WrappingText.out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified 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>