package com.erayt.ctms.Zimport;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.functors.ForClosure;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erayt.ctms.system.standard.xnet.CtmsXNetContext;
import com.erayt.xnet4j.XNetCommand;
import com.erayt.xnet4j.XNetData;
public class ReadForwardExcelServiceImpl implements ReadForwardExcelService {
private SearchPosDao searchForwardDao;
public SearchPosDao getSearchForwardDao() {
return searchForwardDao;
}
public void setSearchForwardDao(SearchPosDao searchForwardDao) {
this.searchForwardDao = searchForwardDao;
}
protected Logger logger = LoggerFactory
.getLogger(this.getClass().getName());
private Workbook wb;
// private JSONObject jsonObject = new JSONObject();
private List<Object> list_key = new ArrayList<Object>();
private List<Object> list_body = new ArrayList<Object>();
private List<HashMap<String,Object>> list_map = new ArrayList<HashMap<String,Object>>();
private CtmsXNetContext ctmsXNetContext;
private String filename3;
public String getFilename3() {
return filename3;
}
public void setFilename3(String filename3) {
this.filename3 = filename3;
}
public String getFilename4() {
return filename4;
}
public void setFilename4(String filename4) {
this.filename4 = filename4;
}
private String filename4;
public void setCtmsXNetContext(CtmsXNetContext ctmsXNetContext) {
this.ctmsXNetContext = ctmsXNetContext;
}
public void init() {
readXml(filename3);
}
public void readXml(String fileName) {
try {
InputStream input = new FileInputStream(fileName); // 建ç«è¾å
¥æµ
// æ ¹æ®æä»¶æ ¼å¼2003æ¥åå§å
wb = new HSSFWorkbook(input);
Sheet sheet = wb.getSheetAt(0); // è·å¾ç¬¬ä¸ä¸ªè¡¨å
Iterator<Row> rows = sheet.rowIterator(); // è·å¾ç¬¬ä¸ä¸ªè¡¨åçè¿ä»£å¨
for(int i=0;i<sheet.getPhysicalNumberOfRows();i++) {
Row row = rows.next(); // è·å¾è¡æ°æ®
//System.out.println(sheet.getPhysicalNumberOfRows()+"+++++");
Iterator<Cell> cells = row.cellIterator();// è·å¾ç¬¬iè¡çè¿ä»£å¨
HashMap<String, String> HashBegain=new HashMap<String, String>();
for(int j=0;j<row.getPhysicalNumberOfCells();j++){
//System.out.println(row.getPhysicalNumberOfCells()+"+++++"+j);
Cell cell = cells.next();
if(i==0||i==7){
list_key.add(cell.getStringCellValue());
}else {
if(cell.getCellType()== cell.CELL_TYPE_NUMERIC){
HashBegain.put((String) list_key.get(j), String.valueOf(cell.getNumericCellValue()));
}
else{HashBegain.put((String) list_key.get(j), cell.getStringCellValue());}
}
}
if(i!=0&&i!=7){
list_body.add(HashBegain);
}
if(i==6){
list_key = new ArrayList<Object>();
}
}
installJson();
readExcelEqual();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// ç»è£
å³æ交æjsonæ¥æ,并åé
public void installJson() {
String headStr = "{Head:" + "{\"chanelNo\":1508170004977,"
+ "\"dataCount\":\"1\"," + "\"eventType\":\"1\","
+ "\"msgNo\":\"1508170004977-20150917171754\","
+ "\"msgType\":\"1\"," + "\"sendingTime\":\"20150917171754\","
+ "\"source\":\"TR1\"," + "\"subSource\":\"TR1\","
+ "\"version\":\"1.0\"}," + "Body:"
+ JSONArray.toJSONString(list_body).replaceAll("\"", "'") + "}";
System.out.println(headStr);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendXNetTradeData(headStr, "RK1IN.Trade", "UTF-8");
}
// åéå°xnetd
public void sendXNetTradeData(String sendJson, String title, String charset) {
XNetData xnetData = new XNetData();
xnetData.setData(sendJson, charset);
xnetData.setTitle(title);
xnetData.setCommand(XNetCommand.TITLE);
if (ctmsXNetContext.getCtmsXnetSocket().isLoginOK()) {
ctmsXNetContext.getCtmsXnetSocket().sendData(xnetData);
} else {
logger.warn("CtmsXnet no login. sendXNetData[" + title
+ "] return.");
}
}
// æå°jsonå±æ§
public static void printClassInfo(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
boolean accessFlag = fields[i].isAccessible();
fields[i].setAccessible(true);
String varName = fields[i].getName();
Object varValue = fields[i].get(object);
System.out.println(varName + "=" + varValue);
fields[i].setAccessible(accessFlag);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//è·åexcelç»ææ°æ®,并è¿è¡æ¯è¾
public void readExcelEqual() {
//读åexcel头寸æ°æ®
HashMap<String, String> HashEnd=new HashMap<String, String>();
Workbook wb1;
List<Object> list_keyBody = new ArrayList<Object>();
List<HashMap> list_keyBody2 = new ArrayList<HashMap>();
//List<HashMap<String,Object>> list_keyBody2 = new ArrayList<HashMap<String,Object>>();
boolean END=false;
try {
InputStream input = new FileInputStream(filename4); // 建ç«è¾å
¥æµ
wb1 = new HSSFWorkbook(input);
Sheet sheet = wb1.getSheetAt(0); // è·å¾ç¬¬ä¸ä¸ªè¡¨å
Iterator<Row> rows = sheet.rowIterator(); // è·å¾ç¬¬ä¸ä¸ªè¡¨åçè¿ä»£å¨
int i = 0;
while (rows.hasNext()) {
HashEnd=new HashMap<String, String>();
Row row = rows.next(); // è·å¾è¡æ°æ®
for (int j = 0; j < row.getLastCellNum(); j++) {
//System.out.println(row.getLastCellNum()+"XXXXX");
Cell cell = row.getCell(j);
if(i==0){
list_keyBody.add(cell.getStringCellValue());
}else{
if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){
HashEnd.put((String) list_keyBody.get(j), String.valueOf(cell.getNumericCellValue()));
}
else
{
HashEnd.put((String) list_keyBody.get(j), cell.getStringCellValue());
}
}
}
if(i!=0){
list_keyBody2.add(HashEnd);}
i++;
}
} catch (IOException ex) {
ex.printStackTrace();
}
//ä»æ°æ®åºè·å头寸æ°æ®
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list_map=searchForwardDao.findForwardPos();
//è¾åºè®¡ç®æ£ç¡®ä¸å¦
for(int k=0;k<list_map.size();k++){
if(END=list_map.get(k).get("AMOUNT_1").toString().equals((String) list_keyBody2.get(k).get("AMOUNT_1")))
{
logger.debug("è¿æ第"+k+"ç» è´§å¸1éé¢è®¡ç®æ£ç¡®"+" Amount1="+list_map.get(k).get("AMOUNT_1").toString());
}else {
logger.debug("è¿æ第"+k+"ç» è´§å¸1éé¢è®¡ç®ä¸æ£ç¡®"+"left="+list_map.get(k).get("AMOUNT_1").toString()+" right="+list_keyBody2.get(k).get("AMOUNT_1"));
}
if(END=list_map.get(k).get("AMOUNT_2").toString().equals((String) list_keyBody2.get(k).get("AMOUNT_2")))
{
logger.debug("è¿æ第"+k+"ç» è´§å¸2éé¢è®¡ç®æ£ç¡®"+" Amount2="+list_map.get(k).get("AMOUNT_2").toString());
}else {
logger.debug("è¿æ第"+k+"ç» è´§å¸2éé¢è®¡ç®ä¸æ£ç¡®"+"left="+list_map.get(k).get("AMOUNT_2").toString()+" right="+list_keyBody2.get(k).get("AMOUNT_2"));
}
if(END=list_map.get(k).get("START_PL").toString().equals((String) list_keyBody2.get(k).get("START_PL")))
{
logger.debug("è¿æ第"+k+"ç» ç´¯è®¡æç计ç®æ£ç¡®"+" START_PL="+list_map.get(k).get("START_PL").toString());
}else {
logger.debug("è¿æ第"+k+"ç» ç´¯è®¡æç计ç®ä¸æ£ç¡®"+"left="+list_map.get(k).get("START_PL").toString()+" right="+list_keyBody2.get(k).get("START_PL"));
}
if(END=list_map.get(k).get("DAILY_PL").toString().equals((String) list_keyBody2.get(k).get("DAILY_PL")))
{
logger.debug("è¿æ第"+k+"ç» æ¥æ¢æ计ç®æ£ç¡®"+" DAILY_PL="+list_map.get(k).get("DAILY_PL").toString());
}else {
logger.debug("è¿æ第"+k+"ç» æ¥æ¢æ计ç®ä¸æ£ç¡®"+"left="+list_map.get(k).get("DAILY_PL").toString()+" right="+list_keyBody2.get(k).get("DAILY_PL"));
}
}
}
}
温馨提示:答案为网友推荐,仅供参考