Перенос строк и перенос текста
Contents
[
Hide
]
Чтобы гарантировать, что текст в ячейке может быть прочитан, можно применить явные разрывы строк и перенос текста. Перенос текста превращает одну строку в несколько в ячейке, а явные разрывы строк устанавливаются точно там, где вы их хотите.
Перенос текста в ячейке
Чтобы перенести текст в ячейке, используйте свойство Aspose.Cells.Style.setTextWrapped.
В следующем образце кода показано, как использовать перенос текста и явные переносы строк в ячейке.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(WrapTextinCell.class) + "TechnicalArticles/"; | |
// Create Workbook Object | |
Workbook wb = new Workbook(); | |
// Open first Worksheet in the workbook | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Get Worksheet Cells Collection | |
Cells cell = ws.getCells(); | |
// Increase the width of First Column Width | |
cell.setColumnWidth(0, 35); | |
// Increase the height of first row | |
cell.setRowHeight(0, 65); | |
// Add Text to the First Cell | |
cell.get(0, 0).setValue("I am using the latest version of Aspose.Cells to test this functionality"); | |
// Get Cell's Style | |
Style style = cell.get(0, 0).getStyle(); | |
// Set Text Wrap property to true | |
style.setTextWrapped(true); | |
// Set Cell's Style | |
cell.get(0, 0).setStyle(style); | |
// Save Excel File | |
wb.save(dataDir + "WrapTextinCell_out.xls"); |
Использование явных переносов строк
Вы можете использовать ‘\n’ в Java для вставки явных переносов строк в ячейку.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(UseExplicitLineBreaks.class); | |
// Create Workbook Object | |
Workbook wb = new Workbook(); | |
// Open first Worksheet in the workbook | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Get Worksheet Cells Collection | |
Cells cell = ws.getCells(); | |
// Increase the width of First Column Width | |
cell.setColumnWidth(0, 35); | |
// Increase the height of first row | |
cell.setRowHeight(0, 65); | |
// Add Text to the Firts Cell with Explicit Line Breaks | |
cell.get(0, 0).setValue("I am using \nthe latest version of \nAspose.Cells \nto test this functionality"); | |
// Get Cell's Style | |
Style style = cell.get(0, 0).getStyle(); | |
// Set Text Wrap property to true | |
style.setTextWrapped(true); | |
// Set Cell's Style | |
cell.get(0, 0).setStyle(style); | |
// Save Excel File | |
wb.save(dataDir + "WrappingText.xls"); |