安卓导出Excel 表格

简单的导出一份 Excel表格

首先你需要一个jar包

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

安卓导出Excel 表格_第1张图片

没有可以向我要

感谢作者

工具类代码
public class ExcelUtils {
    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 String pathName;

    

    /**
     * 初始化Excel
     * @param path     导出excel存放的地址(目录)
     * @param fileName
     * @param title
     * @param colName  excel中包含的列名(可以有多个)
     */
    public static void initExcel(String path, String fileName, String title, String[] colName) {
        format();
        WritableWorkbook workbook = null;
        try {
            File file = new File(path + fileName);
            pathName = path + fileName;
            if (!file.exists()) {
                file.createNewFile();
            }
            workbook = Workbook.createWorkbook(file);
            //设置表格的名字
            WritableSheet sheet = workbook.createSheet(title, 0);
            //创建标题栏
            sheet.addCell((WritableCell) new Label(0, 0, path + fileName, 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();
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static  void writeObjListToExcel(List objList, AddArray addArray ) {

        if (objList != null && objList.size() > 0) {
            WritableWorkbook writebook = null;
            InputStream in = null;
            try {
                WorkbookSettings setEncode = new WorkbookSettings();
                setEncode.setEncoding(UTF8_ENCODING);
                in = new FileInputStream(new File(pathName));
                Workbook workbook = Workbook.getWorkbook(in);
                writebook = Workbook.createWorkbook(new File(pathName), workbook);
                WritableSheet sheet = writebook.getSheet(0);

                for (int j = 0; j < objList.size(); j++) {

                    List list = new ArrayList<>();

                    addArray.addData(list, 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() <= 4) {
                            //设置列宽
                            sheet.setColumnView(i, list.get(i).length() + 8);
                        } else {
                            //设置列宽
                            sheet.setColumnView(i, list.get(i).length() + 5);
                        }
                    }
                    //设置行高
                    sheet.setRowView(j + 1, 350);
                }

                writebook.write();
                Log.e("My","导出成功");
            } 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 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, 10);
            arial12format = new WritableCellFormat(arial12font);
            //对齐格式
            arial10format.setAlignment(jxl.format.Alignment.CENTRE);
            //设置边框
            arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

        } catch (WriteException e) {
            e.printStackTrace();
        }
    }


    public interface AddArray {
        void addData(List list, int posion);
    }

}

调用

你信不信你不添加那些个读写权限也能成功

   final ArrayList userBeans = new ArrayList<>();

                userBeans.add(new UserBean("张三","蓝","18"));
                userBeans.add(new UserBean("李四","蓝","21"));
                userBeans.add(new UserBean("王五","蓝","55"));
                userBeans.add(new UserBean("王二麻子","绿","26"));
                userBeans.add(new UserBean("赵六","绿","42"));

                ExcelUtils.initExcel(
                        Environment.getExternalStorageDirectory().getPath() + "/", "模拟测试.xls",  //路径+文件名
                        "模拟测试", new String[]{"输出项"}   //数组有个,你的表格就有及列项
                );

                ExcelUtils.writeObjListToExcel(userBeans, new ExcelUtils.AddArray() {
                    @Override
                    public void addData(List list, int j) {
                        //将你的封装类拆开 添加到这个集合
                        list.add(userBeans.get(j).getAge());
                        list.add(userBeans.get(j).getName());
                        list.add(userBeans.get(j).getSex());
                    }
                });

注意:导出的数据全部都是String 数据类型 , 在Excel 表格中 你如果想要排序是不行的,需要修改数据类型

安卓导出Excel 表格_第2张图片

你可能感兴趣的:(安卓导出Excel 表格)