android 解放双手 自动化生成多语言strings内容

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuliang756071448/article/details/86644277

本文主要基于jxl.jar 自动解析Excel xls文件内容,生成values-文件夹及strings.xml文件

资源文件:代码及jxl.jar下载

只能支持97-2003的Excel xls,不适配Excel xlsx文件,需要另存为97-2003的xls文件后才能读取。

产品经理给出的多语言.xls文件内容如下,
在这里插入图片描述
自动化生成后:
在这里插入图片描述
在这里插入图片描述

工具类:

public class ValueModel {

    private String countryCode; //string文件所属国家代号,即value-后接的字符串
    private String name;        //字段的name
    private String defaultValue;    //字段的默认内容
    private String value;   //字段的多语言内容

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "ValueModel{" +
                "countryCode='" + countryCode + '\'' +
                ", name='" + name + '\'' +
                ", defaultValue='" + defaultValue + '\'' +
                ", value='" + value + '\'' +
                '}';
    }
}
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * Created:     by leon
 * Date:        2018/12/1
 * Description: 根据xls文件自动化生成多语言strings内容
 *              需要参考给定的xls文件格式
 */
public class GlobalizationUtil {

    //xls文件所在目录,也是生成的values文件夹的所在目录
    private final static String DIR = "C:\\Users\\leon\\Desktop\\globalization";
    //xls文件名
    private final static String XMS_PATH = DIR + "\\多语言.xls";
    //key 为第几列, value 为国家代码
    private static Map<Integer, String> sCodeMap = new HashMap<>();
    //key为国家代码, value 为多语言内容
    private static Map<String, List<ValueModel>> sValueMap = new HashMap<>();

    public static void getXlsData() {
        getXlsData(XMS_PATH, 2, 5);
    }

    /**
     * 只能支持xls格式,即97-2003Excel文件,暂不支持xlsx格式
     *
     * @param xlsPath      多语言xls文件
     * @param startColumns 从第几列开始是多语言内容,从0计数
     * @param count        一共有几列是多语言的内容
     */
    public static void getXlsData(String xlsPath, int startColumns, int count) {
        sValueMap.clear();
        sCodeMap.clear();
        File excelFile = new File(xlsPath);
        try {
            WorkbookSettings workbookSettings = new WorkbookSettings();
            //可以设置为UTF-8或者GBK或者ISO-8859-1,这里需要设置为ISO-8859-1,否则有可能其他国家文字会出现乱码
            workbookSettings.setEncoding("ISO-8859-1");
            Workbook workbook = Workbook.getWorkbook(excelFile, workbookSettings);
            //总共有几张表
            int sheetNum = workbook.getNumberOfSheets();
            //0代表获取的是第一张表
            Sheet sheet = workbook.getSheet(0);
            //表名
            String sheetName = sheet.getName();
            //表的行数
            int sheetRows = sheet.getRows();
            //表的列数
            int sheetColumns = sheet.getColumns();

            System.out.println("the num of sheets is " + sheetNum);
            System.out.println("the name of sheet is  " + sheetName);
            System.out.println("total rows is 行=" + sheetRows);
            System.out.println("total cols is 列=" + sheetColumns);
            //i=1,代表从第2行开始读取文件内容
            for (int i = 1; i < sheetRows; i++) {
                //第2行是读取国家代码
                if (i == 1) {
                    for (int number = 0; number < count; number++) {
                        int columnIndex = startColumns + number;
                        String code = sheet.getCell(columnIndex, i).getContents();
                        sCodeMap.put(columnIndex, code);
                        sValueMap.put(code, new ArrayList<>());
                    }
                } else {
                    String name = sheet.getCell(0, i).getContents();
                    if (name == null || name.equals("")) {
                        break;
                    }
                    String defaultValue = sheet.getCell(1, i).getContents();
                    for (int number = 0; number < count; number++) {
                        int columnIndex = startColumns + number;
                        String code = sCodeMap.get(columnIndex);
                        List<ValueModel> countryList = sValueMap.get(code);
                        ValueModel valueModel = new ValueModel();
                        valueModel.setName(name);
                        valueModel.setDefaultValue(defaultValue);
                        valueModel.setCountryCode(code);
                        valueModel.setValue(sheet.getCell(columnIndex, i).getContents());
                        countryList.add(valueModel);
                    }
                }
            }
            workbook.close();
            createValuesStringsXml();
        } catch (Exception e) {
            System.out.println("read error=" + e);
        }
    }

    /**
     * 创建values文件夹和strings文件,并写入内容
     */
    private static void createValuesStringsXml() {
        Set<String> keySet = sValueMap.keySet();
        for (String code : keySet) {
            File cacheDir = new File(DIR, "values-" + code);
            if (!cacheDir.exists()) {
                cacheDir.mkdir();
            }
            File file = new File(cacheDir, "strings.xml");
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
                BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                bw.write("<resources>");
                bw.newLine();
                List<ValueModel> list = sValueMap.get(code);
                for (ValueModel model : list) {
                    bw.write("<string name=\"" + model.getName() + "\">" + model.getValue() + "</string>");
                    bw.newLine();
                }
                bw.write("</resources>");
                bw.close();
                System.out.println("写入完成" + cacheDir.getName() + " " + file.getName());

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

}

猜你喜欢

转载自blog.csdn.net/wuliang756071448/article/details/86644277