Android Excel导入

package com.omniteaching.exceldemo;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class MainActivity extends AppCompatActivity {

    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 final static String SHEET_NAME = "His";
    private String[] titles = new String[]{
            "名字", "年龄"
    };
    private static String path = Environment.getExternalStorageDirectory() + File.separator + "wwm"
            + File.separator + "导出记录" + ".xls";
    private HashMap, ArrayList> mDataMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        MainActivity.exportToExcel(titles, mDataMap, this, path);
    }

    private void initData() {
        ArrayList dataList1 = new ArrayList<>();
        dataList1.add("小一");
        dataList1.add("11");
        mDataMap.put(0, dataList1);
        ;
        ArrayList dataList2 = new ArrayList<>();
        dataList2.add("小er");
        dataList2.add("12");
        mDataMap.put(1, dataList2);

        ArrayList dataList3 = new ArrayList<>();
        dataList3.add("小san");
        dataList3.add("13");
        mDataMap.put(2, dataList3);

        ArrayList dataList4 = new ArrayList<>();
        dataList4.add("小si");
        dataList4.add("14");
        mDataMap.put(3, dataList4);

        ArrayList dataList5 = new ArrayList<>();
        dataList5.add("小wu");
        dataList5.add("15");
        mDataMap.put(4, dataList5);
    }

    public 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.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);
            arial14format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

            /* 标题部分 */
            arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);

            arial10format = new WritableCellFormat(arial10font);
            arial10format.setAlignment(jxl.format.Alignment.CENTRE);
            arial10format.setBackground(Colour.VERY_LIGHT_YELLOW);
            arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

            /* 内容部分 */
            arial12font = new WritableFont(WritableFont.ARIAL, 12);

            arial12format = new WritableCellFormat(arial12font);
            arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出历史记录到EXCEL
     */
    public static void exportToExcel(String[] titles, HashMap, ArrayList>
            checkedDataMap, Context context, String filePath) {
//        File file = new File(FileUtils.getSDPath());
//        String filePath = file.toString() + "/" + fileName;
//        makeDir(file);
        MainActivity.initExcel(filePath, titles);
        MainActivity.writeObjListToExcel(dealWithExportData(checkedDataMap),
                filePath, context);
    }

    public static void initExcel(String filePath, String[] colName) {
        format();
        WritableWorkbook workbook = null;
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            workbook = Workbook.createWorkbook(file);
            WritableSheet sheet = workbook.createSheet(SHEET_NAME, 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));
            }
            workbook.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @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 {
                WorkbookSettings setEncode = new WorkbookSettings();
                setEncode.setEncoding(UTF8_ENCODING);
                in = new FileInputStream(new File(fileName));
                Workbook workbook = Workbook.getWorkbook(in);
                writeBook = Workbook.createWorkbook(new File(fileName), workbook);
                WritableSheet sheet = writeBook.getSheet(0);
                for (int j = 0; j < objList.size(); j++) {
                    ArrayList list = (ArrayList) objList.get(j);
                    for (int i = 0; i < list.size(); i++) {
                        sheet.addCell(new Label(i, j + 1, list.get(i), arial12format));
                    }
                }
                writeBook.write();
                Toast.makeText(c, "保存到" + path, 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 static ArrayList> dealWithExportData(HashMap,
            ArrayList> checkedDataMap) {
        if (null != checkedDataMap) {
            ArrayList> bill2List = new ArrayList>();

            Iterator iterator = checkedDataMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                bill2List.add((ArrayList) entry.getValue());
            }
            return bill2List;
        }
        return null;
    }
}
需要用到的包待会儿上传 excel.jar

你可能感兴趣的:(android)