效果如图。
点击上传按钮,能够选择本机的excl文件。获取路径。
输入框中输入数字ID
然后点击查询。(如果ID没有,或者文件路径没有,提示需要输入ID,或者需要选择文件)
点查询后,能够将ID和路径传给查询java。
查询的java已经写好了。
现在需要的是怎么建立这个窗体,来实现功能。
下面是窗体代码(需要修改的,我只是实现了界面排版):
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import excel.ExcelOperate;
public class AbsolutePosition extends JFrame implements ActionListener {
private JButton b1;
private JScrollPane pane;
private JTextArea area;
public AbsolutePosition() {
setTitle("本窗体使用绝对布局"); // 设置该窗体的标题
setLayout(null); // 使该窗体取消布局管理器设置
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width -500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2, 400, 400); // 绝对定位窗体的位置与大小
Container c = getContentPane(); // 创建容器对象
Label lb1 = new Label ("选择文件:");
b1 = new JButton("点击选择"); // 创建按钮
b1.addActionListener(this);//b1这个实例(按钮等)添加事件监听接口,this表示当前类的对象,在一个类里,你不需要new他的实例就直接可以用this调用它的方法和属性
area = new JTextArea();
pane = new JScrollPane(area);
Label lb2 = new Label ("员工ID:");
JButton b2 = new JButton("查询"); // 创建按钮
JTextField jt=new JTextField();//创建输入框
lb1.setBounds(30, 30, 120, 30); // 设置文本的位置与大小
lb2.setBounds(30, 80, 120, 30);
b1.setBounds(140, 30, 120, 30); // 设置按钮的位置与大小
jt.setBounds(140, 80, 100, 30);//设置文本框位置和大小
b2.setBounds(140, 150, 100, 30);
c.add(b1); // 将按钮添加到容器中
this.add(pane);
c.add(b2);
c.add(lb1);
c.add(jt);
c.add(lb2);
setVisible(true); // 使窗体可见
// 设置窗体关闭方式
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
// 按钮事件
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();// 文件选择对话框
chooser.setAcceptAllFileFilterUsed(false);// 取消所有文件过滤项
chooser.setFileFilter(new FileNameExtensionFilter("Excel文件", "xls"));// 设置只过滤扩展名为.xls的Excel文件
//String firstNum = jt.getText();//获取输入框值
int i = chooser.showOpenDialog(this);// 打开窗口
if (i == JFileChooser.APPROVE_OPTION) {
this.setLocation(0, 0);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
//new ExcelOperate(area, chooser.getSelectedFile().getAbsolutePath())//调用ExcelOperate
// .parseExcel();
//String B=(String)chooser.getSelectedFile().getAbsolutePath();//B为文件路径
//System.out.println(B);
}
}
public static void main(String[] args) {
new AbsolutePosition();
}
}
下面是查询主代码
public static void main(String[] args) throws Exception {
Car fy = new Car();
URL url = new java.net.URL("http://********");//这个是费用接口
//excel路径
String excel_file_path = "D:/cheliang.xls";
//获取excel数据
String A= new String();
String id="16215288";//员工ID
List<LinkedHashMap<String,Object>> dataList = fy.getMsgInfoFromExcel(excel_file_path);
for(LinkedHashMap<String,Object> dataMap : dataList){
String _cjh = dataMap.get("车架号(必填)")==null?"":dataMap.get("车架号(必填)").toString();
String _xm = dataMap.get("被保险人(必填)")==null?"":dataMap.get("被保险人(必填)").toString();
String _tel = dataMap.get("手机号")==null?"":dataMap.get("手机号").toString();
StringBuffer str = new StringBuffer();
str.append("<Request><VinNo>");
str.append(_cjh);
str.append("</VinNo><CustomerName>");
str.append(_xm);
str.append("</CustomerName><Mobile>");
str.append(_tel);
str.append("</Mobile><IdentityCard></IdentityCard><LoginID>");
str.append(id);
str.append("</LoginID></Request>");
URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
String msg = str.toString();
//调用发送方法
//fy.sendMsg(msg, dataOut, con);
//String carInfo=fy.sendMsg(msg, dataOut, con);
String carInfo=sendMsg(msg, dataOut, con);
if(carInfo.equals("车架号校验失败")) {
A=A+"<Response> <VinNo>"+_cjh+"</VinNo> <CustomerName>"+_xm+"</CustomerName> <Mobile>"+carInfo+"</Mobile> <IdentityCard></IdentityCard> <ReturnMsgList> <ReturnMsg> <CarID></CarID> <CI__PolicyNo></CI__PolicyNo> <BZ__PolicyNo></BZ__PolicyNo> <LicenseNo></LicenseNo> <EngineNo></EngineNo> <FamilyType></FamilyType> <FamilyNewName></FamilyNewName> <EndDate></EndDate> <InsuredCode></InsuredCode> <ClaimCount></ClaimCount> <ClaimOldCount></ClaimOldCount> <NodamYearsBI></NodamYearsBI> <Age></Age> <ModelCode></ModelCode> <Message>"+carInfo+"</Message> </ReturnMsg> </ReturnMsgList></Response>";
}/*else if(carInfo == null || str.length() <= 0){
A=A+"<Response> <VinNo>"+_cjh+"</VinNo> <CustomerName>"+_xm+"</CustomerName> <Mobile>"+"获取为空"+"</Mobile> <IdentityCard></IdentityCard> <ReturnMsgList> <ReturnMsg> <CarID></CarID> <CI__PolicyNo></CI__PolicyNo> <BZ__PolicyNo></BZ__PolicyNo> <LicenseNo></LicenseNo> <EngineNo></EngineNo> <FamilyType></FamilyType> <FamilyNewName></FamilyNewName> <EndDate></EndDate> <InsuredCode></InsuredCode> <ClaimCount></ClaimCount> <ClaimOldCount></ClaimOldCount> <NodamYearsBI></NodamYearsBI> <Age></Age> <ModelCode></ModelCode> <Message>"+carInfo+"</Message> </ReturnMsg> </ReturnMsgList></Response>";
}*/else {
A=A+carInfo;
}
OutExcel oed = new OutExcel();
oed.outExcel(A);
dataOut.flush();
dataOut.close();
}
}
主程序中的:excel_file_path和id希望能通过窗口获取过来。当然,如果用页面实现也可以。
欢迎大神私聊解决