需要架包:poi相关jar,Md5.jar
----------------------------------------------------------------------------------------------------------------------------------
package com.fanqi.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONObject;
public class TestDemo1 {
/*
* 读取Excel数据
*/
public List<Map<String, Object>> readExcel(String filename) {
FileInputStream in;
XSSFWorkbook workbook = null;
try {
in = new FileInputStream(filename);
workbook = new XSSFWorkbook(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
XSSFSheet sheet = workbook.getSheetAt(0);
int num = 0;
List<Map<String, Object>> listCanshu = new ArrayList<Map<String, Object>>();
for(int i=1;i<=sheet.getLastRowNum();i++){
Map<String, Object> map = new TreeMap<String, Object>();
String cell0 = sheet.getRow(i).getCell(0).getStringCellValue();
String cell1 = sheet.getRow(i).getCell(1).getStringCellValue();
map.put("canshu1", cell0);
map.put("canshu2", cell1);
listCanshu.add(map);
}
return listCanshu;
}
/*
* 组装成JSON形式
*/
public JSONArray zuZhuangCanShu(List<Map<String, Object>> listCanshu) {
JSONArray jArray = new JSONArray();
for(int i=0;i<listCanshu.size();i++ ){
JSONObject json = new JSONObject(listCanshu.get(i));
jArray.put(json);
}
return jArray;
}
/*
* 验签,加密
*/
public List signMD5(List<Map<String, Object>> l) {
List list = new ArrayList();
for(int i=0;i<l.size();i++){
String str = "";
StringBuffer buffer = null;
byte[] result = null;
Set set = l.get(i).entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry mapEntry = (Map.Entry) it.next();
String key = (String) mapEntry.getKey();
String value = (String) mapEntry.getValue();
str = str + key + "=" + value + "&";
}
str = str.substring(0, str.length()-1);
try {
MessageDigest md = MessageDigest.getInstance("MD5");
result = md.digest(str.getBytes());
buffer = new StringBuffer();
for(byte r : result){
String s =Integer.toHexString(0xff & r);
if(s.length()==1){
buffer.append("0"+s);
}else{
buffer.append(s);
}
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
list.add(buffer.toString());
}
return list;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
TestDemo1 t = new TestDemo1();
List<Map<String, Object>> l = t.readExcel("D:\1.xlsx");
JSONArray jArray = t.zuZhuangCanShu(l);
List list = t.signMD5(l);
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));
}
}