poi与excel表格数据的互转
<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; } }
---------------------------------------------------------------------------
国之殇,未敢忘!
南京大屠杀!
731部队!
(有关书籍《恶魔的饱食》)以及核污染水排海等一系列全无人性的操作,购买他们的食品和为它们提供帮助只会更加变本加厉的害你,呼吁大家不要购买日本相关产品
昭昭前事,惕惕后人
吾辈当自强,方使国不受他人之侮!
---------------------------------------------------------------------------
作者:三号小玩家
出处:https://www.cnblogs.com/q1359720840/
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 版权信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-04-11 三年级英语下
2022-04-11 小学英语三年级上