Skip to content

informat.excel excel文件相关 ​

概述 ​

使用informat.excel对象进行Excel文件的读写操作

参考

文件路径必须是在本地的沙盒路径中,关于文件路径可参考 informat.file

openExistFile ​

打开一个沙盒环境已经存在的Excel文件

注意

如果file不存在,则会抛出异常

javascript
informat.excel.openExistFile(file)
参数类型描述
fileStringapp沙盒环境中的文件路径

返回值

返回Workbook对象

示例

javascript
informat.excel.openExistFile('gzb.xlsx')

openExistStorageFile ​

打开一个已经存储到共享存储的Excel文件

注意

如果file不存在,则会抛出异常

javascript
informat.excel.openExistStorageFile(file)
参数类型描述
fileString共享存储中的文件路径

返回值

返回Workbook对象

示例

javascript
informat.excel.openExistStorageFile('automatic/n3uiy48tig3ch538lwxej.xlsx')

openNewFile ​

创建一个新的Excel文件

javascript
informat.excel.openNewFile(file)

TIP

如果文件存在,在保存时会覆盖原有的文件内容

参数类型描述
fileStringapp的沙盒环境中的文件地址

返回值

返回Workbook对象

示例

javascript
informat.excel.openNewFile('gzb2.xlsx')

openWithTemplate ​

根据模版创建Workbook。

javascript
informat.excel.openWithTemplate(file, templateFile, data);
参数类型描述
fileString生成后的Excel文件
templateFileString模板文件的地址
dataObject传递给模板文件的变量

返回值

返回Workbook对象


openWithTemplateWithSheets ​

根据多sheet模版创建Workbook。

javascript
informat.excel.openWithTemplateWithSheets(file, templateFile, sheetNums, data);

TIP

如果sheetNums为空,则表示导出所有工作表。

参数类型描述
fileString生成后的Excel文件
templateFileString模板文件的地址
sheetNumsArray<Integer>工作表编号列表,从0开始;如果只想导出第2和第3个工作表,则输入[1,2]
dataObject传递给模板文件的变量

返回值

返回Workbook对象

模版表达式

createTemplatePicture ​

创建模版图片对象

javascript
informat.excel.createTemplatePicture(pic);
参数类型描述
imageExcelTamplatePicture图片配置

返回值

返回在模板中使用的图片

示例

javascript
informat.excel.createTemplatePicture({
    type: 'file', // 本地存储
    content: 'pathto/img.jpg', //本地存储中的文件路径
    width: 100, //宽度
    height: 100, //高度
});

informat.excel.createTemplatePicture({
    type: 'storage', // 共享存储
    content: 'pathto/img.jpg', //共享存储中的文件路径
});
javascript
informat.excel.createTemplatePicture({
    type: 'barcode', //条形码
    content: '1234557', // 内容
    width: 100, // 宽度
    height: 100 // 高度
});
javascript
informat.excel.createTemplatePicture({
    type: 'qrcode', // 二维码
    content: 'https://informat.cn', // 内容
    width: 100, // 宽度
    height: 100 // 高度
});

createTemplateCell ​

创建模版单元格

javascript
informat.excel.createTemplateCell(cell);
参数类型描述
cellExcelTamplateCell单元格配置

返回值

返回在模板中使用的单元格

示例

js
const companyCell = informat.excel.createTemplateCell({
    content: '深圳市基石协作有限公司', // 单元格内容
    rowspan: 2, // 行单元格数,默认为1
    colspan: 2  // 列单元格数,默认为1
});

Workbook ​

createSheet ​

创建新的Sheet

javascript
workbook.createSheet(sheetName)

TIP

sheetName不能包含 0x0000 0x0003 : \ * ? / [``]

参数类型描述
sheetNameStringsheet的名称

返回值

返回Sheet对象

示例

js
workbook.createSheet('sheet1')

createSafeSheetName ​

创建一个可用的sheet名称

javascript
workbook.createSafeSheetName(sheetName)

TIP

在sheet名称中不可以出现一些特殊字符,调用此方法可自动的将这些不允许的特殊字符过滤

参数类型描述
sheetNameStringsheet的名称

返回值

返回String,过滤掉特殊字符的sheet名称

示例

js
workbook.createSafeSheetName('sheet1')

getSheet ​

根据名称获取Sheet

javascript
workbook.getSheet(sheetName)
参数类型描述
sheetNameStringsheet的名称

返回值

返回Sheet对象,如果sheetName不存在,则返回null

示例

js
workbook.getSheet('sheet1')

getSheetAt ​

根据位置顺序获取Sheet

javascript
workbook.getSheetAt(sheetIdx)
参数类型描述
sheetIdxIntegersheet的顺序,从0开始

返回值

返回Sheet对象,如果sheet不存在,则返回null

示例

js
workbook.getSheetAt(0)

getNumberOfSheets ​

获取sheet的数量

javascript
workbook.getNumberOfSheets()

返回值 返回Integer对象,sheet的数量


write ​

将内容写入文件

javascript
workbook.write()

TIP

调用write后将sheet的内容写入文件中,并关闭workbook,不能重复的调用write方法,如果在写入的过程中出现错误(磁盘空间不足等),将会抛出异常


createFont ​

创建一个字体配置

TIP

用于设置单元格格式

javascript
workbook.createFont()

返回值

返回Font对象,描述字体的对象


createCellStyle ​

创建单元格样式配置

javascript
workbook.createCellStyle()

TIP

单元格样式配置可以设置单元格的颜色,背景色,边框,对齐方式等样式

返回值 返回CellStyle对象,描述单元格样式的对象

Sheet ​

getSheetName ​

获取sheet名称

javascript
sheet.getSheetName()

返回值

返回String对象,sheet名称


createRow ​

创建一行

javascript
sheet.createRow(row)
参数类型描述
rowInteger行的索引,从0开始

返回值

返回Row对象

示例

js
sheet.createRow(0)

getRow ​

返回行

javascript
sheet.getRow(row)
参数类型描述
rowInteger行的索引,从0开始

返回值

返回Row对象

示例

js
sheet.getRow(0)

removeRow ​

删除行

javascript
sheet.removeRow(row)
参数类型描述
rowInteger行的索引,从0开始

示例

js
sheet.removeRow(0)

getFirstRowNum ​

返回sheet中的第一行的索引

javascript
sheet.getFirstRowNum()

返回值

返回Integer第一行的索引


getLastRowNum ​

返回sheet中的最后一行的索引

javascript
sheet.getLastRowNum()

返回值

返回Integer最后一行的索引


getDefaultRowHeightInPoints ​

返回默认的行高

javascript
sheet.getDefaultRowHeightInPoints()

返回值 返回Double 默认行高


setDefaultRowHeightInPoints ​

设置默认行高

javascript
sheet.setDefaultRowHeightInPoints(height)
参数类型描述
heightDouble默认行高,单位为像素

getDefaultColumnWidth ​

返回默认列宽

javascript
sheet.getDefaultColumnWidth()

返回值

返回Integer 默认列宽


setDefaultColumnWidth ​

返回默认列宽

javascript
sheet.setDefaultColumnWidth(width)
参数类型描述
widthInteger默认列宽

setColumnWidth ​

返回默认列宽

javascript
sheet.setColumnWidth(colIdx, width)
参数类型描述
colIdxInteger列索引
widthInteger列宽

autoSizeColumn ​

根据内容自动设置列的宽度

javascript
sheet.autoSizeColumn(colIdx)

TIP

在数据插入完成后执行自动列宽函数才能生效

参数类型描述
colIdxInteger列索引

setDefaultColumnStyle ​

设置列的默认单元格样式

javascript
sheet.setDefaultColumnStyle(colIdx, cellStyle)
参数类型描述
colIdxInteger列的索引,从0开始
cellStyleCellStyle单元格样式

getColumnStyle ​

返回列的默认单元格样式

javascript
sheet.getColumnStyle(colIdx)
参数类型描述
colIdxInteger列的索引,从0开始

返回值

返回CellStyle默认单元格样式


addMergedRegion ​

合并单元格

javascript
sheet.addMergedRegion(firstRow, lastRow, firstColumn, lastColumn)
参数类型描述
firstRowInteger第一行,从0开始
lastRowInteger最后一行,从0开始
firstColumnInteger第一列,从0开始
lastColumnInteger最后一列,从0开始

addPicture ​

在单元格中插入本地沙盒中的图片

javascript
sheet.addPicture(col, row, filePath)
参数类型描述
colInteger插入的列,从0开始
rowInteger插入的行,从0开始
filePathString插入的图片路径,文件路径是在应用沙盒中的文件地址

返回值

返回Picture对象


addPictureStorage ​

在单元格中插入共享存储中的图片

javascript
sheet.addPictureStorage(col, row, filePath)
参数类型描述
colInteger插入的列,从0开始
rowInteger插入的行,从0开始
filePathString插入的图片路径,文件路径是在共享存储中的路径

返回值 返回Picture 对象


addPictureBarcode ​

在单元格中插入条码图片

javascript
sheet.addPictureBarcode(col, row, setting)
参数类型描述
colInteger插入的列,从0开始
rowInteger插入的行,从0开始
settingPictureSetting图片配置

返回值

返回Picture 对象


addPictureQrcode ​

在单元格中插入二维码图片

javascript
sheet.addPictureQrcode(col, row, setting)
参数类型描述
colInteger插入的列,从0开始
rowInteger插入的行,从0开始
settingPictureSetting图片配置

返回值

返回Picture 对象


getPictures ​

返回sheet中的所有图片

javascript
sheet.getPictures()

返回值

返回Array<Picture> 对象


getObjectDatas ​

返回sheet中的所有附件

javascript
sheet.getObjectDatas()

返回值 返回Array<ExcelObjectData> 对象

Row ​


createCell ​

创建单元格

javascript
row.createCell(colIdx)
参数类型描述
colIdxInteger插入的列,从0开始

返回值 返回Cell 对象


createCellWithValue ​

创建单元格

javascript
row.createCellWithValue(colIdx, value)
参数类型描述
colIdxInteger插入的列,从0开始
valueObject插入的值,Date,Boolean,Number类型会按照原始类型保存,其它类型会转换为字符串

返回值 返回Cell 对象


getCell ​

获取单元格

javascript
row.getCell(colIdx)
参数类型描述
colIdxInteger插入的列,从0开始

返回值

返回Cell 对象,如果单元格不存在返回null


removeCell ​

删除单元格

javascript
row.removeCell(colIdx)
参数类型描述
colIdxInteger删除的列,从0开始

getFirstCellNum ​

返回第一列的索引

javascript
row.getFirstCellNum()

返回值

返回Integer,第一列的索引


getLastCellNum ​

返回最后一列的索引

javascript
row.getLastCellNum()

返回值

返回Integer,最后一列的索引


getRowNum ​

返回行的索引

javascript
row.getRowNum()

返回值

返回Integer,行索引


getHeightInPoints ​

返回行高

javascript
row.getHeightInPoints()

返回值

返回Double,行高


setHeightInPoints ​

设置行高

javascript
row.setHeightInPoints(height)
参数类型描述
heightDouble行高,单位为像素

setStyle ​

设置行的单元格样式

javascript
row.setStyle(cellStyle)
参数类型描述
cellStyleCellStyle单元格样式

getStyle ​

返回行的单元格格式

javascript
row.getStyle()

返回值

返回CellStyle

createCellComment ​

创建批注

javascript
row.createCellComment(colIndex)

返回值

返回ExcelComment

getCellComment ​

获取批注

javascript
row.getCellComment(colIndex)

返回值

返回ExcelComment

Cell ​

setValue ​

设置单元格的值

javascript
cell.setValue(value)

TIP

单元格支持String Boolean Double Date 类型的值,如果为其它类型将会转换成字符串存储

参数类型描述
valueObject单元格的值

getValue ​

返回单元格的值

javascript
cell.getValue()

getValueEvaluated ​

计算完公式以后,返回单元格的值

javascript
cell.getValueEvaluated()

setStyle ​

设置单元格样式

javascript
cell.setStyle(cs)
参数类型描述
cellStyleCellStyle单元格样式

getStyle ​

返回单元格样式

javascript
cell.getStyle()

返回值

返回CellStyle


设置单元格超链接

javascript
cell.setHyperlink(link)
参数类型描述
linkString链接地址

返回单元格的超链接

javascript
cell.getHyperlink()

返回值

返回String 超链接的地址,如果超链接不存在则返回null

Style ​


setAlignment ​

设置横向对齐

javascript
cellStyle.setAlignment(align)
参数类型描述
alignString可选值CENTER CENTER_SELECTION DISTRIBUTED FILL GENERAL JUSTIFY LEFT RIGHT

getAlignment ​

返回横向对齐

javascript
cellStyle.getAlignment()

返回值

类型String


setVerticalAlignment ​

设置垂直对齐

javascript
cellStyle.setVerticalAlignment(align)
参数类型描述
alignString可选值BOTTOM CENTER DISTRIBUTED JUSTIFY TOP

getVerticalAlignment ​

返回垂直对齐

javascript
cellStyle.getVerticalAlignment()

返回值

类型String


getDataFormatString ​

返回单元格数据格式

javascript
cellStyle.getDataFormatString()

返回值

类型String


setWrapText ​

设置单元格文字是否可以换行

javascript
cellStyle.setWrapText(wrap)
参数类型描述
wrapBoolean文字是否换行

getWrapText ​

返回单元格文字是否换行

javascript
cellStyle.getWrapText()

返回值

返回String


setFont ​

设置单元格文字的字体

javascript
cellStyle.setFont(font)
参数类型描述
fontFont字体

setFillForegroundColor ​

设置单元格填充的前景色

javascript
cellStyle.setFillForegroundColor(color)
参数类型描述
colorString十六进制的颜色标识,例如#ff0000 红色

setFillBackgroundColor ​

设置单元格填充的背景色

javascript
cellStyle.setFillBackgroundColor(color)
参数类型描述
colorString十六进制的颜色标识,例如#ff0000 红色

setFillPattern ​

设置填充模式

javascript
cellStyle.setFillPattern(type)
参数类型描述
typeString可选值为 ALT_BARS; BIG_SPOTS; BRICKS; DIAMONDS; FINE_DOTS; LEAST_DOTS; LESS_DOTS; NO_FILL; SOLID_FOREGROUND; SPARSE_DOTS; SQUARES; THICK_BACKWARD_DIAG;THICK_FORWARD_DIAG; THICK_HORZ_BANDS; THICK_VERT_BANDS; THIN_VERT_BANDS;

setHidden ​

设置填充模式

javascript
cellStyle.setHidden(hidden)
参数类型描述
hiddenBoolean设置是否隐藏

setIndention ​

设置缩进

javascript
cellStyle.setIndention(indent)
参数类型描述
indentInteger缩进值

setBorderTop ​

设置上边框样式

js
cellStyle.setBorderTop(borderStyle);
参数类型描述
borderStyleString边框样式

边框样式可选值:

  • NONE:无边框
  • THIN:细边框
  • MEDIUM:中等边框
  • DASHED:虚线边框
  • DOTTED:点状边框
  • THICK:粗边框
  • DOUBLE:双线边框
  • HAIR:极细边框
  • MEDIUM_DASHED:中等虚线边框
  • DASH_DOT:点划线边框
  • MEDIUM_DASH_DOT:中等点划线边框
  • DASH_DOT_DOT:点划点线边框
  • MEDIUM_DASH_DOT_DOT:中等点划点线边框
  • SLANTED_DASH_DOT:倾斜点划线边框

setBorderBottom ​

设置下边框样式

js
cellStyle.setBorderBottom(borderStyle);
参数类型描述
borderStyleString边框样式

setBorderLeft ​

设置左边框样式

js
cellStyle.setBorderLeft(borderStyle);
参数类型描述
borderStyleString边框样式

setBorderRight ​

设置右边框样式

js
cellStyle.setBorderRight(borderStyle);
参数类型描述
borderStyleString边框样式

setLeftBorderColor ​

设置左侧边框颜色

javascript
cellStyle.setLeftBorderColor(color)
参数类型描述
colorString颜色,例如 #f0f0f0

setRightBorderColor ​

设置右侧边框颜色

javascript
cellStyle.setRightBorderColor(color)
参数类型描述
colorString颜色,例如 #f0f0f0

setTopBorderColor ​

设置顶部边框颜色

javascript
cellStyle.setTopBorderColor(color)
参数类型描述
colorString颜色,例如 #f0f0f0

setBottomBorderColor ​

设置底部边框颜色

javascript
cellStyle.setBottomBorderColor(color)
参数类型描述
colorString颜色,例如 #f0f0f0

setRotation ​

设置文字旋转角度

javascript
cellStyle.setRotation(rotation)
参数类型描述
rotationInteger旋转角度,例如90

setDataFormat ​

设置单元格格式

javascript
cellStyle.setDataFormat(fmt)
参数类型描述
fmtString单元格格式,例如#0.00

Font ​

setBold ​

设置文字是否粗体

javascript
font.setBold(bold)
参数类型描述
boldBoolean是否粗体

setItalic ​

设置文字是否斜体

javascript
font.setItalic(italic)
参数类型描述
italicBoolean是否斜体

setUnderline ​

设置文字是否有下划线

javascript
font.setUnderline(underline)
参数类型描述
underlineBoolean是否有下划线

setFontHeightInPoints ​

设置字体大小

javascript
font.setFontHeightInPoints(size)
参数类型描述
sizeInteger字体大小

setFontName ​

设置字体名称

javascript
font.setFontName(name)
参数类型描述
nameString字体名称

Picture ​


getRow ​

返回所在行

javascript
picture.getRow()

返回值

返回Integer


getColumn ​

返回所在列

javascript
picture.getColumn()

返回值

返回Integer


save ​

将图片中的内容保存到本地沙盒中

javascript
picture.save(path)
参数类型描述
pathString文件在本地沙盒中的路径

saveStorage ​

将图片中的内容保存到共享存储中

javascript
picture.saveStorage(path)
参数类型描述
pathString文件在共享存储中的路径

saveAttachment ​

将图片中的内容保存为附件字段

javascript
picture.saveAttachment(tableId, fieldId)
参数类型描述
tableIdString数据表的标识符
fieldIdString附件字段的标识符

返回值

返回TableAttachment对象


setScale ​

设置图片的缩放比例

javascript
picture.setScale(wScale, hScale)
参数类型描述
wScaleDouble宽度的缩放比例
hScaleDouble高度的缩放比例

setSize ​

设置图片的高宽

javascript
picture.setSize(width, height)
参数类型描述
widthInteger图片高度
heightInteger图片宽度

fitWidth ​

按照图片所在单元格的宽度缩放图片

javascript
picture.fitWidth()

TIP

缩放后图片的宽度小于等于所在单元格的宽度,图片的高度跟随宽度缩放的比例


fitHeight ​

按照图片所在单元格的高度缩放图片

javascript
picture.fitHeight()

TIP

缩放后图片的高度小于等于所在单元格的高度,图片的宽度跟随高度缩放的比例


resetSize ​

设置图片大小为图片的原始尺寸

javascript
picture.resetSize()

ExcelObjectData ​

Excel附件


getRow ​

返回附件所在行

javascript
objectData.getRow()

返回值

返回Integer


getColumn ​

返回附件所在列

javascript
objectData.getColumn()

返回值

返回Integer


save ​

将文件内容保存到本地沙盒中

javascript
objectData.save(path)
参数类型描述
pathString文件在本地沙盒中的路径

saveStorage ​

将文件内容保存到共享存储中

javascript
objectData.saveStorage(path)
参数类型描述
pathString文件在共享存储中的路径

saveAttachment ​

将文件内容保存为附件字段

javascript
objectData.saveAttachment(tableId, fieldId)
参数类型描述
tableIdString数据表的标识符
fieldIdString附件字段的标识符

返回值

返回TableAttachment对象


hasDirectoryEntry ​

是否有关联的POIFS目录条目

javascript
objectData.hasDirectoryEntry()

getOLE2ClassName ​

返回OLE2类对象的名称

javascript
objectData.getOLE2ClassName()

getFileName ​

获取文件名称

javascript
objectData.getFileName()

getContentType ​

获取文件类型

javascript
objectData.getContentType()

示例 ​

示例1:读取Excel中所有单元格的数据 ​

js
const workbook = informat.excel.openExistFile('test.xlsx');
for (var sheetIdx = 0; sheetIdx < workbook.getNumberOfSheets(); sheetIdx++) {
    const sheet = workbook.getSheetAt(sheetIdx);
    console.log("sheet name is " + sheet.getSheetName());
    //返回所有的单元格数据
    for (var rowIndex = sheet.getFirstRowNum(); rowIndex <= sheet.getLastRowNum(); rowIndex++) {
        const row = sheet.getRow(rowIndex);
        for (var colIndex = row.getFirstCellNum(); colIndex < row.getLastCellNum(); colIndex++) {
            const cell = row.getCell(colIndex);
            if (cell != null) {
                console.log(cell.getValueEvaluated());
            }
        }
    }
    //返回所有的图片
    sheet.getPictures().forEach(p => {
        console.log("picture " + p.getRow() + " " + p.getColumn());
    });
    //读取图片后,可通过picture的save(localPath),saveStorage(path),saveAttachment(tableKey,fieldKey,fieldName)可以将文件内容存储到本地沙盒或者是共享存储中
}

示例2:将数据写入到Excel文件中 ​

js
const workbook = informat.excel.openNewFile('test.xlsx');
const sheet = workbook.createSheet("sheet1");
const style = workbook.createCellStyle();
const font = workbook.createFont();
font.setBold(true);
font.setItalic(true);
font.setFontHeightInPoints(14);
//
style.setFont(font);
style.setFillBackgroundColor("#fafafa");//设置灰色背景
style.setAlignment("CENTER");//水平居中
style.setVerticalAlignment("CENTER");//垂直居中
style.setFillPattern("SOLID_FOREGROUND");
//
for (var i = 0; i < 10; i++) {
    const row = sheet.createRow(i);
    const cell = row.createCellWithValue(0, "字符串");
    cell.setStyle(style);
    row.createCellWithValue(1, "https://informat.cn/").setHyperlink("https:/informat.cn");
    row.createCellWithValue(2, true);
    row.createCellWithValue(3, 3.14);
    row.createCellWithValue(4, new Date());
}
//
sheet.addMergedRegion(0, 2, 0, 3);//合并单元格
// 在数据插入完成后执行自动列宽函数才能生效
sheet.autoSizeColumn(0);//第一列第二列 自动调整宽度
sheet.autoSizeColumn(1);
//
workbook.write();//写入文件
//

示例3:使用模板文件生成Excel ​

js
const templateFilePath = 'excel_template.xlsx';
const outputFilePath = 'result.xlsx';
informat.website.download('website', 'excel/excel_template.xlsx', templateFilePath);
const workbook = informat.excel.openWithTemplate(outputFilePath, templateFilePath, {
    'orgName': '公司名称xxx',
    'goodAtProduct': '产品xxx',
    'addresss': '公司地址xxxxxxx',
    'reviewUser': '张三',
    'reviewTime': new Date(),
    'list': [
        { 'sort': 1, 'question': '项目1', 'standard': '优秀', 'score': 95, 'realScore': 98, 'realTotalScore': 99 },
        { 'sort': 2, 'question': '项目2', 'standard': '良好', 'score': 78, 'realScore': 79, 'realTotalScore': 80 },
        { 'sort': 3, 'question': '项目3', 'standard': '及格', 'score': 65, 'realScore': 62, 'realTotalScore': 66 },
    ]
});
workbook.write();//写入文件

TIP

模板下载excel_template.xlsx

示例中是把模板文件export_muti_sheet_template.xlsx上传到标识符是website的资源与组件设计器模块的excel文件夹下

示例4:插入图片 ​

js
const workbook = informat.excel.openNewFile('test-pic.xlsx');
const sheet = workbook.createSheet("sheet1");
//
for (var i = 0; i < 5; i++) {
    const row = sheet.createRow(i);
    row.createCellWithValue(0, '第1列');
    row.createCellWithValue(1, '第2列');
}
//
sheet.addPicture(0, 0, "path/to/image.png").setSize(60, 60);
sheet.addPicture(1, 0, "path/to/image.png").setSize(60, 60);
//
workbook.write();//写入文件
//

示例5:根据Excel多个sheet模版导出 ​

js

// 下载模板文件
export function exportMutiSheetTemplate() {
  // 定义模板文件路径
  const templateFilePath = 'mutiSheetTemplate.xlsx';
  const outputFilePath = 'result.xlsx';
  informat.website.download('website', 'excel/export_muti_sheet_template.xlsx', templateFilePath);
  
  // 定义要导出的 sheet 编号列表,从 0 开始。如果为空,则导出所有 sheet
  const sheetNums = [0, 1, 2]; // 导出第一个、第二个和第三个 sheet
  
  // 定义传递给模板的数据
  const data = {
    'orgName': '公司名称xxx',
    'goodAtProduct': '产品xxx',
    'addresss': '公司地址xxxxxxx',
    'reviewUser': '张三',
    'reviewTime': new Date(),
    'list': [
      {'sort': 1, 'question': '项目1', 'standard': '优秀', 'score': 95, 'realScore': 98, 'realTotalScore': 99},
      {'sort': 2, 'question': '项目2', 'standard': '良好', 'score': 78, 'realScore': 79, 'realTotalScore': 80},
      {'sort': 3, 'question': '项目3', 'standard': '及格', 'score': 65, 'realScore': 62, 'realTotalScore': 66},
    ]
  };
  
  // 使用模板文件创建一个新的 Excel 文件,并导出指定的 sheet
  const workbook = informat.excel.openWithTemplateWithSheets(outputFilePath, templateFilePath, sheetNums, data);
  
  // 写入文件
  workbook.write();
  return outputFilePath
}

TIP

模板下载export_muti_sheet_template.xlsx 示例中是把模板文件export_muti_sheet_template.xlsx上传到标识符为website的资源与组件设计器模块的excel文件夹下

示例6:创建批注 ​

js
const workbook = informat.excel.openNewFile('test.xlsx');
const sheet = workbook.createSheet("sheet1");
let row = sheet.createRow(0);
row.createCellWithValue(0, "这是一个测试批注")
let comment = row.createCellComment(0);
comment.setAuthor("系统管理员");
comment.setContent("这是一个测试批注,用于演示Excel批注功能!\n支持换行符哦~");
workbook.write();

示例7:获取批注 ​

js
const workbook = informat.excel.openExistFile('test.xlsx');
const sheet = workbook.getSheetAt(0);
let row = sheet.getRow(0);
let comment = row.getCellComment(0);
console.log('comment content',comment.getContent());
console.log('comment author',comment.getAuthor());

//输出:
comment content 这是一个测试批注,用于演示Excel批注功能!
支持换行符哦~
comment author 系统管理员