项目中需要将未上传的数据导出到excel,供用户可见。不同的模块传入数据类型不一样。所以用了List<ArrayList>将数据封装成二维string类型。效率还挺高

build.gradle中引入jxl库。

    // https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxlimplementation 'net.sourceforge.jexcelapi:jxl:2.6.12'

先上效果图在这里插入图片描述

工具类如下

/*** 创建时间:2020-12-25* 编写人:kanghb* 功能描述:将数据导出excel 经测试4万条学生数据一秒左右完成*/
public class ExcelUtil {private static WritableFont arial14font = null;private static WritableCellFormat arial14format = null;private static WritableFont arial10font = null;private static WritableCellFormat arial10format = null;private static WritableFont arial12font = null;private static WritableCellFormat arial12format = null;private final static String UTF8_ENCODING = "UTF-8";/*** 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...*/private static void format() {try {arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);arial14format = new WritableCellFormat(arial14font);arial14format.setAlignment(jxl.format.Alignment.CENTRE);arial14format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);arial10format = new WritableCellFormat(arial10font);arial10format.setAlignment(jxl.format.Alignment.CENTRE);arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);arial10format.setBackground(Colour.GRAY_25);arial12font = new WritableFont(WritableFont.ARIAL, 12);arial12format = new WritableCellFormat(arial12font);//对齐格式arial12format.setAlignment(jxl.format.Alignment.CENTRE);//设置边框arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);} catch (WriteException e) {e.printStackTrace();}}/*** 初始化Excel表格** @param filePath  存放excel文件的路径(MPCMS/Export/demo.xls)* @param sheetName Excel表格的表名* @param colName   excel中包含的列名(可以有多个)*/public static void initExcel(String filePath, String sheetName, String[] colName) {format();WritableWorkbook workbook = null;try {File file = new File(FileUtilsCore.SDPATH + "/Export");makeDir(file);File saveFile = new File(file, filePath);if (!saveFile.exists()) {saveFile.createNewFile();}workbook = Workbook.createWorkbook(saveFile);//设置表格的名字WritableSheet sheet = workbook.createSheet(sheetName, 0);//创建标题栏sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));for (int col = 0; col < colName.length; col++) {sheet.addCell(new Label(col, 0, colName[col], arial10format));}//设置行高sheet.setRowView(0, 340);workbook.write();} catch (Exception e) {e.printStackTrace();} finally {if (workbook != null) {try {workbook.close();} catch (Exception e) {e.printStackTrace();}}}}/*** 将制定类型的List写入Excel中** @param objList  待写入的list,* @param fileName 文件名:用户名_模块_任务名_时间* @param c*/@SuppressWarnings("unchecked")public static void writeObjListToExcel(List<ArrayList<String>> objList, String fileName, Context c) {if (objList != null && objList.size() > 0) {WritableWorkbook writebook = null;InputStream in = null;try {WorkbookSettings setEncode = new WorkbookSettings();setEncode.setEncoding(UTF8_ENCODING);File file = new File(FileUtilsCore.SDPATH + "/Export");makeDir(file);File saveFile = new File(file, fileName);if (!saveFile.exists()) {saveFile.createNewFile();}in = new FileInputStream(saveFile);Workbook workbook = Workbook.getWorkbook(in);writebook = Workbook.createWorkbook(saveFile, workbook);WritableSheet sheet = writebook.getSheet(0);for (int j = 0; j < objList.size(); j++) {ArrayList<String> list = (ArrayList<String>) objList.get(j);for (int i = 0; i < list.size(); i++) {sheet.addCell(new Label(i, j + 1, list.get(i), arial12format));if (list.get(i).length() <= 5) {//设置列宽sheet.setColumnView(i, list.get(i).length() + 8);} else {//设置列宽sheet.setColumnView(i, list.get(i).length() + 5);}}//设置行高sheet.setRowView(j + 1, 350);}writebook.write();Toast.makeText(c, "导出Excel成功", Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();} finally {if (writebook != null) {try {writebook.close();} catch (Exception e) {e.printStackTrace();}}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}}public static void makeDir(File dir) {if (!dir.getParentFile().exists()) {makeDir(dir.getParentFile());}dir.mkdir();}}

调用代码

                String[] title = {"母本名称", "母本小区", "父本名称", "父本小区", "母本珠号", "父本珠号", "授粉数量", "单位", "时间"};String fileName = App.getInstance().getUserName() + "_" + "授粉" + "_" + crossTaskName + "_" + System.currentTimeMillis() + ".xls";ExcelUtil.initExcel(fileName, "授粉记录", title);ExcelUtil.writeObjListToExcel(recordList, fileName, this);

Android将数据导出为excel文件的方法相关推荐

  1. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件--select MID,CODE,NAME from Dxc_Goods_Cate where ...

  2. python 导出大量数据到excel_怎么在python中将大量数据导出到Excel文件

    怎么在python中将大量数据导出到Excel文件 发布时间:2021-01-16 11:09:40 来源:亿速云 阅读:76 作者:Leah 这期内容当中小编将会给大家带来有关怎么在python中将 ...

  3. js导出的xlsx无法打开_vue将数据导出为excel文件就是如此简单

    前言: 在以前需要将数据导出为excel文件时,往往需要后端提供支持,后端导出后以文件流的形式进行下载.但也带了一些问题,如: 1. 代码量复杂,一般前端难以完成. 2. 实现数据导出往往会大量消耗服 ...

  4. php mysql 导出到excel,php mysql数据导出到excel文件

    这篇文章主要为大家详细介绍了php mysql数据导出到excel文件,具有一定的参考价值,可以用来参考一下. 感兴趣的小伙伴,下面一起跟随512笔记的小编罗X来看看吧. 经测试代码如下: /** * ...

  5. dbeaver导出excel文件_vue将数据导出为excel文件就是如此简单

    前言: 在以前需要将数据导出为excel文件时,往往需要后端提供支持,后端导出后以文件流的形式进行下载.但也带了一些问题,如: 1. 代码量复杂,一般前端难以完成. 2. 实现数据导出往往会大量消耗服 ...

  6. Vue将echarts数据导出成excel文件

    Vue将echarts数据导出成excel文件 一.下载vendor插件 下载 vendor 文件放置项目的 src 目录下 链接:https://pan.baidu.com/s/1XYYQ186zo ...

  7. 数据导出到excel文件给客户端下载的几种方法

    (原创)datagrid数据导出到excel文件给客户端下载的几种方法 看到这个觉得这个人很厉害,mvp啊. 我就改成这个样子了.  Response.Clear();         Respons ...

  8. Matlab GUI界面表格中数据导出到excel文件带标题

    Matlab GUI界面表格中的数据导出到excel文件带标题 data=get(handles.uitable1,'Data'); ht={'日期','编号','姓名'}; [filename,pa ...

  9. 编写Python脚本调用企查查股东信息接口将数据导出为excel文件

    此文章已同步更新至我的个人博客https://simonting.gitee.io 前言 调用企查查的API获取一万多家公司股东信息数据导出为excel文件. 1.设置IP白名单 IP获取建议使用百度 ...

  10. vue导出excel加一个进度条_vue项目中如何把数据导出成excel文件

    Loading... 关于vue中如何把数据导出成excel文件 ### 1. 安装依赖 ``` yarn add file-saver yarn add xlsx yarn add script-l ...

最新文章

  1. ViewPager 的适配器 PagerAdapter 、FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
  2. Windows单机配置Kafka环境
  3. Jexus支持HTTPS协议
  4. idea中每次push/pull都需要输入账号密码
  5. Python raw_input 函数 - Python零基础入门教程
  6. redis数据库及与python交互
  7. python小游戏之一
  8. 从Log4j迁移到LogBack的理由
  9. 计算机网络题库——第3章数据链路层
  10. Flutter如何强制竖屏
  11. 计算机系古生物学,超级计算机模拟生命起源
  12. 《人性的弱点》阅读摘抄-1
  13. 在Android开发当中如何在一个app中打开淘宝app
  14. 爬取某电影网站(未写完)
  15. 中国半导体设备行业需求状况与投资前景展望分析报告2022版
  16. 普元eos使用svn_普元DevOps介绍
  17. PPT个人学习笔记(四)——神奇移动的实现
  18. 【Mac】如何直接屏幕截图复制到剪切板
  19. 大咖专访:同Bigbang公链创始人“一休”一起见证区块链技术的缘起时刻!
  20. Java工程师主要是负责什么 岗位职责都有哪些

热门文章

  1. ios7下弹出新浪微博界面,一出现就消失的问题
  2. c语言学籍信息系统,c语言学籍信息管理系统设计
  3. 互斥事件的概念和公式_独立事件与互斥事件的区别与联系公式
  4. 微信朋友圈会在服务器上保留多久,微信朋友圈有没有草稿?可以存多久?
  5. 20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
  6. 打印没反应/打印耗时长/not accessible
  7. 交换机接出来的网线可以再接上无线路由器实现无线上网吗
  8. 贝叶斯新闻分类实战项目详解
  9. 高考为什么考计算机信息,信息技术是否应该进入高考?
  10. 上海大学计算机专业就业薪资,人均月薪过万?985、211、普通大学毕业生薪资水平大起底!和你想的不一样……...