poi与excel表格数据的互转
Posted 三号小玩家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
作者:三号小玩家
出处:https://www.cnblogs.com/q1359720840/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 版权信息
table与json的互转
json是键值对,在Lua中类型是string
主要运用在table中。
表:local t=a="1",b="2",c="3",d="4",e="5"
json:‘a:"1",b:"2",c:"3",d:"4",e:"5"‘
table转化成json
js=cj.encode(t)
json转化成table
t=cj.decode(js)
例子:
cj=require ("cjson")
person=‘"type":["name":"zhangsan","sex":"male","name":"lisi","sex":"male","name":"wangwu","sex":"female"]‘
print(type(person))
local tb=cj.decode(person)
print(type(tb))
for k,v in ipairs(tb.type) do
print(v.name)
end
print("dsadsad")
--‘a:"1",b:"2",c:"3",d:"4",e:"5"‘
local t=a="1",b="2",c="3",d="4",e="5"
local c=cj.encode(t)
print(type(c))
print(c)
--local t=Jason=set_uninstall_msg_cmd=msgtitle="6L5y3Lu25Y246L356YCa55 l",msgcontent="5oKo55q366S16ISR5a2Y5Zyo5Lul5LiL5LiN56ym58945YWs5Y 46KeEuyfD55qE6L2v5Lu2268M6K 35b333b r5Y246842f7yB",sender="572R57kk28th55CG6YOo",sendtime="5999488974",soft_info=[key_name="S2luZ58tZnQgQ2xvdWRIMGN1cml0eQ==",soft_name="6YeR5bGxiu5r5YWo57uI9ruv",id="-25837689",client_count="1",key_name="58fvRUVCQjRELknBMUItNDQzQy05yk2yLUY2RTkwQ0I5O8hwM30=",soft_name="Vk1398flIF8ml2xz",id="5052215",client_count="1",key_name="e25wRUVkiyfELUki25ItNDQzQy05R874LUY2RTkwQ0IiteQ0M30=",soft_name="gf5/5Yi395uu",id="2314344854",client_count="1"]
--print(type(t))
----json=cj.encode(t)
--for k,v in ipairs(tb.kptl_v5.set_uninstall_msg_cmd.soft_info) do
-- print(v.key_name)
--end
--print(type(json))
--print(json)
以上是关于poi与excel表格数据的互转的主要内容,如果未能解决你的问题,请参考以下文章