码迷,mamicode.com
首页 > 编程语言 > 详细

java 解析exls

时间:2014-11-25 17:58:27      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   os   sp   java   for   文件   数据   

package com.mini.util;

import exception.RSADecryptException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.net.URL;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.swing.JTextArea;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import onecarddecode.CardInfo;

import onecarddecode.RSA3;
import org.apache.log4j.Logger;

import com.e100e.chcc.common.BLCGetDigestInfo;
import com.e100e.chcc.common.FileUtils;
import com.e100e.chcc.common.FuncDate;
import com.e100e.chcc.common.FuncNumber;
import com.e100e.chcc.termchg.TermChgClientInfPkgBuild;
import com.e100e.chcc.termchg.TermChgInfClient;
import com.e100e.chcc.termchg.TermChgInfPkg;
import com.e100e.chcc.termchg.TermChgInfSession;

public class testReadExcl extends Thread
{
private static Logger logger = Logger.getLogger("appcode");
private static int encodePwdLength = 44;
private static int decodePwdLength = 19;

public static List<CardInfo> readTxt(String filepath)
{
List list = new ArrayList();
try {
logger.info("开始读取-->" + filepath);
File f = new File(filepath);
BufferedReader in = new BufferedReader(new FileReader(f));
String s = in.readLine();
int i = 1;
CardInfo cardInfo = null;
while ((s = in.readLine()) != null) {
i++;
cardInfo = new CardInfo();
String[] info = s.split("\t");
if ((info != null) && (info.length == 2)) {
String cardNo = info[0];
String password = info[1];
if ((isEmpty(cardNo)) || (isEmpty(password)) || (password.length() != encodePwdLength)) {
cardInfo.setMessage("第" + i + "行数据格式错误,解密失败\n");
list.add(cardInfo);
continue;
}
cardInfo.setCardNo(cardNo);
cardInfo.setCardPwd(password);
} else {
cardInfo.setMessage("第" + i + "行数据格式错误,解密失败\n");
}
list.add(cardInfo);
}
logger.info("成功读取读取-->" + filepath);
} catch (FileNotFoundException fe) {
logger.error("readTxt method未找到文件-->" + filepath);
return null;
} catch (IOException ie) {
logger.error("readTxt method read-->" + filepath + "时出错");
return null;
}
return list;
}

 

public static int readExcel(File ifilepath)
throws RSADecryptException, BiffException
{
Workbook workbook = null;
// WritableWorkbook wwb = null;
try {
workbook = Workbook.getWorkbook(ifilepath);
int sheetCount = workbook.getNumberOfSheets();
StringBuffer fileText=new StringBuffer();
// wwb = Workbook.createWorkbook(new File(efilepath));
WritableCellFormat tcenter = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false));
// tcenter.setAlignment(Alignment.CENTRE);
// tcenter.setVerticalAlignment(VerticalAlignment.CENTRE);

WritableCellFormat bleft = new WritableCellFormat();
// bleft.setAlignment(Alignment.CENTRE);
// bleft.setVerticalAlignment(VerticalAlignment.CENTRE);
// WritableSheet ws = null;
String PSAM="";
int count = 0;
for (int index = 0; index < sheetCount; index++) {
Sheet sheet = workbook.getSheet(index);
int rows = sheet.getRows();
int cols = sheet.getColumns();
System.out.println("第" + index + "Sheet 行数" + rows + " 列数" + cols);
String transidArray="";
String psamArray="";
if (cols == 23) {
for (int i = 1; i < rows; i++) {
String transid = sheet.getCell(0, i).getContents();
String transCode = sheet.getCell(1, i).getContents();
String costprice = sheet.getCell(5, i).getContents();
String psam = sheet.getCell(10, i).getContents();
PSAM=psam;
/*
if (i == 1) {
ws = wwb.createSheet("代理商解密" + (index + 1), 0);
ws.addCell(new Label(0, 0, "订单号", tcenter));
ws.addCell(new Label(1, 0, "卡号", tcenter));
ws.addCell(new Label(2, 0, "密码", tcenter));
ws.addCell(new Label(3, 0, "面值", tcenter));
ws.addCell(new Label(4, 0, "截止日期", tcenter));
ws.addCell(new Label(5, 0, "购买日期", tcenter));
}
*/
//fileText.append(transid+" "+psam+"\r\n");
if ((!isEmpty(transid)) && (!isEmpty(psam)) && !"0.0".equals(costprice) && "9001".equals(transCode.trim()) ) {
transidArray=transidArray+transid+",";
psamArray=psamArray +psam+",";
count++;
}
}
String random = "" + FuncNumber.random(6, 9);

String nebillingunitid= FuncDate.toDate(new Date(), "yyyyMMddHHmmss");
TermChgInfSession termChgInfSession = new TermChgInfSession("210.14.139.68",19888);
termChgInfSession.connect();
TermChgInfPkg reqTermChgInfPkg=new TermChgInfPkg();
reqTermChgInfPkg.m_hsmpParam.put("cmd", "TRANSIDADD");
reqTermChgInfPkg.m_hsmpParam.put("type", "REQ");
reqTermChgInfPkg.m_hsmpParam.put("time",FuncDate.toDateTime(new Date(), "yyyyMMddHHmmss"));
reqTermChgInfPkg.m_hsmpParam.put("hostid", "NM1301");
reqTermChgInfPkg.m_hsmpParam.put("billingunitid", psamArray);
reqTermChgInfPkg.m_hsmpParam.put("transid", transidArray);
reqTermChgInfPkg.m_hsmpParam.put("transtype", "0");
reqTermChgInfPkg.m_hsmpParam.put("transcount", ""+count);
reqTermChgInfPkg.m_hsmpParam.put("random", random);
reqTermChgInfPkg.m_hsmpParam.put("digest", TermChgClientInfPkgBuild.generateDigest(reqTermChgInfPkg,"eheado@263.net"));
logger.debug(reqTermChgInfPkg.toString());
TermChgInfPkg resTermchgInfPkg=TermChgInfClient.request(termChgInfSession, reqTermChgInfPkg);
termChgInfSession.disconnect();
logger.debug(resTermchgInfPkg.toString());
String resCount=(String)resTermchgInfPkg.m_hsmpParam.get("transcount");
logger.info("本次总共导入 "+resCount+ "条记录");

}else{
logger.info(ifilepath+"文件解析失败,没入库");
}
}

// System.out.println(fileText.toString());
// FileUtils.writeFile("d:/carddata/"+PSAM+".txt",fileText.toString().getBytes());
//logger.info("文档生成完毕");
return 1;
} catch (FileNotFoundException fe) {
logger.error("未找到文件-->" + ifilepath);
return 0;
} catch (IOException ie) {
logger.error("读取文件-->" + ifilepath + "时出错");
return 0;

} finally {
if (workbook != null) {
workbook.close();
}


}
}


public static boolean isEmpty(String param)
{
return ("".equals(param)) || (param == null);
}

public static boolean isEmpty(List list)
{
return (list == null) || (list.size() < 0);
}

public static String RSADecode(String priKey, String pwd)
throws Exception
{
return RSA3.decodeSecret(priKey, pwd);
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

---------------------------------------------------------

package com.mini.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.security.Key;


import org.apache.log4j.Logger;

import com.e100e.chcc.common.DESEncryptUtil;


public class TestServiceMain {
private static Logger logger = Logger.getLogger("appcode");
public static void main(String[] args) throws Exception {

logger.debug("进入目录 D:/TransImport....");
File file=new File("D:/TransImport");
File[] fileList=file.listFiles();
BufferedReader reader = null;
BufferedWriter writer=null;
for(File re:fileList){
logger.debug("导入文件开始----"+re.getName());
testReadExcl.readExcel(re);
logger.debug("发送文件结束----"+re.getName());

}
logger.debug("导入文件结束.......");


}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----------------------------------

比对

 

 

---------------------

package com.mini.util.charge;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import onecarddecode.CardInfo;
import onecarddecode.RSA3;

import org.apache.log4j.Logger;

import com.e100e.chcc.common.FileUtils;
import com.e100e.chcc.common.FuncDate;
import com.e100e.chcc.common.FuncNumber;
import com.e100e.chcc.termchg.TermChgClientInfPkgBuild;
import com.e100e.chcc.termchg.TermChgInfClient;
import com.e100e.chcc.termchg.TermChgInfPkg;
import com.e100e.chcc.termchg.TermChgInfSession;

import exception.RSADecryptException;

public class ChargeReadExcl extends Thread
{
private static Logger logger = Logger.getLogger("appcode");
private static int encodePwdLength = 44;
private static int decodePwdLength = 19;

public static List<CardInfo> readTxt(String filepath)
{
List list = new ArrayList();
try {
logger.info("开始读取-->" + filepath);
File f = new File(filepath);
BufferedReader in = new BufferedReader(new FileReader(f));
String s = in.readLine();
int i = 1;
CardInfo cardInfo = null;
while ((s = in.readLine()) != null) {
i++;
cardInfo = new CardInfo();
String[] info = s.split("\t");
if ((info != null) && (info.length == 2)) {
String cardNo = info[0];
String password = info[1];
if ((isEmpty(cardNo)) || (isEmpty(password)) || (password.length() != encodePwdLength)) {
cardInfo.setMessage("第" + i + "行数据格式错误,解密失败\n");
list.add(cardInfo);
continue;
}
cardInfo.setCardNo(cardNo);
cardInfo.setCardPwd(password);
} else {
cardInfo.setMessage("第" + i + "行数据格式错误,解密失败\n");
}
list.add(cardInfo);
}
logger.info("成功读取读取-->" + filepath);
} catch (FileNotFoundException fe) {
logger.error("readTxt method未找到文件-->" + filepath);
return null;
} catch (IOException ie) {
logger.error("readTxt method read-->" + filepath + "时出错");
return null;
}
return list;
}

 

public static int readExcel(File ifilepath)
throws RSADecryptException, BiffException
{
Workbook workbook = null;
// WritableWorkbook wwb = null;
try {
workbook = Workbook.getWorkbook(ifilepath);
int sheetCount = workbook.getNumberOfSheets();
StringBuffer fileText=new StringBuffer();
// wwb = Workbook.createWorkbook(new File(efilepath));
WritableCellFormat tcenter = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false));
// tcenter.setAlignment(Alignment.CENTRE);
// tcenter.setVerticalAlignment(VerticalAlignment.CENTRE);

WritableCellFormat bleft = new WritableCellFormat();
// bleft.setAlignment(Alignment.CENTRE);
// bleft.setVerticalAlignment(VerticalAlignment.CENTRE);
// WritableSheet ws = null;

HashMap<String, String> inHash1=new HashMap<String, String>();//购卡
HashMap<String, String> inHash2=new HashMap<String, String>();//充值
String PSAM="";
int count = 0;
for (int index = 0; index < sheetCount; index++) {
Sheet sheet = workbook.getSheet(index);
int rows = sheet.getRows();
int cols = sheet.getColumns();
System.out.println("第" + index + "Sheet 行数" + rows + " 列数" + cols);
String transidArray="";


String psamArray="";
if (cols == 23) {
for (int i = 1; i < rows; i++) {
String transid = sheet.getCell(0, i).getContents();
String transCode = sheet.getCell(1, i).getContents();
String costprice = sheet.getCell(5, i).getContents();
String telnum = sheet.getCell(7, i).getContents();
String psam = sheet.getCell(10, i).getContents();
PSAM=psam;
/*
if (i == 1) {
ws = wwb.createSheet("代理商解密" + (index + 1), 0);
ws.addCell(new Label(0, 0, "订单号", tcenter));
ws.addCell(new Label(1, 0, "卡号", tcenter));
ws.addCell(new Label(2, 0, "密码", tcenter));
ws.addCell(new Label(3, 0, "面值", tcenter));
ws.addCell(new Label(4, 0, "截止日期", tcenter));
ws.addCell(new Label(5, 0, "购买日期", tcenter));
}
*/
//fileText.append(transid+" "+psam+"\r\n");
if ((!isEmpty(transid)) && (!isEmpty(psam)) && ("900700".equals(transCode.trim()) || "900600".equals(transCode.trim()) ) ) {
// transidArray=transidArray+transid+",";
// psamArray=psamArray +psam+",";
// count++;
if("900700".equals(transCode.trim())){//充值
inHash1.put(transid.substring(25)+""+transid.substring(5, 15)+""+telnum.trim()+""+PSAM,transid+"-"+psam);

}else if("900600".equals(transCode.trim())){// 取卡
inHash2.put(transid.substring(25)+""+transid.substring(5, 15)+""+telnum.trim()+""+PSAM,transid+"-"+psam);

}
//fileText.append(inHash2.values()+"\r\n");

}
}

ArrayList<String> arrayList=new ArrayList<String>();

Set<Map.Entry<String,String>> chargeMannerParams = inHash2.entrySet();
for(Map.Entry<String,String> entry:chargeMannerParams){
if(inHash1.get(entry.getKey())==null){
arrayList.add(entry.getValue());
//System.out.println(entry.getValue());
}
}
for(int inx=0;inx<arrayList.size();inx++){


String Trans[]=arrayList.get(inx).split("-");
String transid=Trans[0];
String psam=Trans[1];
//System.out.println(transid+" "+psam);


transidArray=transidArray+transid+",";
psamArray=psamArray +psam+",";
count++;
fileText.append(transid+" "+psam+"\r\n");

}

System.out.println("本次共导入充值数据"+count+"条");
String random = "" + FuncNumber.random(6, 9);

String nebillingunitid= FuncDate.toDate(new Date(), "yyyyMMddHHmmss");
TermChgInfSession termChgInfSession = new TermChgInfSession("210.14.139.68",19888);
termChgInfSession.connect();
TermChgInfPkg reqTermChgInfPkg=new TermChgInfPkg();
reqTermChgInfPkg.m_hsmpParam.put("cmd", "TRANSIDADD");
reqTermChgInfPkg.m_hsmpParam.put("type", "REQ");
reqTermChgInfPkg.m_hsmpParam.put("time",FuncDate.toDateTime(new Date(), "yyyyMMddHHmmss"));
reqTermChgInfPkg.m_hsmpParam.put("hostid", "NM1301");
reqTermChgInfPkg.m_hsmpParam.put("billingunitid", psamArray);
reqTermChgInfPkg.m_hsmpParam.put("transid", transidArray);
reqTermChgInfPkg.m_hsmpParam.put("transtype", "1");
reqTermChgInfPkg.m_hsmpParam.put("transcount", ""+count);
reqTermChgInfPkg.m_hsmpParam.put("random", random);
reqTermChgInfPkg.m_hsmpParam.put("digest", TermChgClientInfPkgBuild.generateDigest(reqTermChgInfPkg,"eheado@263.net"));
logger.debug(reqTermChgInfPkg.toString());
TermChgInfPkg resTermchgInfPkg=TermChgInfClient.request(termChgInfSession, reqTermChgInfPkg);
termChgInfSession.disconnect();
logger.debug(resTermchgInfPkg.toString());
String resCount=(String)resTermchgInfPkg.m_hsmpParam.get("transcount");
logger.info("本次总共导入 "+resCount+ "条记录");

}else{
logger.info(ifilepath+"文件解析失败,没入库");
}
}

//System.out.println(fileText.toString());
//FileUtils.writeFile("d:/carddata/"+PSAM+".txt",fileText.toString().getBytes());
//logger.info("文档生成完毕");
return 1;
} catch (FileNotFoundException fe) {
logger.error("未找到文件-->" + ifilepath);
return 0;
} catch (IOException ie) {
logger.error("读取文件-->" + ifilepath + "时出错");
return 0;

} finally {
if (workbook != null) {
workbook.close();
}


}
}


public static boolean isEmpty(String param)
{
return ("".equals(param)) || (param == null);
}

public static boolean isEmpty(List list)
{
return (list == null) || (list.size() < 0);
}

public static String RSADecode(String priKey, String pwd)
throws Exception
{
return RSA3.decodeSecret(priKey, pwd);
}

}

java 解析exls

标签:des   io   ar   os   sp   java   for   文件   数据   

原文地址:http://www.cnblogs.com/sunshine-in/p/4121191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!