Android 读取excel(支持 xls)和 xml

一、使用jxl 读取excel文件

1.build.gradle的dependencies中加入 

implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'

2.将xls文件复制到assets文件夹

Android 读取excel(支持 xls)和 xml_第1张图片

3.读取文件内容:

public static List readExcelToAssets(Context context) {
        List contentBeans = new ArrayList<>();
        try {
            InputStream is = context.getAssets().open("App.xls");
            Workbook book = Workbook.getWorkbook(is);
            book.getNumberOfSheets();
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();

            for (int i = 1; i < Rows; ++i) {
                String key = (sheet.getCell(0, i)).getContents();
                List values = new ArrayList<>();
                String value1 = (sheet.getCell(1, i)).getContents();
                values.add(value1);
                String value2 = (sheet.getCell(2, i)).getContents();
                values.add(value2);
                String value3 = (sheet.getCell(3, i)).getContents();
                values.add(value3);
                String value4 = (sheet.getCell(4, i)).getContents();
                values.add(value4);
                String value5 = (sheet.getCell(5, i)).getContents();
                values.add(value5);
                contentBeans.add(new ContentBean(key, values));
                Log.e("FileUtil", "第" + i + "行数据=\n" + key + "," + value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5);

            }
            book.close();

        } catch (Exception e) {

            Log.e("FileUtil", "e" + e);
        }
        return contentBeans;
    }

二、使用XmlPullParser读取xml文件

1.将xml文件复制到assets文件夹

2.读取文件内容:

/**
     * 获取XML中所有的指令信息
     * xml文件内容:
     * 
     *     APP
     * 
     */
    public static List parseDateSource(Context context) {
        List languageBeans = new ArrayList<>();
        try {
            InputStream inputStream = context.getAssets().open("strings.xml");
            XmlPullParser parse = Xml.newPullParser();
            parse.setInput(inputStream, "utf-8");
            int event = parse.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        Log.e("FileUtil", "parseDateSource 初始化指令集合");
                        break;
                    case XmlPullParser.START_TAG:
                        String keys = parse.getName();
                        // APP
                        // 读取string标签下的值,key = app_name,value = APP
                        if (keys.equals("string")) {
                            String key = parse.getAttributeValue(null, "name");
                            String value = parse.nextText();
                            Log.e("FileUtil", "START_TAG " + key + "," + value);
                            languageBeans.add(new LanguageBean(key, value));
                        } else {
                            Log.e("FileUtil", "START_TAG keys " + keys);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        Log.e("FileUtil", "END_TAG");
                        break;
                    default:
                        break;
                }
                event = parse.next();// 进入到下一个元素并触发相应事件
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return languageBeans;
    }

完整的FileUtil工具类:

package com.file.util.file;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;

import androidx.annotation.RequiresApi;

import com.file.util.MainActivity;
import com.file.util.bean.ContentBean;
import com.file.util.bean.LanguageBean;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

/**
 * @Author: Jin
 * @Description:
 * @CreateDate: 2022/11/17  16:34
 */
public class FileUtil {

    public static void readExcel(Context context) {
        String logFilePath = Environment.getExternalStorageDirectory() + File.separator + "Visitor";
        File file = new File(logFilePath, "test.xls");
        Log.e("FileUtil", "file=" + file.getAbsolutePath());
        try {
            InputStream is = new FileInputStream(file);
            Workbook book = Workbook.getWorkbook(is);
            book.getNumberOfSheets();
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();

            for (int i = 1; i < Rows; ++i) {
                String name = (sheet.getCell(0, i)).getContents();
                String department = (sheet.getCell(1, i)).getContents();
                String company = (sheet.getCell(2, i)).getContents();
                String phone = (sheet.getCell(3, i)).getContents();

                Log.e("FileUtil", "第" + i + "行数据=" + name + "," + department + "," + company + "," + phone);

            }
            book.close();

        } catch (Exception e) {

            Log.e("FileUtil", "e" + e);
        }
    }

    public static List readExcelToAssets(Context context) {
        List contentBeans = new ArrayList<>();
        try {
            InputStream is = context.getAssets().open("App.xls");
            Workbook book = Workbook.getWorkbook(is);
            book.getNumberOfSheets();
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();

            for (int i = 1; i < Rows; ++i) {
                String key = (sheet.getCell(0, i)).getContents();
                List values = new ArrayList<>();
                String value1 = (sheet.getCell(1, i)).getContents();
                values.add(value1);
                String value2 = (sheet.getCell(2, i)).getContents();
                values.add(value2);
                String value3 = (sheet.getCell(3, i)).getContents();
                values.add(value3);
                String value4 = (sheet.getCell(4, i)).getContents();
                values.add(value4);
                String value5 = (sheet.getCell(5, i)).getContents();
                values.add(value5);
                contentBeans.add(new ContentBean(key, values));
                Log.e("FileUtil", "第" + i + "行数据=\n" + key + "," + value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5);

            }
            book.close();

        } catch (Exception e) {

            Log.e("FileUtil", "e" + e);
        }
        return contentBeans;
    }

    /**
     * 获取XML中所有的指令信息
     * xml文件内容:
     * 
     *     APP
     * 
     */
    public static List parseDateSource(Context context) {
        List languageBeans = new ArrayList<>();
        try {
            InputStream inputStream = context.getAssets().open("strings.xml");
            XmlPullParser parse = Xml.newPullParser();
            parse.setInput(inputStream, "utf-8");
            int event = parse.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        Log.e("FileUtil", "parseDateSource 初始化指令集合");
                        break;
                    case XmlPullParser.START_TAG:
                        String keys = parse.getName();
                        // APP
                        // 读取string标签下的值,key = app_name,value = APP
                        if (keys.equals("string")) {
                            String key = parse.getAttributeValue(null, "name");
                            String value = parse.nextText();
                            Log.e("FileUtil", "START_TAG " + key + "," + value);
                            languageBeans.add(new LanguageBean(key, value));
                        } else {
                            Log.e("FileUtil", "START_TAG keys " + keys);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        Log.e("FileUtil", "END_TAG");
                        break;
                    default:
                        break;
                }
                event = parse.next();// 进入到下一个元素并触发相应事件
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return languageBeans;
    }

    public static List makeContent(Context context, int type) {
        List contents = new ArrayList<>();
        List languageBeans = FileUtil.parseDateSource(context);
        List contentBeans = FileUtil.readExcelToAssets(context);
        // 寻找语言
        for (LanguageBean languageBean : languageBeans) {
            for (ContentBean contentBean : contentBeans) {
                if (TextUtils.equals(languageBean.getValue(), contentBean.getKey())) {
                    if (contentBean.getValues().size() > type) {
                        languageBean.setValue(contentBean.getValues().get(type));
                    }
                    break;
                }
            }
        }
        // 组装结果
        for (LanguageBean languageBean : languageBeans) {
            String content = "" + languageBean.getValue() + "";
            Log.i("FileUtil", "makeContent " + content);
            contents.add(content);
        }
        return contents;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void saveXML(Context context, int type) {
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS + "/RedKey/language/");
        if (!file.exists()) {
            file.mkdirs();
        }

        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile() + "/" + "language_" + getLanguageName(type) + ".txt", true)));
            List contents = makeContent(context, type);
            for (String content : contents) {
                out.write(content + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String getLanguageName(int type) {
        String languageName = "";
        switch (type) {
            case 0:
                languageName = "en";
                break;
            case 1:
                languageName = "ru";
                break;
            case 2:
                languageName = "de";
                break;
            case 3:
                languageName = "fr";
                break;
            case 4:
                languageName = "pl";
                break;
        }
        return languageName;
    }
}

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