对齐设置
配置对齐设置
Microsoft Excel中的对齐设置
任何使用Microsoft Excel格式化单元格的人都会熟悉Microsoft Excel中的对齐设置。
从上面的图中可以看出,有不同种类的对齐选项:
- 文本对齐(水平和垂直)
- 缩进
- 方向
- 文本控制
- 文本方向
Aspose.Cells完全支持所有这些对齐设置,并在下面详细讨论。
Aspose.Cells中的对齐设置
Aspose.Cells 提供了一个类 Workbook,代表一个 Excel 文件。Workbook 类包含一个 Worksheets 集合,可以访问 Excel 文件中的每一个工作表。一个工作表由 Worksheet 类表示。Worksheet 类提供了一个 getCells() 集合。Cells 集合中的每个项目代表一个 Cell 类的对象。
Aspose.Cells 提供 getStyle 和 setStyle 方法,用于 Cell 类中获取和设置单元格的格式。Style 类提供了有用的属性,用于配置对齐设置。
使用 TextAlignmentType 枚举选择任何文本对齐类型。TextAlignmentType 枚举中预定义的文本对齐类型包括:
文本对齐类型 | 描述 |
---|---|
Bottom | 表示底部文本对齐 |
Center | 表示居中文本对齐 |
CenterAcross | 表示跨列居中文本对齐 |
Distributed | 表示分散文本对齐 |
Fill | 表示填充文本对齐 |
General | 表示常规文本对齐 |
Justify | 表示两端对齐文本对齐 |
Left | 表示左对齐文本对齐 |
Right | 表示右对齐文本对齐 |
Top | 表示顶部文本对齐 |
JustifiedLow | 用于阿拉伯文本具有调整的卡什达长度。 |
ThaiDistributed | 分布泰文,因为每个字符被视为一个单词。 |
水平对齐
使用 Style 对象的 setHorizontalAlignment 方法水平对齐文本。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
const style = cell.getStyle(); | |
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
垂直对齐
与水平对齐类似,使用 Style 对象的 setVerticalAlignment 方法垂直对齐文本。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Clearing all the worksheets | |
workbook.getWorksheets().clear(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
const style = cell.getStyle(); | |
// Setting the vertical alignment of the text in a cell | |
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
缩进
可以通过 Style 对象的 setIndentLevel 方法设置单元格中文本的缩进级别。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
const style = cell.getStyle(); | |
// Setting the indentation level of the text (inside the cell) to 2 | |
style.setIndentLevel(2); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
方向
通过 Style 对象的 setRotationAngle 方法设置单元格中文本的方向(旋转)。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
const style = cell.getStyle(); | |
// Setting the rotation of the text (inside the cell) to 25 | |
style.setRotationAngle(25); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
文本控制
以下部分讨论了如何通过设置文本换行、缩小以适应和其他格式设置选项来控制文本。
自动换行文本
在单元格中换行文本可以更容易阅读:单元格的高度会调整以适应全部文本,而不是裁剪或溢出到相邻的单元格。通过 Style 对象的 setIsTextWrapped(boolean) 方法开启或关闭文本换行。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create Workbook Object | |
const wb = new AsposeCells.Workbook(); | |
// Open first Worksheet in the workbook | |
const ws = wb.getWorksheets().get(0); | |
// Get Worksheet Cells Collection | |
const cell = ws.getCells(); | |
// Increase the width of First Column Width | |
cell.setColumnWidth(0, 35); | |
// Increase the height of first row | |
cell.setRowHeight(0, 36); | |
// Add Text to the First Cell | |
cell.checkCell(0, 0).putValue("I am using the latest version of Aspose.Cells to test this functionality"); | |
// Make Cell's Text wrap | |
const style = cell.checkCell(0, 0).getStyle(); | |
style.setIsTextWrapped(true); | |
cell.checkCell(0, 0).setStyle(style); | |
// Save Excel File | |
wb.save(path.join(dataDir, "WrappingText.out.xlsx")); |
缩小以适应
一种字段中换行文本的选项是缩小文本大小以适应单元格尺寸。这通过将 Style 对象的 setShrinkToFit(boolean) 方法设置为 true 实现。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
const style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setShrinkToFit(true); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
合并单元格
像 Microsoft Excel 一样,Aspose.Cells 支持将多个单元格合并为一个。Aspose.Cells 提供两种方法,一种是调用 Cells 集合的 merge 方法。merge 方法需要以下参数来合并单元格:
- 第一行:开始合并的第一行。
- 第一列:开始合并的第一列。
- 行数:要合并的行数。
- 列数:要合并的列数。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Create a Workbook. | |
const wbk = new AsposeCells.Workbook(); | |
// Create a Worksheet and get the first sheet. | |
const worksheet = wbk.getWorksheets().get(0); | |
// Create a Cells object to fetch all the cells. | |
const cells = worksheet.getCells(); | |
// Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.merge(5, 2, 2, 3); | |
// Input data into C6 Cell. | |
cells.get(5, 2).putValue("This is my value"); | |
// Create a Style object to fetch the Style of C6 Cell. | |
const style = cells.get(5, 2).getStyle(); | |
// Create a Font object | |
const font = style.getFont(); | |
// Set the name. | |
font.setName("Times New Roman"); | |
// Set the font size. | |
font.setSize(18); | |
// Set the font color | |
font.setColor(AsposeCells.Color.Blue); | |
// Bold the text | |
font.setIsBold(true); | |
// Make it italic | |
font.setIsItalic(true); | |
// Set the background color of C6 Cell to Red | |
style.setForegroundColor(AsposeCells.Color.Red); | |
style.setPattern(AsposeCells.BackgroundType.Solid); | |
// Apply the Style to C6 Cell. | |
cells.get(5, 2).setStyle(style); | |
// Save the Workbook. | |
wbk.save(path.join(dataDir, "mergingcells.out.xls")); |
另一种方法是首先调用 Cells 集合的 createRange 方法创建要合并的单元格范围。createRange 方法接受与上面讨论的 merge 方法相同的参数集,并返回一个 Range 对象。Range 对象还提供一个 merge 方法,将指定范围中的单元格范围合并。
文本方向
可以设置单元格中文本的阅读顺序。阅读顺序是以字符、单词等显示的视觉顺序。例如,英语是从左到右的语言,而阿拉伯语是从右到左的语言。
读取顺序由 Style 对象的 TextDirection 属性设置。Aspose.Cells 在 TextDirectionType 枚举中预定义了文本方向类型。
文本方向类型 | 描述 |
---|---|
Context | 与第一个输入字符的语言一致的阅读顺序 |
LeftToRight | 从左到右的阅读顺序 |
RightToLeft | 从右到左的阅读顺序 |
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Loads the workbook which contains hidden external links | |
const workbook = new AsposeCells.Workbook(filePath); | |
// Instantiating a Workbook object | |
// Obtaining the reference of first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style in the "A1" cell | |
const style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setTextDirection(AsposeCells.TextDirectionType.LeftToRight); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |