(转)通过libxl库读写excel的XLS和XLSX文件
By
admin
at 2019-12-14 • 2人收藏 • 4115人看过
LibXL可以不依赖EXCEL读取XLS和XLSX文件。
包括设置对齐方式、字体、颜色等格式
通过上面的介绍可以看出: 利用这个库可以不需要电脑上安装excel软件
感谢aardio交流群: /zgmf-x-20a/ 对 libxl.dll 文件的封装
Git工程代码: https://github.com/zgmf-x-20a/libxl
工程本地下载:
dll文件下载:
注意: 库里未提供注册号, 生成的文件头部会自动添加 试用版 提示.(如下下图)
如果需要去掉此提示, 请去官方申请注册码.
下面贴出使用代码示例中的一个: (其他示例,请下载源码查看)
import libxl; import console; var autofilter_byValue = function(){ io.open() var start = time.tick() var file = "export/autofilter_byValue.xlsx" var xlExcel,err = libxl() var xlSheet = xlExcel.addSheet("my"); if xlSheet{ xlSheet.Cell(3,2).value = "Country"; xlSheet.Cell(3,3).value = "Road injures"; xlSheet.Cell(3,4).value = "Smoking"; xlSheet.Cell(3,5).value = "Suicide"; xlSheet.Cell(4, 2).value ="USA"; xlSheet.Cell(5, 2).value ="UK"; xlSheet.Cell(4, 3).value = 64; xlSheet.Cell(5, 3).value =94; xlSheet.Cell(4, 4).value = 69; xlSheet.Cell(5, 4).value =55; xlSheet.Cell(4, 5).value = 49; xlSheet.Cell(5, 5).value = 64; xlSheet.Cell(6, 2).value ="Germany"; xlSheet.Cell(7, 2).value ="Switzerland"; xlSheet.Cell(6, 3).value =88; xlSheet.Cell(7, 3).value = 93; xlSheet.Cell(6, 4).value =46; xlSheet.Cell(7, 4).value = 54; xlSheet.Cell(6, 5).value =55; xlSheet.Cell(7, 5).value = 50; xlSheet.Cell(8, 2).value ="Spain"; xlSheet.Cell(9, 2).value ="Italy"; xlSheet.Cell(8, 3).value = 86; xlSheet.Cell(9, 3).value = 75; xlSheet.Cell(8, 4).value = 47; xlSheet.Cell(9, 4).value = 52; xlSheet.Cell(8, 5).value = 69; xlSheet.Cell(9, 5).value = 71; xlSheet.Cell(10, 2).value ="Greece"; xlSheet.Cell(11, 2).value ="Japan"; xlSheet.Cell(10, 3).value =67; xlSheet.Cell(11, 3).value =91; xlSheet.Cell(10, 4).value =23; xlSheet.Cell(11, 4).value =57; xlSheet.Cell(10, 5).value =87; xlSheet.Cell(11, 5).value =36; var xlAutoFilter = xlSheet.autoFilter(); xlAutoFilter.setRef("B3E11"); var xlFilterColumn = xlAutoFilter.getColumn(1); xlFilterColumn.filterText = "Japan" xlFilterColumn.filterText = "USA" xlFilterColumn.filterText = "Switzerland" xlSheet.applyFilter(); io.print(xlExcel.Save(file)) io.print(xlExcel.getErrMsg()) xlExcel.Close() io.print("use " + (time.tick() - start + " ms")); } }; autofilter_byValue() console.pause(true);
登录后方可回帖