CBrother提供了一个Excel扩展来操作excle文件,目前支持。xlsx和。csv两种格式。Excel类 Excel类表示一个Excel文档importlibexcelvarexcelnewExcel(); 函数 描述 用法 load(path) 加载excel文档,path为绝对路径,返回true加载成功 excel。load(e:test。xlsx) excel。load(e:test。csv) save(path) 保存excel文档,path为绝对路径,返回true保存成功 excel。save(e:test。xlsx) excel。save(e:test。csv) getWorksheetCount() 获取文档内表格数量 varcntexcel。getWorksheetCount() getWorksheet(name) 根据表格名字获取表格对象,没找到返回null varsheetexcel。getWorksheet(sheet1) getWorksheet(index) 根据序号获取表格对象,没找到返回null,序号从0开始 varsheetexcel。getWorksheet(0) addWorksheet(name) 添加一张表,name为表名,返回添加的表格对象,添加失败返回null varsheetexcel。addWorksheet(sheet2) removeWorksheet(name) 根据表格名字删除表格,返回true为删除成功。 excel。removeWorksheet(sheet2) removeWorksheet(index) 根据序号删除表格,返回true为删除成功,序号从0开始 excel。removeWorksheet(0)ExcelSheet类 ExcelSheet类表示excel内的一张表格。该对象不能主动创建,只能从Excel类里面获取。 函数 描述 用法 getTotalRows() 获取该表格使用到的最大行数 varrowsexcelsheet。getTotalRows() getTotalCols() 获取该表格使用到的最大列数 varclosexcelsheet。getTotalCols() getSheetName() 获取本张表格名称 varnameexcelsheet。getSheetName() setSheetName(name) 修改本张表格名称,返回true为修改成功 excelsheet。setSheetName(sheet2) getCell(name) 根据格子名称获取格子对象,对应格子没有内容返回null varcellexcelsheet。getCell(A5) getCell(row,cloName) 根据行数和列名称获取格子对象,row为整数表示行号,从0开始, cloName为字符串列名称,对应格子没有内容返回null varcellexcelsheet。getCell(4,A) getCell(row,clo) 根据行数和列数获取格子对象,row为整数表示行号,从0开始, clo为整数列号,从0开始,对应格子没有内容返回null varcellexcelsheet。getCell(4,0) addCell(name) 根据格子名称增加格子对象,返回增加的格子对象,出错返回null varcellexcelsheet。addCell(A5) addCell(row,cloName) 根据行数和列名称增加格子对象,row为整数表示行号,从0开始, cloName为字符串列名称,返回增加的格子对象,出错返回null varcellexcelsheet。addCell(4,A) addCell(row,clo) 根据行数和列数增加格子对象,row为整数表示行号,从0开始, clo为整数列号,从0开始,返回增加的格子对象,出错返回null varcellexcelsheet。addCell(4,0) getIndex() 获取本表格序号 varindexexcelsheet。getIndex() setIndex(index) 修改本表格序号,从0开始,修改成功返回true excelsheet。setIndex(1) mergeCells(referencestring) 合并单元格 excelsheet。mergeCells(A1:B5) mergeCells(leftTopCell,rightBottomCell) 合并单元格 excelsheet。mergeCells(A1,B5) mergeCells(leftRow,leftClo,rightRow,rightClo) 合并单元格 excelsheet。mergeCells(0,0,4,1) setRowHeight(row,height) 设置行高 excelsheet。setRowHeight(0,60) setColWidth(col,height) 设置列宽 excelsheet。setColWidth(0,60)ExcelCell类 ExcelCell类表示表格里的格子。该对象不能主动创建,只能从ExcelSheet类里面获取。 函数 描述 用法 getInt() 获取值,转为整数。 varvalueexcelCell。getInt() getDouble() 获取值,转为浮点数。 varvalueexcelCell。getDouble() getString() 获取值,转为字符串。 varvalueexcelCell。getString() set(value) 设置格子的内容,value可以为字符串、整数、浮点数 excelCell。set(0) excelCell。set(111) setFontColor(rgb) 设置格子字体颜色 excelCell。setFontColor(ff0000) setFontName(name) 设置格子字体 excelCell。setFontName(宋体) setFontBold(bold) 设置格子字体加粗 excelCell。setFontBold(true) setFontSize(size) 设置格子字号 excelCell。setFontSize(20) setBackgroundColor(rgb) 设置格子颜色 excelCell。setBackgroundColor(ffff00) setTopBorderColor(rgb) 设置格子顶部边框颜色 excelCell。setTopBorderColor(000000) setTopBorderStyle(style) 设置格子顶部边框样式,样式定义在libexcel。cb里 excelCell。setTopBorderStyle(BORDERSTYLEDASHED) setBottomBorderColor(rgb) 设置格子底部边框颜色 excelCell。setBottomBorderColor(000000) setBottomBorderStyle(style) 设置格子底部边框样式,样式定义在libexcel。cb里 excelCell。setBottomBorderStyle(BORDERSTYLETHIN) setLeftBorderColor(rgb) 设置格子左侧边框颜色 excelCell。setLeftBorderColor(000000) setLeftBorderStyle(style) 设置格子左侧边框样式,样式定义在libexcel。cb里 excelCell。setLeftBorderStyle(BORDERSTYLESLANTDASHDOT) setRightBorderColor(rgb) 设置格子右侧边框颜色 excelCell。setRightBorderColor(000000) setRightBorderStyle(style) 设置格子右侧边框样式,样式定义在libexcel。cb里 excelCell。setRightBorderStyle(BORDERSTYLEDOTTED) setDiagonalBorderColor(rgb) 设置格子斜线颜色 excelCell。setDiagonalBorderColor(000000) setDiagonalBorderStyle(style) 设置格子斜线样式,样式定义在libexcel。cb里 excelCell。setDiagonalBorderStyle(BORDERSTYLEDOUBLE) setDiagonalBorderShow(up,down) 设置格子斜线具体显示那条线 excelCell。setDiagonalBorderShow(true,true)创建一个Excel例子:importlibexcelfunctionmain(parm){varexcelnewExcel();varexcelsheetexcel。addWorksheet(222);varcellexcelsheet。addCell(A10);cell。set(aaa);cellexcelsheet。addCell(5,B);cell。set(100);cellexcelsheet。addCell(6,7);cell。set(200。0);excel。addWorksheet(333);excel。save(GetRoot()222。xlsx);excel。save(GetRoot()222。csv);} 运行后会在脚本工作路径旁边生成222。xlsx和222。csv 打开222。xlsx查看如下图,两张表都在,因为xlsx支持多张表 打开222。csv查看如下图,只有一张表,因为csv不支持多张表 读取Excel例子:importCBExcel。codefunctionmain(parm){varexcelnewExcel();varpathGetRoot()222。if(!excel。load(path)){printloaderr:}varsheetCntexcel。getWorksheetCount();printsheetCnt:sheetCvarexcelsheetexcel。getWorksheet(0);orexcel。getWorksheet(222);vartotalRowsexcelsheet。getTotalRows();vartotalClosexcelsheet。getTotalCols();printtotalRows:totalRprinttotalClos:totalCvarcellexcelsheet。getCell(A10);orgetCell(9,0);orgetCell(9,A);printcell。getString();cellexcelsheet。getCell(5,b);printcell。getInt();printcell。getString();cellexcelsheet。getCell(6,7);printcell。getDouble();printcell。getString();} 运行结果如下:sheetCnt:2totalRows:10totalClos:7aaa100100200。000000200。000000 同样的方法也可以读取。csv用Excel样式画一个机器人: 先来看一下效果 再来看一下代码importlibexcelfunctionmain(parm){varexcelnewExcel();varpathGetRoot()excelrobot。varexcelsheetexcel。addWorksheet(robot);excelsheet。setColWidth(D,15);excelsheet。setColWidth(F,3);excelsheet。setColWidth(H,15);excelsheet。setRowHeight(5,81);excelsheet。setRowHeight(6,29);headvarcellexcelsheet。addCell(E6);cell。set(。);cell。setFontBold(true);cell。setFontSize(36);cell。setAlignmentHorizontal(ALIGNMENTHORIZONTALRIGHT);cell。setAlignmentVertical(ALIGNMENTVERTICALTOP);cell。setBackgroundColor(00B050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cell。setLeftBorderStyle(BORDERSTYLETHIN);cellexcelsheet。addCell(F6);cell。set();cell。setFontBold(true);cell。setFontSize(72);cell。setAlignmentHorizontal(ALIGNMENTHORIZONTALCENTER);cell。setAlignmentVertical(ALIGNMENTVERTICALBOTTOM);cell。setBackgroundColor(00B050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cellexcelsheet。addCell(G6);cell。set(。);cell。setFontBold(true);cell。setFontSize(36);cell。setAlignmentVertical(ALIGNMENTVERTICALTOP);cell。setBackgroundColor(00B050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cell。setRightBorderStyle(BORDERSTYLETHIN);armvarcellexcelsheet。addCell(B7);cell。setBackgroundColor(92D050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cell。setLeftBorderStyle(BORDERSTYLETHIN);cellexcelsheet。addCell(C7);cell。setBackgroundColor(92D050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cell。setBottomBorderStyle(BORDERSTYLETHIN);excelsheet。mergeCells(B8:B16);cellexcelsheet。addCell(B8);cell。setBackgroundColor(92D050);cell。setLeftBorderColor(000000);cell。setLeftBorderStyle(BORDERSTYLETHIN);cell。setRightBorderStyle(BORDERSTYLETHIN);cellexcelsheet。addCell(I7);cell。setBackgroundColor(92D050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cell。setBottomBorderStyle(BORDERSTYLETHIN);cellexcelsheet。addCell(J7);cell。setBackgroundColor(92D050);cell。setTopBorderColor(000000);cell。setTopBorderStyle(BORDERSTYLETHIN);cell。setRightBorderStyle(BORDERSTYLETHIN);excelsheet。mergeCells(J8:J16);cellexcelsheet。addCell(J8);cell。setBackgroundColor(92D050);for(vari8;i16;i){cellexcelsheet。addCell(Bi);cell。setLeftBorderColor(000000);cell。setLeftBorderStyle(BORDERSTYLETHIN);cell。setRightBorderStyle(BORDERSTYLETHIN);cell。setBottomBorderStyle(BORDERSTYLETHIN);cellexcelsheet。addCell(Ji);cell。setLeftBorderColor(000000);cell。setLeftBorderStyle(BORDERSTYLETHIN);cell。setRightBorderStyle(BORDERSTYLETHIN);cell。setBottomBorderStyle(BORDERSTYLETHIN);}bodyexcelsheet。mergeCells(D7:H21);cellexcelsheet。addCell(D7);cell。set(CBrother);cell。setFontSize(72);cell。setFontName(宋体);cell。setFontColor(31869B);cell。setBackgroundColor(00B0F0);cell。setAlignmentHorizontal(ALIGNMENTHORIZONTALCENTER);cell。setAlignmentVertical(ALIGNMENTVERTICALCENTER);vararr〔D,E,F,G,H〕;for(vari7;i21;i){for(varj0;jarr。size();j){cellexcelsheet。addCell(arr〔j〕i);cell。setLeftBorderColor(000000);cell。setLeftBorderStyle(BORDERSTYLEDOUBLE);cell。setRightBorderStyle(BORDERSTYLEDOUBLE);cell。setTopBorderStyle(BORDERSTYLEDOUBLE);cell。setBottomBorderStyle(BORDERSTYLEDOUBLE);}}legexcelsheet。mergeCells(E22:E31);cellexcelsheet。addCell(E22);cell。setBackgroundColor(FFFF00);cell。setDiagonalBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setDiagonalBorderShow(true,false);excelsheet。mergeCells(G22:G31);cellexcelsheet。addCell(G22);cell。setBackgroundColor(FFFF00);cell。setDiagonalBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setDiagonalBorderShow(false,true);for(vari22;i31;i){cellexcelsheet。addCell(Ei);cell。setLeftBorderColor(000000);cell。setLeftBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setRightBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setTopBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setBottomBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cellexcelsheet。addCell(Gi);cell。setLeftBorderColor(000000);cell。setLeftBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setRightBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setTopBorderStyle(BORDERSTYLEMEDIUMDASHDOT);cell。setBottomBorderStyle(BORDERSTYLEMEDIUMDASHDOT);}excel。save(path);}