poi与excel表格数据的互转

发布时间 2023-04-11 17:57:36作者: 三号小玩家
  <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>


import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.io.*;
import java.util.*;

public class POIUtils {

            public static void main(String[] args) throws Exception {
               Map ecMap = importTable();
//                2.拉取excel key中英文 map<中文,list> map1(全部的),
                //map1  map<中文,codes>  (全部的),
                // map<中文,英文> map2(需要修改的资源英文文案)
                //  map<key,中文> map3(剩下的数据),
                // 空 map<key,中文> map4(改好的)
                LinkedMultiValueMap<String, String> map1 = new LinkedMultiValueMap<>(2048);
                HashMap<String, String>  map2 = new HashMap<String, String>();
                HashMap<String, String>  map3 = new HashMap<String, String>();
                HashMap<String, String>  map4 = new HashMap<String, String>();
                importTable2(map1,map2,map3);

                updateMap(ecMap, map1, map2, map3, map4);
                insert_new(map2, map4);
                insert_old(map2,map3);
//                6.将没改好的插入到 中文,key中文进行键值对插入,对应的key3。
//                7.英文同上。


            }

    private static void insert_old(HashMap<String, String> map2,HashMap<String, String> map3) throws  Exception {
        Properties prop_zh = new Properties();
         OutputStream zh_in = new BufferedOutputStream(new FileOutputStream("x\\src\\main\\resources\\bdp_i18n\\old_zh.properties"));
         Iterator<Map.Entry<String, String>> zh_it = map3.entrySet().iterator();
        while (zh_it.hasNext()) {
            Map.Entry<String, String> zh_entry = zh_it.next();
            String entryKey = zh_entry.getKey();
            String entryValue = zh_entry.getValue();
            prop_zh.setProperty(entryKey, entryValue);
        }
        prop_zh.store(zh_in,null);


        Properties prop_en = new Properties();
        OutputStream en_in = new BufferedOutputStream(new FileOutputStream("x\\src\\main\\resources\\bdp_i18n\\old_en.properties"));


        Iterator<Map.Entry<String, String>> en_it = map3.entrySet().iterator();
        while (en_it.hasNext()) {
            Map.Entry<String, String> en_entry = en_it.next();
            String entryKey_en = en_entry.getKey();
            String entryValue_en = en_entry.getValue();
            //根据key中文,找到中文,根据key英文插入英文,这俩可以一起弄
            String yyvalue = map2.get(entryValue_en);
            prop_en.setProperty(entryKey_en,yyvalue);
        }
        prop_en.store(en_in,null);

    }

    private static void insert_new(HashMap<String, String> map2, HashMap<String, String> map4) throws IOException {
    /*  4.插入properties文件中文,去找map4,key中文的进行键值对插入
      5.插入英文,根据key中文,找到中文,根据key英文插入英文,这俩可以一起弄。*/
        Properties prop_zh = new Properties();
         OutputStream zh_in = new BufferedOutputStream(new FileOutputStream("x\\src\\main\\resources\\bdp_i18n\\new_zh.properties"));

       // prop_zh.load(zh_in);
        Iterator<Map.Entry<String, String>> zh_it = map4.entrySet().iterator();
        while (zh_it.hasNext()) {
            Map.Entry<String, String> zh_entry = zh_it.next();
            String entryKey = zh_entry.getKey();
            String entryValue = zh_entry.getValue();
            prop_zh.setProperty(entryKey, entryValue);
        }
        prop_zh.store(zh_in, null);


        Properties prop_en = new Properties();
        OutputStream en_in = new BufferedOutputStream(new FileOutputStream("x\\src\\main\\resources\\bdp_i18n\\new_en.properties"));


        Iterator<Map.Entry<String, String>> en_it = map4.entrySet().iterator();
        while (en_it.hasNext()) {
            Map.Entry<String, String> en_entry = en_it.next();
            String entryKey_en = en_entry.getKey();
            String entryValue_en = en_entry.getValue();
            //根据key中文,找到中文,根据key英文插入英文,这俩可以一起弄
            prop_en.setProperty(entryKey_en, map2.get(entryValue_en));
        }
        prop_en.store(en_in, null);
    }

    /**
     * 修改添加数据。
     * @param ecMap
     * @param map1
     * @param map2
     * @param map3
     * @param map4
     */
    private static void updateMap(Map ecMap, LinkedMultiValueMap<String, String> map1, HashMap<String, String> map2, HashMap<String, String> map3, HashMap<String, String> map4) {
        Iterator<Map.Entry<String, String>> it = ecMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            String entryKey = entry.getKey();
            String entryValue = entry.getValue();
            if(map1.containsKey(entryKey)){
                //1. 修改map2.英文
                map2.put(entryKey,entryValue);
                List<String> codeList = map1.get(entryKey);
                for (int i = 0; i <codeList.size() ; i++) {
                    //把修改成功的加入到新的集合, 删除老的集合。
                    map4.put( codeList.get(i),entryKey);
                    map3.remove( codeList.get(i));
                }

            }
        }
    }

    /**
     * 中英文key导入
     * @return
     */
    private static  void importTable2(LinkedMultiValueMap<String, String> linkedMap,HashMap<String, String> map2,HashMap<String, String> map3) {
        // 创建文件并指定文件路径
        File file = new File("x\\时间-事情\\翻译文案输出.xlsx");
        Map ecMap=new HashMap<String,String>();
        try {
            // 创建改文件的输入流
            FileInputStream stream = new FileInputStream(file);

            // 创建工作簿
            XSSFWorkbook workbook = new XSSFWorkbook(stream);

            // 获取一个工作表,下标从0开始
            XSSFSheet sheet = workbook.getSheetAt(0);

            // 通过循环,逐行取出表中每行数据
            for(int i=0;i<=sheet.getLastRowNum();i++){
                // 获取行
                XSSFRow row = sheet.getRow(i);

                // 获取行中列的数据
                String[] value = new String[3];

                try {//翻译文案key	翻译文案中文	翻译文案英文
                    value[0] = row.getCell(0).getStringCellValue();
                    value[1] = row.getCell(1).getStringCellValue();
                    value[2] = row.getCell(2).getStringCellValue();

                    linkedMap.add(value[1],value[0]);
                    map2.put( value[1] , value[2] );
                    map3.put( value[0] , value[1] );

                } catch (Exception e) {
                    ecMap.remove(value[0]);
                }



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

    }


    private static  Map<String,String> importTable() {
        // 创建文件并指定文件路径
        File file = new File("x\\newtable.xlsx");
        Map ecMap=new HashMap<String,String>();
        try {
            // 创建改文件的输入流
            FileInputStream stream = new FileInputStream(file);

            // 创建工作簿
            XSSFWorkbook workbook = new XSSFWorkbook(stream);

            // 获取一个工作表,下标从0开始
            XSSFSheet sheet = workbook.getSheetAt(0);

            // 通过循环,逐行取出表中每行数据
            for(int i=0;i<=sheet.getLastRowNum();i++){
                // 获取行
                XSSFRow row = sheet.getRow(i);

                // 获取行中列的数据
                String[] value = new String[3];

                try {
                    value[0] = row.getCell(0).getStringCellValue();
                    value[1] = row.getCell(1).getStringCellValue();
                    ecMap.put(value[0],value[1]);
                } catch (Exception e) {
                    ecMap.remove(value[0]);
                }



            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ecMap;
    }
}