当前位置: 首页 > news >正文

android JXL 导出Excel(.xls/xlsx)

前面使用过 POI 导出xlsx但是它体量比较大,功能较丰富,在一些对包size比较敏感并且导出需求相对简单的项目中就不太适合。

poi链接:Android 导入导出excel xls、xlsx_android excel导入导出-CSDN博客

jxl 包体积小,使用简单、API 直观。

1.导入依赖

    implementation("net.sourceforge.jexcelapi:jxl:2.6.12")

2.导出方式,可根据自身需求修改

 public static boolean exportExcel(Context context, List<Contact> contactList) {try {File dir = context.getExternalFilesDir(null);File file = new File(dir, "联系人.xlsx");if (file.exists()) {file.delete();}WritableWorkbook workbook = Workbook.createWorkbook(file);WritableSheet sheet = workbook.createSheet("联系人", 0);// 设置列宽(单位:字符数) 可根据实际情况调整sheet.setColumnView(0, 10); // 姓名sheet.setColumnView(1, 20); // 组织sheet.setColumnView(2, 15); // 个人电话sheet.setColumnView(3, 15); // 家庭电话sheet.setColumnView(4, 15); // 工作电话sheet.setColumnView(5, 30); // 邮箱sheet.setColumnView(6, 40); // 备注// 写入表头sheet.addCell(new Label(0, 0, "姓名"));sheet.addCell(new Label(1, 0, "组织"));sheet.addCell(new Label(2, 0, "个人电话"));sheet.addCell(new Label(3, 0, "家庭电话"));sheet.addCell(new Label(4, 0, "工作电话"));sheet.addCell(new Label(5, 0, "邮箱"));sheet.addCell(new Label(6, 0, "备注"));// 写入数据for (int i = 0; i < contactList.size(); i++) {Contact c = contactList.get(i);sheet.addCell(new Label(0, i + 1, c.getName()));sheet.addCell(new Label(1, i + 1, c.getOrg()));sheet.addCell(new Label(2, i + 1, c.getPhone()));sheet.addCell(new Label(3, i + 1, c.getHomePhone()));sheet.addCell(new Label(4, i + 1, c.getWorkPhone()));sheet.addCell(new Label(5, i + 1, c.getEmail()));sheet.addCell(new Label(6, i + 1, c.getNote()));}workbook.write();workbook.close();Log.d("导出文件", "导出成功  path:" + file.getPath());return true;} catch (Exception e) {e.printStackTrace();}return false;}public static class Contact {private String Name;private String phone;private String email;private String org;private String note;private String homePhone;private String workPhone;get set 省略}

3.使用

 new Thread(new Runnable() {@Overridepublic void run() {List<ExportFile.Contact> contacts = new ArrayList<>();for (int i = 0; i < 10; i++) {ExportFile.Contact contact = new ExportFile.Contact();contact.setName("刘二毛" + i);contact.setPhone("1234567890" + i);contact.setHomePhone("0395-550999" + i);contact.setWorkPhone("0397-999999" + i);contact.setEmail("liuermao666@Gmail.eslssdkj.com");contact.setNote("备注" + i);contact.setOrg("组织结构" + i);contacts.add(contact);}boolean result = ExportFile.exportExcel(ActivityLianXiRen.this, contacts);}}).start();

http://www.dtcms.com/a/298068.html

相关文章:

  • 解决企业微信收集表没有图片、文件组件,不能收集图片的问题
  • windows 安排 openssl
  • 三、操作系统——第1章:计算机系统概述
  • 星痕共鸣 C++显示打出的攻击力
  • 【前端工程化】前端项目开发过程中如何做好通知管理?
  • AVL树和红黑树的特性以及模拟实现
  • 【CMake】CMake 与 C++ 协同:条件配置机制及控制台控制实例解析
  • [C++]string::substr
  • MindJourney:构建空间智能的新范式——VLM与视频扩散式世界模型的融合
  • 【LeetCode Solutions】LeetCode 热题 100 题解(16 ~ 20)
  • 【牛客网C语言刷题合集】(三)
  • 2025年-ClickHouse 高性能实时分析数据库(大纲版)
  • 【开发杂谈】用AI玩AI聊天游戏:使用 Electron 和 Python 开发大模型语音聊天软件
  • 如何搭建Linux环境下的flink本地集群
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-26,(知识点:硬件电路的调试方法:信号追踪,替换,分段调试)
  • 飞算 JavaAI “撤回接口信息” 功能:误删接口不用慌,一键恢复更省心
  • Linux 设备驱动模型
  • WINDOWS10系统重装软件篇
  • QML图形效果之阴影效果(DropShadow与InnerShadow)
  • Cacti命令执行漏洞分析(CVE-2022-46169)
  • compileSdkVersion和targetSdkVersion可以不一样的版本吗
  • 图论:并查集
  • 深入解析JVM垃圾回收调优:性能优化实践指南
  • Python 数据可视化之 Matplotlib 库
  • Java常用命令、JVM常用命令
  • RAG面试内容整理-3. 向量检索原理与常用库(ANN、FAISS、Milvus 等)
  • blender基本操作
  • flutter TextField 失去焦点事件
  • Qt:qRegisterMetaType函数使用介绍
  • 安全风险监测平台:被动应对向主动预防的转变