excel
excel
excel模块用于操作Office Excel文档,基于 ECMA-376,ISO/IEC 29500 国际标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的电子表格文档。支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多种文档格式
excel
属性与方法
名称 | 类型 | 参数 | 返回值 | 说明 |
---|---|---|---|---|
createFile | 方法 | options?:object | ExcelFile | 新建一个excel文档 |
openFile | 方法 | filePath:string,options?:object | ExcelFile | 打开已有的excel文档 |
createFile
新建一个excel文档,并默认包含名为Sheet1
的工作表 语法
createFile(options?:object)
参数
- options: excel文件的相关配置,具体配置项如下:
{
password:"123456", //设置文件的访问密码
}
示例
import excel from "excel"
let excelFile1 = excel.createFile()
//创建带有访问密码的文件
let excelFile2 = excel.createFile({password:"123456"})
openFile
打开已有的excel文档 语法
openFile(filePath:string,options?:object)
参数
- filePath: 文件路径
- options: excel文件的相关配置,具体配置项如下:
{
password:"123456", //文件的访问密码
}
示例
import excel from "excel"
let excelFile1 = excel.openFile("./test1.xlsx")
//如果test2.xlsx有访问密码
let excelFile2 = excel.openFile("./test2.xlsx",{password:"123456"})
ExcelFile
Excel对象表示一个excel文件。
属性与方法
名称 | 类型 | 参数 | 返回值 | 说明 |
---|---|---|---|---|
newSheet | 方法 | sheetName:string | int | 新建sheet页 |
newStyle | 方法 | style:object | int | 创建样式对象 |
setSheetName | 方法 | oldName:string, newName:string | 修改sheet页名称 | |
setSheetRow | 方法 | sheetName:string,cell:string,rows:any[] | 设置行数据 | |
getSheetList | 方法 | string[] | 获取sheet名列表 | |
getSheetIndex | 方法 | name:string | int | 获取sheet名对应的索引 |
getSheetName | 方法 | index:int | string | 获取sheet索引对应的名称 |
setActiveSheet | 方法 | index:int | 设置某一个sheet索引为活动工作表 | |
setCellValue | 方法 | sheetName:string, cell:string, value:any | 设置单元格内容 | |
setCellFormula | 方法 | sheetName:string, cell:string, formula:string | 单元格中添加公式 | |
setColWidth | 方法 | sheetName:string,startCol:string, endCol:string,width:int | 设置列宽 | |
setRowHeight | 方法 | sheetName:string,row:int,height:int | 设置行高 | |
setCellStyle | 方法 | sheetName:string,topLeftCell:string, bottomRightCell:string,styleIndex:int | 设置单元格样式 | |
setColStyle | 方法 | sheetName:string,col:string,styleIndex:int | 设置列样式 | |
setRowStyle | 方法 | sheetName:string,startRow:int, endRow:int,styleIndex:int | 设置行样式 | |
deleteSheet | 方法 | sheetName:string | 删除sheet页 | |
getCellValue | 方法 | sheetName:string, cell:string | string | 获取单元格内容 |
getRows | 方法 | sheetName:string | any[] | 获取一个sheet页中所有单元格内容 |
mergeCell | 方法 | sheetName:string,topLeftCell:string,bottomRightCell:string | 合并单元格 | |
write | 方法 | writer:Writer | 将文件内容写入到一个Writer中 | |
save | 方法 | filePath:string | 保存文件 | |
close | 方法 | 关闭文件 | ||
getBuffer | 方法 | buffer | 获取文件的buffer对象(字节数组) |
newSheet
新建工作表,如果工作表名称不存在,则会创建并返回该索引;如果工作表名称已存在,则直接返回索引
语法
newSheet(sheetName:string)
参数
- sheetName: 工作表名称
返回值
- int : 工作表的索引值
import excel from "excel"
let file = excel.createFile()
console.log(file.newSheet("Sheet2"))//打印1,因为默认已经存在Sheet1
newStyle
新建样式对象
语法
newStyle(style:object)
参数
- style: 样式设置对象,详细设置请查阅
Style
返回值
- int : 样式对象的索引值,用于在
setCellStyle
函数中使用
import excel from "excel"
let file = excel.createFile()
const style = {
border: [
{ type: "left", color: "#000000", style: 1 },
{ type: "top", color: "#000000", style: 1 },
{ type: "bottom", color: "#000000", style: 1 },
{ type: "right", color: "#000000", style: 1 }
],
fill: {
type: "pattern",
pattern: 1,
color: ["#FFFF00"],
shading: 0
},
font: {
bold: true,
italic: true,
underline: "",
family: "Arial",
size: 12,
strike: false,
color: "#FF0000",
colorTint: 0,
vertAlign: ""
},
alignment: {
horizontal: "center",
indent: 0,
justifyLastLine: false,
readingOrder: 0,
relativeIndent: 0,
shrinkToFit: false,
textRotation: 0,
vertical: "center",
wrapText: false
},
protection: {
hidden: false,
locked: true
},
numFmt: 14,
decimalPlaces: 2,
customNumFmt: "\"¥\"#,##0.00",
negRed: true
};
//返回样式对象的索引值
let styleIndex = file.newStyle(style)
setSheetName
修改sheet名称
语法
setSheetName(oldName:string, newName:string)
参数
- oldName: 原工作表名称
- newName: 修改后的工作表名称
示例
import excel from "excel"
let file = excel.createFile()
file.setSheetName("Sheet1", "新sheet名称")
setSheetRow
设置行数据
语法
setSheetRow(sheetName:string,cell:string,rows:any[])
参数
- sheetName: 工作表名称
- cell: 行的起始单元格,例如 A1
- rows: 一个包含行数据的数组
示例
import excel from "excel"
let file = excel.createFile()
file.setSheetRow("Sheet1","A1",["姓名","年龄"])
for (let i = 2; i < 10; i++) {
file.setSheetRow("Sheet1","A"+i,["人员",12])
}
file.save("./excel.xlsx")
getSheetList
获取sheet名列表
语法
getSheetList():string[]
返回值
- string[]: 包含所有工作表名称的数组
示例
import excel from "excel"
let file = excel.createFile()
console.log(file.getSheetList()) //打印:[ 'Sheet1' ]
getSheetIndex
获取某个工作表的索引
语法
getSheetIndex(name:string):int
参数
- name: 工作表名称
返回值
- int: 工作表名称对应的索引值,如果名称不存在,返回
-1
示例
import excel from "excel"
let file = excel.createFile()
console.log(file.getSheetIndex("Sheet1")) //打印:0
console.log(file.getSheetIndex("Sheet2")) //打印:-1
getSheetName
获取某个工作表的名称
语法
getSheetName(index:int):string
参数
- index: 工作表索引值
返回值
- string: 工作表名称,如果索引不存在,返回空字符串
示例
import excel from "excel"
let file = excel.createFile()
console.log(file.getSheetName(0)) //打印:Sheet1
console.log(file.getSheetName(1)) //打印:
setActiveSheet
设置某个工作表为活动工作表,活动工作表是指默认打开的工作表
语法
setActiveSheet(index:int)
参数
- index: 工作表索引值
示例
import excel from "excel"
let file = excel.createFile()
file.newSheet("Sheet2")
console.log(file.setActiveSheet(1))//设置第二个工作表为活动工作表
setCellValue
设置单元格内容
语法
setCellValue(sheetName:string, cell:string, value:any)
参数
- sheetName:工作表名称
- cell:单元格名称,格式为:列名+行索引,如:A1(第一行第一列)
- value:单元格内容
示例
import excel from "excel"
let file = excel.createFile()
file.setCellValue("Sheet1", "A1", "测试1")
file.setCellValue("Sheet1", "A2", 1)
file.setCellValue("Sheet1", "A3", true)
file.setCellValue("Sheet1", "B1", 1.21)
file.setCellValue("Sheet1", "B2", null)
file.setCellValue("Sheet1", "B3", 323n)
setCellFormula
单元格中添加公式
语法
setCellFormula(sheetName:string, cell:string, formula:string)
参数
- sheetName:工作表名称
- cell:单元格名称,格式为:列名+行索引,如:A1(第一行第一列)
- formula: 单元格公式
示例
import excel from "excel"
let file = excel.createFile()
file.setSheetRow("Sheet1","A1",["姓名","年龄"])
for (let i = 2; i < 10; i++) {
file.setSheetRow("Sheet1","A"+i,["人员",12])
}
//计算B1:B9的和
file.setCellFormula("Sheet1","B10","sum(B1:B9)")
file.save("./excel.xlsx")
setColWidth
设置列宽
语法
setColWidth(sheetName:string,startCol:string,endCol:string,width:int)
参数
- sheetName: 工作表名称
- startCol: 起始列名。例如,"A" 表示第一列
- endCol: 截止列名。例如,"B" 表示第二列
- width: 列宽
示例
import excel from "excel"
let file = excel.createFile()
//将第1,2列的宽度都设置为50
file.setColWidth("Sheet1","A","B",50)
setRowHeight
设置行高
语法
setRowHeight(sheetName:string,row:int,height:int)
参数
- sheetName: 工作表名称
- row: 行号,例如:1
- height: 行高
示例
import excel from "excel"
let file = excel.createFile()
//将第1行的高度设置为30
file.setRowHeight("Sheet1",1,30)
setCellStyle
设置单元格样式
语法
setCellStyle(sheetName:string,topLeftCell:string,bottomRightCell:string,styleIndex:int)
参数
- sheetName: 工作表名称
- topLeftCell: 表示单元格范围的左上角单元格。例如,"A1" 表示第一列第一行
- bottomRightCell: 表示单元格范围的右下角单元格。例如,"C3" 表示第三列第三行。
- styleIndex: 样式索引
示例
import excel from "excel"
let file = excel.createFile()
file.setCellValue("Sheet1", "A1", "测试1")
file.setCellValue("Sheet1", "A2", 1)
file.setCellValue("Sheet1", "A3", true)
file.setCellValue("Sheet1", "B1", 1.21)
//设置单元格样式,styleIndex由newStyle函数创建并返回
file.setCellStyle("Sheet1", "B1", "B1",styleIndex)
setColStyle
设置列样式
语法
setColStyle(sheetName:string,col:string,styleIndex:int)
参数
- sheetName: 工作表名称
- col: 列名。例如,"A" 表示第一列
- styleIndex: 样式索引
示例
import excel from "excel"
let file = excel.createFile()
//设置第二列样式,styleIndex由newStyle函数创建并返回
file.setColStyle("Sheet1", "B",styleIndex)
setRowStyle
设置行样式
语法
setRowStyle(sheetName:string,startRow:int,endRow:int,styleIndex:int)
参数
- sheetName: 工作表名称
- startRow: 起始行。例如,1 表示第一行
- endRow: 截止行。例如,2 表示第一行
- styleIndex: 样式索引
示例
import excel from "excel"
let file = excel.createFile()
//设置1-3行的样式,styleIndex由newStyle函数创建并返回
file.setRowStyle("Sheet1", 1,3,styleIndex)
deleteSheet
删除工作表,当只有1个工作表时,删除操作将无效
语法
deleteSheet(sheetName:string)
参数
- sheetName:工作表名称
import excel from "excel"
let file = excel.createFile()
file.deleteSheet("Sheet1")
getCellValue
获取单元格内容
语法
getCellValue(sheetName:string, cell:string):string
参数
- sheetName:工作表名称
- cell:单元格名称,格式为:列名+行索引,如:A1(第一行第一列)
返回值
- string :单元格内容。默认读取的所有内容都为string类型
示例
import excel from "excel"
let file = excel.openFile("./1.xlsx")
let value = file.getCellValue("Sheet1", "A1")
console.log(value)
getRows
获取sheet页所有内容
语法
getRows(sheetName:string):any[]
参数
- sheetName:工作表名称
返回值
- any[] : 工作表中的所有内容
示例
import excel from "excel"
let file = excel.openFile("./excel.xlsx")
let rows = file.getRows("Sheet1")
console.log(rows) //结果为: [ [ '字符串', '1.21' ], [ '', '2222' ], [ 'TRUE', '43434' ] ]
mergeCell
合并单元格
语法
mergeCell(sheetName:string,topLeftCell:string,bottomRightCell:string)
参数
- sheetName: 工作表名称
- topLeftCell: 表示单元格范围的左上角单元格。例如,"A1" 表示第一列第一行
- bottomRightCell: 表示单元格范围的右下角单元格。例如,"C3" 表示第三列第三行。
示例
import excel from "excel"
let file = excel.openFile("./excel.xlsx")
//第一行的第1-2列进行合并
file.mergeCell("Sheet1","A1","B1")
save
保存文件
语法
save(filePath:string)
参数
- filePath: 文件路径
示例
import excel from "excel"
let file = excel.createFile()
file.setCellValue("Sheet1", "A1", "测试1")
file.setCellValue("Sheet1", "A2", "测试2")
file.save("./1.xlsx")
write
将文件内容写入到一个writer中
语法
write(writer:Writer)
参数
- writer: 输出流
示例
import excel from "excel"
let file = excel.createFile()
file.setCellValue("Sheet1", "A1", "测试1")
file.setCellValue("Sheet1", "A2", "测试2")
let buf = new Buffer()
file.write(buf)
close
关闭文件,记得操作完文档后,一定要关闭文件
语法
close()
示例
import excel from "excel"
let file = excel.createFile()
file.setCellValue("Sheet1", "A1", "测试1")
file.setCellValue("Sheet1", "A2", "测试2")
file.save("./1.xlsx")
file.close()
getBuffer
获取文件的字节数组对象buffer
语法
getBuffer():Buffer
返回值
- Buffer: buffer对象
示例
import excel from "excel"
let file = excel.createFile()
file.setCellValue("Sheet1", "A1", "测试1")
file.setCellValue("Sheet1", "A2", "测试2")
let bu = file.getBuffer()
Style
样式对象的属性
属性名称 | 类型 | 说明 |
---|---|---|
border | object[] | 用于定义单元格的边框 |
fill | object | 用于定义单元格的填充样式 |
font | object | 用于定义单元格的字体样式 |
alignment | object | 用于定义单元格的对齐方式 |
protection | object | 用于定义单元格的保护属性 |
numFmt | int | 用于定义单元格的内置数字格式索引 |
decimalPlaces | int | 用于设置内置货币格式的小数位数 |
customNumFmt | string | 用于定义单元格的自定义数字格式 |
negRed | boolean | 用于将负数显示为红色 |
border
用于定义单元格的边框,是一个对象数组,对象属性如下:
{
type: "left", //边框类型(如 "left", "top", "right", "bottom", "diagonalDown", "diagonalUp")
color: "#000000", //边框颜色(使用 RGB 十六进制格式,如 "#000000")
style: 1 //边框样式(具体样式参见下表)
}
type
边框类型 | 描述 |
---|---|
left | 左边框 |
top | 上边框 |
right | 右边框 |
bottom | 下边框 |
diagonalDown | 左上到右下对角线边框 |
diagonalUp | 右上到左下对角线边框 |
style
边框样式 | 名称 | 宽度 | 显示效果 |
---|---|---|---|
0 | 无 | 0 | |
1 | 连续 | 1 | ----------- |
2 | 连续 | 2 | ----------- |
3 | 短划线 | 1 | - - - - - - |
4 | 点 | 1 | . . . . . . |
5 | 连续 | 3 | ----------- |
6 | 双线 | 3 | =========== |
7 | 连续 | 0 | ----------- |
8 | 短划线 | 2 | - - - - - - |
9 | 短划线点 | 1 | - . - . - . |
10 | 短划线点 | 2 | - . - . - . |
11 | 短划线双点 | 1 | - . . - . . |
12 | 短划线双点 | 2 | - . . - . . |
13 | 斜短划线点 | 2 | / - . / - . |
示例
import excel from "excel"
let file = excel.createFile()
const style = {
border: [
{ type: "left", color: "#000000", style: 1 },
{ type: "top", color: "#000000", style: 1 },
{ type: "bottom", color: "#000000", style: 1 },
{ type: "right", color: "#000000", style: 1 }
]
};
let styleIndex = file.newStyle(style)
fill
用于定义单元格的填充样式,对象属性如下:
{
type: "pattern",//填充类型,取值范围 [pattern,gradient],
pattern: 1, //填充模式索引(参考下表)
color: ["#FFFF00"],//填充颜色数组
shading: 0 //填充阴影索引(可选)(参考下表)
}
type
用于定义单元格的填充类型。
- pattern:使用图案填充,可以结合
pattern
字段和color
字段定义具体的填充图案和颜色。 - gradient:使用渐变填充,可以结合
color
字段定义渐变的颜色。
pattern
索引 | 样式 | 索引 | 样式 |
---|---|---|---|
0 | 无 | 10 | 深格子 |
1 | 实心 | 11 | 浅水平线 |
2 | 中灰 | 12 | 浅垂直线 |
3 | 深灰 | 13 | 浅向下对角线 |
4 | 浅灰 | 14 | 浅向上对角线 |
5 | 深水平线 | 15 | 浅格子 |
6 | 深垂直线 | 16 | 浅格子 |
7 | 深向下对角线 | 17 | 灰色125 |
8 | 深向上对角线 | 18 | 灰色0625 |
9 | 深格子 |
shading
索引 | 样式 | 索引 | 样式 |
---|---|---|---|
0-2 | 水平 | 9-11 | 向下对角线 |
3-5 | 垂直 | 12-15 | 从角落开始 |
6-8 | 向上对角线 | 16 | 从中心开始 |
示例
import excel from "excel"
let file = excel.createFile()
const style = {
fill: {
type: "pattern",
pattern: 1,
color: ["#FFFF00"],
shading: 0
},
};
let styleIndex = file.newStyle(style)
font
用于定义单元格的字体样式,对象属性如下:
{
bold: true,//是否加粗
italic: true,//是否斜体
underline: "single",//下划线样式,可选范围:["none", "single", "double"]
family: "Arial",//字体
size: 12, //字体大小
color: "#FF0000", //字体颜色(使用 RGB 十六进制格式,如 "#FF0000")
colorTint: 0,//颜色的色调调整.正值表示变亮,负值表示变暗,浮点数,范围从 -1 到 1
strike: false,//是否使用删除线
vertAlign: "" //文本的垂直对齐方式,可选范围:[baseline,superscript,subscript];baseline(默认基线对齐),superscript(上标),subscript(下标)
}
示例
import excel from "excel"
let file = excel.createFile()
const style = {
font: {
bold: true,
italic: true,
underline: "",
family: "Arial",
size: 12,
strike: false,
color: "#FF0000",
colorTint: 0,
vertAlign: ""
},
};
let styleIndex = file.newStyle(style)
alignment
用于定义单元格的对齐方式,对象属性如下:
{
horizontal: "center",//水平对齐方式(参考下表)
vertical: "center",//垂直对齐方式(参考下表)
indent: 0, //定义单元格内容的缩进级别。每一级缩进表示三个空格的宽度。整数,表示缩进级别。示例:Indent: 2(表示缩进 2 级,即 6 个空格)
justifyLastLine: false,//(可选)最后一行对齐在水平对齐设置为 "justify" 时,如果为 true,则对齐最后一行内容。
readingOrder: 0, //单元格内容的阅读顺序。取值范围:[0,1,2]。0:上下文相关,1:从左到右,2:从右到左
relativeIndent: 0,//(可选)相对缩进级别,用于进一步调整文本的缩进。整数,表示相对缩进级别
shrinkToFit: false,//(可选)自动缩小以适应单元格大小。如果为 true,则自动缩小内容以适应单元格大小
textRotation: 0,//定义单元格内容的文本旋转角度。整数,范围为 0 到 180,表示逆时针旋转角度;示例:TextRotation: 90(表示文本逆时针旋转 90 度)
wrapText: false //(可选)自动换行。如果为 true,则自动换行显示单元格内容
}
horizontal
水平对齐方式 | 描述 |
---|---|
left | 左对齐 |
center | 居中对齐 |
right | 右对齐 |
fill | 填充 |
justify | 两端对齐 |
centerContinuous | 跨列居中 |
distributed | 分散对齐 |
vertical
垂直对齐方式 | 描述 |
---|---|
top | 顶端对齐 |
center | 垂直居中 |
justify | 垂直两端对齐 |
distributed | 垂直分散对齐 |
示例
import excel from "excel"
let file = excel.createFile()
const style = {
alignment: {
horizontal: "center",
indent: 0,
justifyLastLine: false,
readingOrder: 0,
relativeIndent: 0,
shrinkToFit: false,
textRotation: 0,
vertical: "center",
wrapText: false
},
};
let styleIndex = file.newStyle(style)
protection
用于定义单元格的保护属性,对象属性如下:
{
hidden: false,//是否隐藏单元格内容
locked: true //是否锁定单元格
}
示例
import excel from "excel"
let file = excel.createFile()
const style = {
protection: {
hidden: false,
locked: true
},
};
let styleIndex = file.newStyle(style)
numFmt
用于定义单元格的内置数字格式索引。Excel 的内置所有语言格式如下表所示:
索引 | 格式字符串 |
---|---|
0 | General |
1 | 0 |
2 | 0.00 |
3 | #,##0 |
4 | #,##0.00 |
5 | ($#,##0_);($#,##0) |
6 | ($#,##0_);Red |
7 | ($#,##0.00_);($#,##0.00) |
8 | ($#,##0.00_);Red |
9 | 0% |
10 | 0.00% |
11 | 0.00E+00 |
12 | # ?/? |
13 | # ??/?? |
14 | m/d/yy |
15 | d-mmm-yy |
16 | d-mmm |
17 | mmm-yy |
18 | h:mm AM/PM |
19 | h:mm:ss AM/PM |
20 | h:mm |
21 | h:mm:ss |
22 | m/d/yy h:mm |
23 | #,##0_)😭#,##0) |
24 | #,##0_);Red |
25 | #,##0.00_)😭#,##0.00) |
26 | #,##0.00_);Red |
27 | (* #,##0);(* (#,##0);(* "-");(@_) |
28 | ($* #,##0);($* (#,##0);($* "-");(@_) |
29 | (* #,##0.00);(* (#,##0.00);(* "-"??);(@_) |
30 | ($* #,##0.00);($* (#,##0.00);($* "-"??);(@_) |
31 | mm:ss |
32 | [h]:mm:ss |
33 | mm:ss.0 |
34 | ##0.0E+0 |
35 | @ |
36 | |
37 | (#,##0_)😭#,##0) |
38 | (#,##0_);Red |
39 | (#,##0.00_)😭#,##0.00) |
40 | (#,##0.00_);Red |
41 | (* #,##0);(* (#,##0);(* "-");(@_) |
42 | ($* #,##0);($* (#,##0);($* "-");(@_) |
43 | (* #,##0.00);(* (#,##0.00);(* "-"??);(@_) |
44 | ($* #,##0.00);($* (#,##0.00);($* "-"??);(@_) |
45 | mm:ss |
46 | [h]:mm:ss |
47 | mm:ss.0 |
48 | ##0.0E+0 |
49 | @ |
示例
import excel from "excel"
let file = excel.createFile()
const style = {
numFmt: 14,
};
let styleIndex = file.newStyle(style)
decimalPlaces
用于设置内置货币格式的小数位数。(可选)
示例
import excel from "excel"
let file = excel.createFile()
const style = {
decimalPlaces: 2,
};
let styleIndex = file.newStyle(style)
customNumFmt
用于定义单元格的自定义数字格式。
示例
import excel from "excel"
let file = excel.createFile()
const style = {
customNumFmt: "\"¥\"#,##0.00",
};
let styleIndex = file.newStyle(style)
negRed
用于将负数显示为红色
示例
import excel from "excel"
let file = excel.createFile()
const style = {
negRed: true
};
let styleIndex = file.newStyle(style)