excel

excel

excel模块用于操作Office Excel文档,基于 ECMA-376,ISO/IEC 29500 国际标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的电子表格文档。支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多种文档格式

excel

属性与方法

名称类型参数返回值说明
createFile方法options?:objectExcelFile新建一个excel文档
openFile方法filePath:string,options?:objectExcelFile打开已有的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:stringint新建sheet页
newStyle方法style:objectint创建样式对象
setSheetName方法oldName:string, newName:string修改sheet页名称
setSheetRow方法sheetName:string,cell:string,rows:any[]设置行数据
getSheetList方法string[]获取sheet名列表
getSheetIndex方法name:stringint获取sheet名对应的索引
getSheetName方法index:intstring获取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:stringstring获取单元格内容
getRows方法sheetName:stringany[]获取一个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

样式对象的属性

属性名称类型说明
borderobject[]用于定义单元格的边框
fillobject用于定义单元格的填充样式
fontobject用于定义单元格的字体样式
alignmentobject用于定义单元格的对齐方式
protectionobject用于定义单元格的保护属性
numFmtint用于定义单元格的内置数字格式索引
decimalPlacesint用于设置内置货币格式的小数位数
customNumFmtstring用于定义单元格的自定义数字格式
negRedboolean用于将负数显示为红色

border

用于定义单元格的边框,是一个对象数组,对象属性如下:

{ 
    type: "left", //边框类型(如 "left", "top", "right", "bottom", "diagonalDown", "diagonalUp")
    color: "#000000", //边框颜色(使用 RGB 十六进制格式,如 "#000000")
    style: 1 //边框样式(具体样式参见下表)
}

type

边框类型描述
left左边框
top上边框
right右边框
bottom下边框
diagonalDown左上到右下对角线边框
diagonalUp右上到左下对角线边框

style

边框样式名称宽度显示效果
00
1连续1-----------
2连续2-----------
3短划线1- - - - - -
41. . . . . .
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

索引样式索引样式
010深格子
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 的内置所有语言格式如下表所示:

索引格式字符串
0General
10
20.00
3#,##0
4#,##0.00
5($#,##0_);($#,##0)
6($#,##0_);Red
7($#,##0.00_);($#,##0.00)
8($#,##0.00_);Red
90%
100.00%
110.00E+00
12# ?/?
13# ??/??
14m/d/yy
15d-mmm-yy
16d-mmm
17mmm-yy
18h:mm AM/PM
19h:mm:ss AM/PM
20h:mm
21h:mm:ss
22m/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);($* "-"??);(@_)
31mm:ss
32[h]:mm:ss
33mm: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);($* "-"??);(@_)
45mm:ss
46[h]:mm:ss
47mm: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)
更新时间 9/19/2024, 12:09:33 PM