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=0i<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=0j<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 = 0i <fields.lengthi++) {
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 = 0j <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=0k<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"))
}
}
}
}
根据你的要求写的程序,你看看怎么样。import java.awt.*
import java.awt.event.*
import javax.swing.*
public class Demo extends JFrame implements ActionListener{
JLabel l1,l2,l3
JButton b1,b2
JTextField tf1,tf2,tf3
JRadioButton rb1,rb2,rb3,rb4
ButtonGroup bg
JPanel p1,p2,p3,p4,p5
static int a
/**
* @param args
*/
public Demo()
{ /*第一行*/
this.setLayout(new GridLayout(5,1))
p1=new JPanel()
l1=new JLabel("a")
tf1=new JTextField(12)/*单行文本框宽度8*/
tf1.setText(""+(int)(Math.random()*100))
p1.add(l1)
p1.add(tf1)
/*第二行*/
p2=new JPanel()
rb1=new JRadioButton("+")
rb2=new JRadioButton("-")
rb3=new JRadioButton("*"键拆丛枯)
rb4=new JRadioButton("/")
bg=new ButtonGroup()
bg.add(rb1)bg.add(rb2)bg.add(rb3)bg.add(rb4)
p2.add(rb1)p2.add(rb2)p2.add(rb3)p2.add(rb4)
/*第三行*/
p3=new JPanel()
l2=new JLabel("b")
tf2=new JTextField(12)
tf2.setText(""+(int)(Math.random()*100))
p3.add(l2)p3.add(tf2)
/*第四行*/
p4=new JPanel()
l3=new JLabel("结果")
tf3=new JTextField(12)
p4.add(l3)p4.add(tf3)
/*第五行*/
p5=new JPanel()
b1=new JButton("确定")
b2=new JButton("重置")
p5.add(b1)
p5.add(b2)
b1.addActionListener(this)
b2.addActionListener(this)
/*在界面上加入五个JPanel*/
this.add(p1)
this.add(p2)
this.add(p3)
this.add(p4)
this.add(p5)
/*设置页面稿郑枣参数*/
this.setLocation(800,500)
this.pack()
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE)
this.setTitle("演算")
this.setVisible(true)
}
public static void main(String[] args) {
new Demo()
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== b1)
{
jisuan()
tf3.setText(a+"")
}
if(e.getSource()== b2)
{
tf1.setText(""+(int)(Math.random()*100))
tf2.setText(""+(int)(Math.random()*100))
tf3.setText("")
tf1.requestFocus()
}
}
public void jisuan()
{
if(this.rb1.isSelected())a=Integer.parseInt(tf1.getText())+Integer.parseInt(tf2.getText())
if(this.rb2.isSelected())a=Integer.parseInt(tf1.getText())-Integer.parseInt(tf2.getText())
if(this.rb3.isSelected())a=Integer.parseInt(tf1.getText())*Integer.parseInt(tf2.getText())
if(this.rb4.isSelected())
if(Integer.parseInt(tf2.getText())==0)
a=0
else
a=Integer.parseInt(tf1.getText())/Integer.parseInt(tf2.getText())
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)