经常用到excel操作,也有几个现成的库能实现我需要的功能,但用起来总是感觉不顺手。

于是便抽了两天时间,在aaz.libxl库的基础上,按照我的使用习惯进行了修改。

以后再也不用为操作excel发愁啦。

下载地址:http://chengxu.online  →aardio资源下载→libxl.rar

解压缩将文件放于:\lib\godking\libxl

1、大名鼎鼎的libxl,封装为aardio库,便于使用。

2、为了节约劳动力,在aaz.libxl库的基础上进行修改,在此对作者表示感谢。

3、dll版本为4.0.4.0。

4、例程代码效果如下:

一、自定义数字格式

import godking.libxl;var book = godking.libxl.new("d:\custom.xls","各种数字格式")
var sheet = book.sheet();
var format = {};
var numformat = {"0.0";"0.00";"0.000";"0.0000 元";"#,###.00 $";"#,###.00 $[Black][<1000];#,###.00 $[Red][>=1000]";"合计 ##,###.00 元"
}
for(i=1;#numformat;1){format[i] = book.addFormat();format[i].numFormat = book.addNumFormat(numformat[i])// 以上两句可以合并为一句实现,如下:// format[i] = book.addFormat({ numFormat = book.addNumFormat(numformat[i]) });
}
sheet.setValue( 1, 1, 25.718, format[1] )
sheet.setValue( 2, 1, 25.718, format[2] )
sheet.setValue( 3, 1, 25.718, format[3] )
sheet.setValue( 4, 1, 25.718, format[4] )
sheet.setValue( 5, 1, 1800.5, format[5] )
sheet.setValue( 6, 1, 500   , format[6] )
sheet.setValue( 7, 1, 1600  , format[6] )sheet.setValue( 8, 1, "=SUM(A1:A7)", format[7] )
sheet.setCol( 1,1,20 )book.save()
book.release()

二、写入各种类型的数据

import godking.libxl;//新建文件
var book = godking.libxl.new("d:\example.xls");
var sheet = book.sheet();//按照指定的单元格格式写入数据
sheet.setCellStr(1, 1,  "我是字符串1");
sheet.setCellNum(2, 1,  3.14);
sheet.setCellDate(3,1,  "2022-11-17");
sheet.setCellBool(4,1,  true);
sheet.setCellBlank(5,1);//自动识别单元格格式写入数据,支持文本、数值、日期、逻辑、公式、空
sheet.setValue(1,2, "我是字符串2");
sheet.setValue(2,2, 3.1415926);
sheet.setValue(3,2, ..time("2022-12-18"));
sheet.setValue(4,2, false);
sheet.setValue(5,2);//批量获取、设置单元格内容(批量复制内容,不带格式)
var t = sheet.getValues(1,1,5,2);  // 复制(1,1)到(5,2)范围内的所有内容
sheet.setValues(1,3,t); // 粘贴到 (1,3) 单元格,向右、下一直填充到(5,4)单元格//测试清空单元格内容
sheet.setValue(1,3); //清空(1,3)单元格内容//设置列宽
sheet.setCol(1,4,12); //设置第1列到第4列的列宽为12//批量设置单元格内容
sheet.setTable(1,5,{colCount = 2; // 避免第一行的null值影响获取列数{"姓名",null}{"年龄",18}{null,`=(F2+1)&"虚岁"`}{..time("2022-10-1"),"国庆节"}{"婚否",false}});//保存文件
book.save()
book.release()

三、设置单元格字体、格式

import godking.libxl;var book = godking.libxl.new("d:\format.xls","格式演示")
var sheet = book.sheet();var font = book.addFont()
font.config = {name = "宋体",size = 18,italic = false,strikeOut = false,color = 51,bold = false,script = false,underline = false,
}var format = book.addFormat()
format.config = {alignH = 2;border = 12;borderColor = 2;font = font;fillPattern=1,patternForegroundColor=30,patternBackgroundColor=30,
}sheet.setValue( 2, 1, "格式演示Format", format )
sheet.setCol( 1, 1, 25 ) book.save( )
book.release()

四、填写收据

import godking.libxl;var book = godking.libxl.new("d:\invoice.xls","发票例程" );
var sheet = book.sheet()var boldFont = book.addFont({bold = true});var titleFont = book.addFont({name = "黑体",size = 16});var titleFormat = book.addFormat({font = titleFont,alignH = 2});var headerFormat = book.addFormat({ alignH = 2/*_ALIGNH_CENTER*/,border = 1/*_BORDERSTYLE_THIN*/,font = boldFont;fillPattern = 1 /*_FILLPATTERN_SOLID*/,patternForegroundColor = 47 /*COLOR_TAN*/});var descriptionFormat = book.addFormat({borderLeft = 1 /*BORDERSTYLE_THIN*/});var amountFormat = book.addFormat({  numFormat = 5,borderLeft = 1,borderRight = 1});var totalLabelFormat = book.addFormat({  borderTop = 1,alignH = 3,font = boldFont});var totalFormat = book.addFormat({   numFormat = 5,border = 1,font = boldFont,fillPattern = 1,patternForegroundColor = 13});var signatureFormat = book.addFormat({ alignH = 2,borderTop = 10});sheet.setMerge(2,1,2,2)
sheet.setValue(2, 1, "收款收据", titleFormat)sheet.setValue(4, 1, "姓名: 张三")
sheet.setValue(5, 1, "地址: 中国山东")
sheet.setValue(6, 1, "开票时间:"++..tostring(..time()))sheet.setValue(7, 1, "品名", headerFormat)
sheet.setValue(7, 2, "数量", headerFormat)sheet.setValue( 8, 1, "铅笔", descriptionFormat);
sheet.setValue(8, 2, 85, amountFormat);
sheet.setValue( 9, 1, "衬衫", descriptionFormat);
sheet.setValue(9, 2, 150, amountFormat);
sheet.setValue( 10, 1, "茶杯", descriptionFormat);
sheet.setValue(10, 2, 45, amountFormat);sheet.setValue( 11, 1, "合计:", totalLabelFormat);
sheet.setValue(11, 2, "=SUM(B8:B10)", totalLabelFormat);sheet.setValue(14, 2, "签名", signatureFormat);sheet.setCol( 1, 1, 40, null, 0);
sheet.setCol(2, 2, 15, , 0);book.save()
book.release()

五、与sql查询数据无缝衔接:

import console;
import sqlServer
var s = sqlServer( ["Data Source"] = "192.168.1.18,1433";["Database"] = "fang";["User ID"] = "fangs";["Password"] = "fangs1234";
)
var t = s.getTable("select * from fangs")import godking.libxl
var book = godking.libxl.new(".xls","Sheet1")
var sheet = book.sheet()
sheet.setTable(1,1,t.fields); //填充表头
// 或 sheet.setRowValue(1,1,t.fields);  //填充表头
sheet.setTable(2,1,t); //填充数据
book.save("d:\t.xls")console.pause(true);

六、筛选、排序

//筛选器例程
import godking.libxl;
// 注意:xls格式不支持筛选功能
var book = godking.libxl.new("d:\filter.xlsx","筛选器例程")
var sheet = book.sheet();sheet.setValue(2, 1,"国家");sheet.setValue(2, 2,"交通死亡率");sheet.setValue(2, 3,"吸烟死亡率");sheet.setValue(2, 4,"自杀死亡率");sheet.setValue(3, 1,"USA");      sheet.setValue(4, 1,"Greenland"); sheet.setValue(3, 2, 64);         sheet.setValue(4, 2, 94);sheet.setValue(3, 3, 69);         sheet.setValue(4, 3, 55);sheet.setValue(3, 4, 49);         sheet.setValue(4, 4, 64);sheet.setValue(5, 1,"Germany");   sheet.setValue(6, 1,"Switzerland");sheet.setValue(5, 2, 88);         sheet.setValue(6, 2, 93); sheet.setValue(5, 3, 46);         sheet.setValue(6, 3, 54);sheet.setValue(5, 4, 55);         sheet.setValue(6, 4, 50);sheet.setValue(7, 1,"Spain");     sheet.setValue(8, 1,"Gobon"); sheet.setValue(7, 2, 86);         sheet.setValue(8, 2, 75); sheet.setValue(7, 3, 47);         sheet.setValue(8, 3, 52);sheet.setValue(7, 4, 69);         sheet.setValue(8, 4, 71);sheet.setValue(9, 1,"Greece");    sheet.setValue(10, 1,"Japan");sheet.setValue(9, 2, 67);         sheet.setValue(10, 2, 91);sheet.setValue(9, 3, 23);         sheet.setValue(10, 3, 57);sheet.setValue(9, 4, 87);         sheet.setValue(10, 4, 36);var filter = sheet.filter()// G开头的国家(注意:col1和col2不能同时生效,比较遗憾)
var col1 = filter.filterColumn(1)
col1.addFilter("G*")//交通死亡率>50且<90(注意:col1和col2不能同时生效,比较遗憾)
//此处设置了col2后,col1的设置将失效。只有最后一次filterColumn设置才有效。
var col2 = filter.filterColumn(2)
col2.setCustomFilter(">",50,"<",90,"and")filter.setSort(2,true) // 按第2列逆序排序
filter.apply() // 应用筛选和排序设置book.save()
book.release()import win ; win.delay(500) ;
import process
process.execute("d:\filter.xlsx")

aardio - 【库】libxl库,一个dll操作excel相关推荐

  1. 盘点一个Pandas操作Excel多条件取值的实战案例

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 长乐钟声花外尽,龙池柳色雨中深. ...

  2. 一个C#操作Excel类,功能比较全

    using System; using System.Data; using System.Configuration; using System.Web; using Microsoft.Offic ...

  3. 如何用Python操作Excel自动化办公?一个案例教会你openpyxl——读取数据

    欢迎大家关注我,我是拾陆,关注同名"二八Data" 数据分析工作最难搞的是处理数据的过程,不然不会有专门的ETL(数据抽取.转换.加载)工程师了.如果是企业级数据处理可能数据库直接 ...

  4. C++操作Excel学习笔记

    C++操作Excel学习笔记 一: [当前博文转载自http://blog.csdn.net/fullsail/article/details/4067416] C++读取Excel文件方式比较 C+ ...

  5. excel进度条与百分比不符_用Python操作Excel数据

    今天给大家展示一个python操作Excel的小demo,demo的例子比较简单,如果大家有什么建议,欢迎可以直接在函数君的微信后台留言. 首先,我们需要安装python在我们的电脑上. 安装的步骤如 ...

  6. golang操作excel两种excelize包对比

    Go 语言是一门适合用于编写高效且并发的 Web 应用程序的编程语言,同时也可以使用它进行数据处理和分析.在数据处理和分析过程中,Excel 是一种常用的电子表格软件,很多情况下需要将数据导入到 Ex ...

  7. Qt动态库静态库的创建、使用、多级库依赖、动态库改成静态库等详细说明

    本文描述的是windows系统下,通过qtcreator在pro文件中添加动态库与静态库的方法: 1.添加动态库(直接添加动态库文件.dll,非子项目) 通过qtcreator创建动态库的方法就不在此 ...

  8. python(进阶篇)——自动化操作Excel(xlrd和xlwt)

    活动地址:CSDN21天学习挑战赛 学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩: 虽然永远无法预料明天是晴还是雨, 也无法预知你在乎的人是否还在身旁, 以及你一直以来的坚持究竟能否换来什么. ...

  9. php 操作xls,php中使用PHPExcel操作excel(xls)文件

    PHPExcel是php的一个插件,它可以实现读取excel文件也就是xls文件了,下面我们就来看一个PHPExcel操作excel(xls)文件例子,希望能帮助到各位. 读取中文的xls.csv文件 ...

  10. winfrom 操作Excel

    利用 Aspose.Cells.dll 操作Excel,内容如下: 1.界面设计: 2.逻辑: using System; using System.Collections.Generic; usin ...

最新文章

  1. mongodb 与 mysql区别 NOSQL 型号与SQL型号的区别 是非关系型号与关系型号的区别
  2. 虚拟机忘记密码解决方法
  3. Cortana小娜:城市信息提醒
  4. 在现有K8S集群上安装部署JenkinsX
  5. jenkins pipeline发送邮件报错解决办法: failed to connect, no password specified?
  6. rabbitmq 取消消息_SpringBoot整合RabbitMQ实现延迟消息
  7. 字符内存转成字符串_字符串内存内部
  8. 腾讯TBS加载网页无法自适应记录
  9. android小记之FTP文件上传
  10. 【C语言】输入一个字符串,统计其中的单词个数,将第一个单词的首字母改为大写,并输出改写后的字符串...
  11. 前端工程师需要懂的前端面试题(c s s方面)总结(二)
  12. 浅谈C++的智能指针
  13. 电脑安装ubuntu linux操作系统
  14. Excel文件导入web页面
  15. 利用matlab来进行路径规划,matlab路径规划系列
  16. FE - Vue 使用 XLSL 导出 excel 文件
  17. sql超键 候选键 主键
  18. 【http-flv】zlmedia http 客户端拉取 http-flv 流程
  19. Python数据类型练习题
  20. PYCharm 5 注册码破解

热门文章

  1. Vue Devtools下载使用
  2. 前端模板引擎 -- Freemarker
  3. PayPal接口开发
  4. 数据挖掘:Apriori 关联规则分析算法原理分析与代码实现
  5. 群晖 Docker Gitlab 安装 及 https配置
  6. 【雷达与对抗】【2015】【部分源码】用于雷达成像与信道探测的FMCW信号
  7. Python 中模拟键盘输入
  8. java定义矩形的周长和面积_定义一个长方形类,定义 求周长和面积的方法实例
  9. Hexo | yilia主题安装
  10. JavaScript 每日一题---LeetCode 2.两数相加