Android 列表数据写入到本地Excel文件(包括图片)

要想用此demo,需要在build.gradle文件中加入:implementation group: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6.12'

如果是便已无法通过,还是自己手动下载jxl.jar包吧

 

MainActivity.java

public class MainActivity extends Activity {

    private Button btn;

    private String excelPath;
    private File excelFilePath;
    private File excelFile;

    private Bitmap bitmap;

    private List list = new ArrayList();

    private static final String TEST_PICTURE_PATH = "mnt/sdcard/testdata";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PermissionRequest.requestPermission(this);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        bitmap = BitmapFactory.decodeFile("mnt/sdcard/testdata/test.png", options);

        initData();

        btn = findViewById(R.id.excel);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                excelPath = TEST_PICTURE_PATH + File.separator + "test.xls";
                excelFilePath = new File(TEST_PICTURE_PATH);
                if (!excelFilePath.exists()) {
                    excelFilePath.mkdirs();
                }

                excelFile = new File(excelPath);
                if (!excelFile.exists()) {
                    try {
                        excelFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                exportData(list);
            }
        });
    }

    private void initData() {
        for (int i = 1; i <= 100; i++) {
            ResultModel model = new ResultModel();
            model.setNumber(i);
            model.setAnswer_view(bitmap);
            model.setAnswer_text(i);
            model.setVerify(i);
            model.setResult("〇");
            list.add(model);
        }
    }


    /**
     * @param list 导出数据
     */
    public static void exportData(List list) {
        WritableWorkbook wwb = null;
        try {
            wwb = Workbook.createWorkbook(new File("mnt/sdcard/testdata/test.xls"));
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("dd", "exportData: " + e.toString());
        }
        if (wwb != null) {
            //创建底部的选项卡  传参是选项卡的名称  和  选型卡的索引
            WritableSheet writableSheet = wwb.createSheet("test", 0);
            //创建excel的表头的信息
            String[] topic = {"序号", "图片", "答案", "作答", "评分"};
            for (int i = 0; i < topic.length; i++) {
                //横向的在单元格中填写数据
                Label labelC = new Label(i, 0, topic[i]);
                try {
                    writableSheet.addCell(labelC);

                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
            //从实体中遍历数据并将数据写入excel文件中
            ResultModel account;
            ArrayList li;
            for (int j = 0; j < list.size(); j++) {
                account = list.get(j);
                li = new ArrayList<>();
                li.add(account.getNumber() + "");
                li.add(account.getAnswer_view() + "");
                li.add(account.getAnswer_text() + "");
                li.add(account.getVerify() + "");
                li.add(account.getResult() + "");
                int k = 0;
                for (int i = 0; i < li.size(); i++) {
                    if (k == 1) {
                        try {
                            WritableImage image = new WritableImage(k, j + 1, 1, 1, saveBitmapFile(account.getAnswer_view(), "/mnt/sdcard/testdata/test.png"));
                            writableSheet.addImage(image);
                        } catch (Exception e) {
                            Log.d(TAG, "exportData: error " + e.toString());
                        }
                    } else {
                        Label labelC = new Label(k, j + 1, li.get(i));
                        try {
                            writableSheet.addCell(labelC);
                        } catch (WriteException e) {
                            e.printStackTrace();
                            Log.d(TAG, "exportData: error " + e.toString());
                        }
                    }
                    k++;
                }
            }
        }
        //将文件从内存写入到文件当中
        try {
            wwb.write();
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    public static File saveBitmapFile(Bitmap bitmap, String filepath) {
        File file = new File(filepath);
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}

本文是一个很好地Android数据传入本地Excel的demo,里面有整型,字符串和Bitmap类型

不过要提前将测试图片保存到:mnt/sdcard/testdata/test.png

希望对大家有帮助

 

你可能感兴趣的:(Android,excel,android)