需要架包: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)); } }
1 package cn.fanqi.fq; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import org.apache.poi.ss.usermodel.IndexedColors; 8 import org.apache.poi.xssf.usermodel.XSSFCellStyle; 9 import org.apache.poi.xssf.usermodel.XSSFFont; 10 import org.apache.poi.xssf.usermodel.XSSFRow; 11 import org.apache.poi.xssf.usermodel.XSSFSheet; 12 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 13 14 public class TestDemo { 15 16 public void createExcel() { 17 18 XSSFWorkbook x = new XSSFWorkbook(); 19 20 XSSFFont font1 = x.createFont(); 21 font1.setFontHeightInPoints((short) 15); 22 font1.setFontName("Pristina"); 23 font1.setColor(IndexedColors.GREEN.index); 24 //font1.setColor(HSSFColor.YELLOW.index); 25 XSSFCellStyle style = x.createCellStyle(); 26 style.setFont(font1); 27 28 XSSFSheet sheet = x.createSheet(); 29 XSSFRow row0 = sheet.createRow(0); 30 row0.createCell(0).setCellValue("姓名"); 31 row0.getCell(0).setCellStyle(style); 32 row0.createCell(1).setCellValue("性别"); 33 row0.getCell(1).setCellStyle(style); 34 row0.createCell(2).setCellValue("年龄"); 35 row0.getCell(2).setCellStyle(style); 36 row0.createCell(3).setCellValue("职位"); 37 row0.getCell(3).setCellStyle(style); 38 row0.createCell(4).setCellValue("工作年限"); 39 row0.getCell(4).setCellStyle(style); 40 41 User u = new User(); 42 u.setName("郭大侠"); 43 u.setSex("男"); 44 u.setAge("30"); 45 u.setJob("Java开发"); 46 u.setExperience("2"); 47 48 User u1 = new User(); 49 u1.setName("陶大婶"); 50 u1.setSex("男"); 51 u1.setAge("28"); 52 u1.setJob("Java开发"); 53 u1.setExperience("3"); 54 55 ArrayList<User> arrayList = new ArrayList<User>(); 56 arrayList.add(u); 57 arrayList.add(u1); 58 59 for(int i=1; i<3; i++){ 60 XSSFRow row = sheet.createRow(i); 61 row.createCell(0).setCellValue(arrayList.get(i-1).getName()); 62 row.createCell(1).setCellValue(arrayList.get(i-1).getSex()); 63 row.createCell(2).setCellValue(arrayList.get(i-1).getAge()); 64 row.createCell(3).setCellValue(arrayList.get(i-1).getJob()); 65 row.createCell(4).setCellValue(arrayList.get(i-1).getExperience()); 66 } 67 68 try { 69 x.write(new FileOutputStream("E:\\test.xlsx")); 70 } catch (FileNotFoundException e) { 71 e.printStackTrace(); 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } 75 } 76 77 public static void main(String[] args) { 78 79 TestDemo t = new TestDemo(); 80 t.createExcel(); 81 } 82 }
时间: 2024-10-16 01:29:39