在Android开发中有时候我们需要将数据直接生成Excel表格并导出,

方法如下:
1.下载jxl-2.6.12.jar包:放置到Android项目的libs文件夹下,并引用依赖
2.将自己要存入表格的数据,抽象成一个实体类,例如:FaceTestEntity
3.调用jxl这个jar包的Api,生成表格,设置样式,写入数据,导出表格。

我将生成表格的代码封装成了工具类,需要的同学可以拿到直接使用,或根据自身需求修改样式参数:


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);arial10font.setColour(Colour.DARK_BLUE);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, 10);//设置字体颜色arial12format = new WritableCellFormat(arial12font);//对齐格式arial12font.setColour(Colour.BLACK);arial12font.setPointSize(10);//设置边框arial12format.setBorder(Border.ALL, BorderLineStyle.THIN);arial12format.setAlignment(jxl.format.Alignment.CENTRE);} catch (WriteException e) {e.printStackTrace();}}public static WritableCellFormat getHeader() {WritableFont font = new WritableFont(WritableFont.TIMES, 12,WritableFont.BOLD);// 定义字体try {font.setColour(Colour.BLUE);// 蓝色字体} catch (WriteException e1) {e1.printStackTrace();}WritableCellFormat format = new WritableCellFormat(font);try {format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中format.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);// 黑色边框format.setBackground(Colour.GREEN);// 黄色背景} catch (WriteException e) {e.printStackTrace();}return format;}public static <T> void setSheetHeader(Context context, String fileName, String[] colName,String sheetName, List<T> objList) {WritableWorkbook workbook = null;try {File file = new File(fileName);if (!file.exists()) {file.createNewFile();}WorkbookSettings workbookSettings = new WorkbookSettings();workbookSettings.setEncoding("UTF-8");workbookSettings.setFormulaAdjust(true);workbookSettings.setRefreshAll(true);// 创建Excel工作表OutputStream os = new FileOutputStream(file);workbook = Workbook.createWorkbook(os);//设置表格的名字WritableSheet sheet = workbook.createSheet(sheetName, 0);Label label;for (int i = 0; i < colName.length; i++) {// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z// 在Label对象的子对象中指明单元格的位置和内容label = new Label(i, 0, colName[i], getHeader());// 将定义好的单元格添加到工作表中sheet.addCell(label);}for (int i = 0; i < objList.size(); i++) {FaceTestEntity faceTestEntity = (FaceTestEntity) objList.get(i);Label name = new Label(0, i + 1, faceTestEntity.getName());Label trackId = new Label(1, i + 1, String.valueOf(faceTestEntity.getTrackId()));Label liveNess = new Label(2, i + 1, String.valueOf(faceTestEntity.isLiveness()));Label rgbTime = new Label(3, i + 1, String.valueOf(faceTestEntity.getRGB_Tiem()));Label rgbIrTime = new Label(4, i + 1, String.valueOf(faceTestEntity.getRGB_IR_Time()));Label compareTime = new Label(5, i + 1, String.valueOf(faceTestEntity.getCompare_time()));Label compareSimilar = new Label(6, i + 1, String.valueOf(faceTestEntity.getCompareSimilar()));sheet.addCell(name);sheet.addCell(trackId);sheet.addCell(liveNess);sheet.addCell(rgbTime);sheet.addCell(rgbIrTime);sheet.addCell(compareTime);sheet.addCell(compareSimilar);}ToastUtils.showShortToast(context, "写入成功");sheet.setColumnView(0, 350);workbook.write();} catch (IOException e) {e.printStackTrace();} catch (RowsExceededException e) {e.printStackTrace();} catch (WriteException e) {e.printStackTrace();} finally {try {workbook.close();} catch (IOException e) {e.printStackTrace();} catch (WriteException e) {e.printStackTrace();}}}/*** 初始化Excel** @param fileName 导出excel存放的地址(目录)* @param colName  excel中包含的列名(可以有多个)*/public static void initExcel(String fileName, String sheetName, String[] colName) {format();WritableWorkbook workbook = null;try {File file = new File(fileName);if (!file.exists()) {file.createNewFile();}WorkbookSettings workbookSettings = new WorkbookSettings();workbookSettings.setEncoding("UTF-8");workbookSettings.setFormulaAdjust(true);workbookSettings.setRefreshAll(true);FileOutputStream os = new FileOutputStream(file);workbook = Workbook.createWorkbook(os);//设置表格的名字WritableSheet sheet = workbook.createSheet(sheetName, 0);//创建标题栏sheet.addCell((WritableCell) new Label(0, 0, "这是标题栏", arial14format));for (int col = 0; col < colName.length; col++) {// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z// 在Label对象的子对象中指明单元格的位置和内容sheet.addCell(new Label(col, 1, colName[col], /*getHeader()*/arial10format));sheet.setColumnView(0, colName[col].length() * 8);}//设置行高sheet.setRowView(0, 340);workbook.write();} catch (Exception e) {e.printStackTrace();} finally {if (workbook != null) {try {workbook.close();} catch (Exception e) {e.printStackTrace();}}}}/*** 写入实体类数据到Excel文件*/@SuppressWarnings("unchecked")public static <T> void writeObjListToExcel(List<T> objList, String fileName, Context c) {if (objList != null && objList.size() > 0) {WritableWorkbook writebook = null;InputStream in = null;try {in = new FileInputStream(new File(fileName));Workbook workbook = Workbook.getWorkbook(in);writebook = Workbook.createWorkbook(new File(fileName), workbook);WritableSheet sheet = writebook.getSheet(0);if (objList.size() > 0) {for (int j = 0; j < objList.size(); j++) {//要写入表格的数据对象FaceTestEntity faceTestEntity = (FaceTestEntity) objList.get(j);List<String> list = new ArrayList<>();list.add(faceTestEntity.getName());list.add(String.valueOf(faceTestEntity.getTrackId()));list.add(String.valueOf(faceTestEntity.isLiveness()));list.add(String.valueOf(faceTestEntity.getRGB_Tiem()));list.add(String.valueOf(faceTestEntity.getRGB_IR_Time()));list.add(String.valueOf(faceTestEntity.getCompare_time()));list.add(String.valueOf(faceTestEntity.getCompareSimilar()));for (int i = 0; i < list.size(); i++) {sheet.addCell(new Label(i, j + 2, list.get(i), arial12format));if (list.get(i).length() <= 4) {//设置列宽 ,第一个参数是列的索引,第二个参数是列宽sheet.setColumnView(i, list.get(i).length() * 7);} else {//设置列宽sheet.setColumnView(i, list.get(i).length() * 4);}}//设置行高,第一个参数是行数,第二个参数是行高sheet.setRowView(j, 340);}}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();}}}}}
}

使用方法,记得使用前申请文件读写权限

private void excelExecute() {String dirPath = Environment.getExternalStorageDirectory() + File.separator + "AndroidExcel";File file = new File(dirPath);if (!file.exists())file.mkdirs();String testExcelPath = dirPath + File.separator + "testExcel.xls";List<FaceTestEntity> list = new ArrayList<>();FaceTestEntity faceTestEntity1 = new FaceTestEntity("小狗", 1,false, 123, 234, 234,0.92);FaceTestEntity faceTestEntity2 = new FaceTestEntity("大熊", 2,false, 34, 355, 243,0.94);FaceTestEntity faceTestEntity3 = new FaceTestEntity("黑虎", 3,false, 24, 56, 356,0.90);FaceTestEntity faceTestEntity4 = new FaceTestEntity("滤后", 4,false, 25, 46, 234,0.27);FaceTestEntity faceTestEntity5 = new FaceTestEntity("祖居", 5,false, 25, 256, 234,0.46);FaceTestEntity faceTestEntity6 = new FaceTestEntity("哆啦", 6,false, 47, 47, 267,0.96);FaceTestEntity faceTestEntity7 = new FaceTestEntity("钱出", 7,false, 256, 245, 256,0.95);FaceTestEntity faceTestEntity8 = new FaceTestEntity("尽在", 8,false, 256, 256, 234,0.99);list.add(faceTestEntity1);list.add(faceTestEntity2);list.add(faceTestEntity3);list.add(faceTestEntity4);list.add(faceTestEntity5);list.add(faceTestEntity6);list.add(faceTestEntity7);list.add(faceTestEntity8);String[] colName = {"name", "trackId", "isLiveness", "RGB_Tiem","RGB_IR_Time", "compare_time", "compareSimilar"};ProgressDialog progressDialog = new ProgressDialog(this);progressDialog.setMessage("表格创建中...");progressDialog.show();ExcelUtil.initExcel(testExcelPath, "测试表1", colName);
//        ExcelUtil.setSheetHeader(this, testExcelPath, colName, "测试表1", list);ExcelUtil.writeObjListToExcel(list, testExcelPath, this);progressDialog.dismiss();}

Android创建Excel表格相关推荐

  1. 如何通过VB合并Excel单元格以及设置Excel行高?VB创建Excel表格,合并单元格,生成图形等操作

    如何通过VB合并Excel单元格以及设置Excel行高? 例如:我想把第一列的第4,5,6,7行合并...我在怎样让合并单元格里的字居中,怎样改变字体. 请不吝赐教... ============== ...

  2. ODBC读写创建excel表格

    一.前言 在工作中,我们经常需要处理各种各样的excel表格.然而,巨大的数据量处理起来十分消耗时间,这种费时费力的活应该交给电脑来做.下面将讲解如何通过C++工程来读写Excel表格并生成新的表格. ...

  3. Android实现Excel表格样式

    原理描述: 想写来着,但是似乎描述不太清楚.效果图来着,没找到好的视频录制软件,直接上代码吧. 代码: 1.xml布局文件 自定义控件的包名删除了部分,需要重新导入自定义控件!!! <?xml ...

  4. 如何在Python中创建Excel表格

    之前在学习os模块中,我们知道了如何创建一个txt格式的文件(具体操作见https://mp.csdn.net/postedit/80903024) 但是当我们爬取一些小说或一些图片时,我们需要分类管 ...

  5. 实习笔记 C#创建excel表格并且学会使用NPOI

    前言  在创建之初我们先区分一下 excel表格文件,区别于两种格式,一种是xls,一种是xlsx格式其中两者的区别:.xls格式单个工作表最多支持65536行,256列..xlsx格式最多支持104 ...

  6. android 输出xlsx文件格式,Android导出Excel表格文件

    1.首先再Android添加jar包:jxl-2.6.12.jar 2.创建javaBean类,用于存储需要写入表格中的数据 public class DemoBean { private Strin ...

  7. python创建excel表格_python使用VBA:Excel创建图表(转)

    # -*- coding: utf-8 -*- """ Created on Thu Mar 06 11:22:03 2014 @author: Administrato ...

  8. android excel 筛选功能,Android实现Excel表格展示数据

    前言 在Android开发过程中,我们偶尔会需要将一些数据以Excel表格展示.那么今天就让我们来学习下Excel表格展示相关内容吧 今天涉及的内容: 准备数据model 1.1 ColumnTitl ...

  9. python创建excel新的表格_python创建Excel表格并添加工作表

    创建Excel工作簿方法 from openpyxl import Workbook workbook=Workbook() workbook.properties.title="我是标题& ...

最新文章

  1. Matplotlib三维绘图,这一篇就够了
  2. volatile 和 mutable 关键字
  3. 钉钉自定义机器人python_使用钉钉自定义机器人发送舔狗日记[70行][python]
  4. 南科大新任校长薛其坤:考研3次才进入中科院,杨振宁曾点赞他「诺奖级」研究成果...
  5. PMP-【第9章 项目资源管理】-2021-2-15(200页-219页)
  6. 学英语不必太在意单词
  7. 进阶丨如何让你的数据分析更加简洁专业
  8. 构架高性能WEB网站的几点知识
  9. mysql left join两个表,mysql left join 多个表
  10. SQL 中 left join 的底层原理(各种JOIN的复杂度探究)
  11. Ucosii消息邮箱使用
  12. [剑指offer][JAVA]面试题第[21]题[调整数组顺序使奇数位于偶数面前][双指针]
  13. 边缘检测后去除噪点_修图前vs修图后,原来那些网红“照骗”都是这样修出来的!...
  14. linux 下安装ftp服务器
  15. CSS 实现行内和上下自适应的几种方法
  16. SQL Server 锁
  17. 面向对象——类设计(六)——算法类
  18. [转]Basic OCR in OpenCV
  19. DNS介绍,哪个好,速度快稳定
  20. python表示差值_Python-dataframe的对应列求差值

热门文章

  1. 指定文件名无效或太长,请指定另一文件名
  2. 开源OA协同办公平台使用教程:O2OA如何集成yozo
  3. Remove from Sidebar后找回
  4. Redis入门权威指北
  5. Flutter升级3.0
  6. 怎样在计算机里恢复云文档图标,电脑常识科普:Win10资源管理器中的WPS云文档图标怎么彻底删除...
  7. 为什么Android手机总是越用越慢?
  8. 服务器拷贝文件提示ms-dos功能无效,复制文件提示“MS-DOS功能无效”无法移动解决措施...
  9. DolphinScheduler技术分析(八)
  10. 南信大网络工程和计算机科学与技术,南信大最好就业的专业?