java弹窗导入导出文件

package com.action.frame;

import java.awt.Container;
import java.awt.Font;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import com.action.ExcelUnit.XxlsServices;
import com.action.service.PCBExportService;
import com.action.service.PCBRule;
import com.action.service.PCBRuleService;

public class PCB extends JFrame implements ActionListener {

private static final long serialVersionUID = -4026600690895973169L;
private JButton bottom;
private JButton bottom1;
private JButton bottom2;
private JButton bottom3;
private JPanel contentPane;
private Label label;

private JScrollPane scrollMypane;

public static void main(String[] args) {

PCB example = new PCB();
example.setTitle("PCB");
example.setBounds(250, 100, 900,600);
example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
example.setVisible(true);
}


/**
* 默认构造器
*/
public PCB() {
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
bottom = new JButton("导入到数据库");
bottom1 = new JButton("导出到Excel");
bottom2 = new JButton("关 闭");
bottom3 = new JButton("计算");
contentPane.add(bottom);
contentPane.add(bottom1);
contentPane.add(bottom2);
contentPane.add(bottom3);
bottom.setBounds(100, 143, 200, 40);
bottom1.setBounds(540, 143, 200, 40);
bottom2.setBounds(360, 443, 100, 40);
bottom3.setBounds(320, 143, 200, 40);
bottom.addActionListener(this);
bottom1.addActionListener(this);
bottom2.addActionListener(this);
bottom3.addActionListener(this);
font();
}

public void font() {
Container container = getContentPane();
container.setLayout(null);
label = new Label("数据导入中...", Label.CENTER);
label.setFont(new Font("Serif",Font.PLAIN,24));
scrollMypane = new JScrollPane(label);
scrollMypane.setBounds(new Rectangle(260, 270, 370, 100));
container.add(scrollMypane);
label.setVisible(false);
}


/**
* 监听事件
*/
public void actionPerformed(ActionEvent e) {
//导入到数据库
if (e.getSource() == bottom) {
readExcelToDatabase();
}
//导出
if (e.getSource() == bottom1) {
String path = null;
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("请选择导出的目录...");
fc.setApproveButtonText("确定");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)) {
label.setVisible(true);
label.setText("数据导出中");
path = fc.getSelectedFile().getPath();
new PCBExportService().dealExportCore(path);
label.setText("导出完毕");
}
}

if (e.getSource() == bottom2) {
System.exit(0);
}
//计算
if (e.getSource() == bottom3) {
label.setVisible(true);
label.setText("数据计算中");
new PCBRuleService().dealPCBRuleCore();
try {
PCBRule p = new PCBRule();
p.replaceRule();
p.cabinetSum();
} catch (SQLException ex) {
ex.printStackTrace();
}
label.setText("数据计算完毕");
}

}

/**
* 写数据到数据库
*/
public void readExcelToDatabase() {
//
String path = null;
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("请选择要上传的文件...");
fc.setApproveButtonText("确定");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)) {
path = fc.getSelectedFile().getPath();
// 导入(计算时间);
label.setVisible(true);
System.out.println("数据导入中...");
try {
XxlsServices howto = new XxlsServices();
howto.process(path);
howto.close();
} catch (Exception e) {
label.setText("数据导入出错");
e.printStackTrace();
}
label.setText("导入完毕");
}
}

}

猜你喜欢

转载自xiyuliuguang.iteye.com/blog/2035365