一。在项目的libs文件夹下面加入jar包
,网上搜索下载,稍后我上传demo,可以从里面得到。
二。
1.生成excel并保持到sd卡,需要先判断sd卡是否可用
获取sd卡可用内存大小,如果太小数据就无法写入
private static long getAvailableStorage(Context context) { String root = context.getExternalFilesDir(null).getPath(); //获取磁盘使用情况 StatFs statFs = new StatFs(root); long blockSize = statFs.getBlockSize(); long availableBlocks = statFs.getAvailableBlocks(); long availableSize = blockSize * availableBlocks; // Formatter.formatFileSize(context, availableSize); return availableSize; }
2.表格的第一行表头,可以设置一个样式
public static WritableCellFormat getHeader() { //参数1:字体大小, 2:18,3:粗体,4:斜体 WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, true);// 定义字体 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.YELLOW);// 黄色背景 } catch (WriteException e) { e.printStackTrace(); } return format; }
3.创建表格并向里面写入数据(注释比较清晰,自己体会下存入数据方式)
public static void writeToExcel(Context context, List<Order> orders, String fileName) throws Exception { //sd卡是否可用 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && getAvailableStorage(context) > 1000000) { Toast.makeText(context, "SD卡不可用!", Toast.LENGTH_SHORT).show(); return; } String[] title = { "单号", "电话", "姓名", "地址"};//表头 File file; Fil