(转)libxl操作excel文件aardio库和调用示例
By
admin
at 2021-02-18 • 0人收藏 • 2318人看过
感谢: fish(290xxxx037) 分享
https://github.com/btx638/LibXL-aardio
说到这个libxl ,之前的文章 https://www.chengxu.xyz/t/342 里面 /zgmf-x-20a/ 也对 libxl.dll 进行过封装 , 他的Git工程代码: https://github.com/zgmf-x-20a/libxl
针对这两个封装, 可以自己去试一试, 哪个顺手用哪个,
这里分享的库智能提示还不是很完善, 需要自己去查看文档相互对照.
对于我们这些使用者来说总是不会嫌弃库太多,O(∩_∩)O哈哈~
下面看官们自己对比下他们两个的封装 , 顺道还能学习下库是怎么封装的吧
aaz.libxl库文件:
使用示例:
这里帖两个使用示例:
1.格式化文本样式:
import aaz.libxl; var book = aaz.libxl.createBook() book.setKey( ) var f = {}; var format = {}; var customNumFormats = { "0.0"; "0.00"; "0.000"; "0.0000"; "#,###.00 $"; "#,###.00 $[Black][<1000];#,###.00 $[Red][>=1000]" } for(i=1;#customNumFormats;1){ f[i] = book.addCustomNumFormat(customNumFormats[i]) } for(i=1;#customNumFormats;1){ format[i] = book.addFormat() format[i].numFormat = f[i] } var sheet = book.addSheet( "Custom formats" ) sheet.setCol( 0, 0, 20, null, 0 ) sheet.writeNum( 2, 0, 25.718, format[1] ) sheet.writeNum( 3, 0, 25.718, format[2] ) sheet.writeNum( 4, 0, 25.718, format[3] ) sheet.writeNum( 5, 0, 25.718, format[4] ) sheet.writeNum( 7, 0, 1800.5, format[5] ) sheet.writeNum( 9, 0, 500, format[6] ) sheet.writeNum( 10, 0, 1600, format[6] ) book.save( "\custom.xls" ) book.release()
2. 单元格格式
import aaz.libxl; var book = aaz.libxl.createBook() book.setKey() var font = book.addFont() font.setName("Impact").setSize(36) var format = book.addFormat() format.config = { alignH = 2; border = 12; borderColor = 2; font = font; } var sheet = book.addSheet( "Custom" ) sheet.writeStr( 2, 1, "Format", format ) sheet.setCol( 1, 1, 25 ) book.save( "\format.xls" ) book.release() execute("pause")
3.公式
import aaz.libxl; var book = aaz.libxl.createXMLBook(); book.setKey( ) var boldFont = book.addFont(0); boldFont.bold = 1; var titleFont = book.addFont(0); titleFont.name = "Arial Black" titleFont.size = 16; var titleFormat = book.addFormat(); titleFormat.font = titleFont; var headerFormat = book.addFormat(); headerFormat.alignH = 2/*_ALIGNH_CENTER*/ headerFormat.border = 1/*_BORDERSTYLE_THIN*/ headerFormat.font = boldFont; headerFormat.fillPattern = 1 /*_FILLPATTERN_SOLID*/ headerFormat.patternForegroundColor = 47 /*COLOR_TAN*/ var descriptionFormat = book.addFormat(); descriptionFormat.borderLeft = 1 /*BORDERSTYLE_THIN*/ var amountFormat = book.addFormat(); amountFormat.numFormat = 5 amountFormat.borderLeft = 1 amountFormat.borderRight = 1 var totalLabelFormat = book.addFormat(); totalLabelFormat.borderTop = 1 totalLabelFormat.alignH = 3 totalLabelFormat.font = boldFont var totalFormat = book.addFormat(); totalFormat.numFormat = 5 totalFormat.border = 1 totalFormat.font = boldFont totalFormat.fillPattern = 1 totalFormat.patternForegroundColor = 13 var signatureFormat = book.addFormat(); signatureFormat.alignH = 2 signatureFormat.borderTop = 1 var sheet = book.addSheet( "Invoice" ) sheet.writeStr(2, 1, "Invoice No. 3568", titleFormat) sheet.writeStr(4, 1, "Name: John Smith") sheet.writeStr(5, 1, "Address: San Ramon, CA 94583 USA") sheet.writeStr(7, 1, "Description", headerFormat) sheet.writeStr(7, 2, "Amount", headerFormat) sheet.writeStr( 8, 1, "Ball-Point Pens", descriptionFormat); sheet.writeNum(8, 2, 85, amountFormat); sheet.writeStr( 9, 1, "T-Shirts", descriptionFormat); sheet.writeNum(9, 2, 150, amountFormat); sheet.writeStr( 10, 1, "Tea cups", descriptionFormat); sheet.writeNum(10, 2, 45, amountFormat); sheet.writeStr( 11, 1, "Total:", totalLabelFormat); sheet.writeFormula(11, 2, "=SUM(C9:C11)", totalFormat); sheet.writeStr(14, 2, "Signature", signatureFormat); sheet.setCol( 1, 1, 40, null, 0); sheet.setCol(2, 2, 15, , 0); book.save("\invoice.xlsx") book.release()
4.数据读取
io.open() import aaz.libxl; var book = aaz.libxl.createBook(); book.setKey() book.load("\example.xls") var sheet = book.getSheet(0) if( sheet ){ var s = sheet.readStr( 2, 1 ) io.print(s) var d = sheet.readNum( 3, 1 ) io.print( d ) var s = sheet.readStr( 4, 1 ) io.print(s) } book.release() execute("pause")
1 个回复 | 最后更新于 2021-03-24
登录后方可回帖
很赞,感谢分享~