java在可视化界⾯输⼊信息,输出为excel⽂件
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.FileNotFoundException;
public class SimpleExcelWrite {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Window window=new Window();
window.show();
}
}
class Window implements MouseListener {
public JFrame frame =new JFrame("作业");
private JLabel label1=new JLabel("姓名:");
private JTextField txt1=new JTextField();
private JLabel label2=new JLabel("年龄:");
private JTextField txt2=new JTextField();
private JLabel label3=new JLabel("籍贯:");
private JTextField txt3=new JTextField();
private JButton btn1=new JButton("添加到excel⽂件");
private String text1;
private String text2;
private String text3;
private int distinguish;
public Window(){
}
public void show(){
frame.setLayout(null);
frame.setSize(470,300);
frame.setLocation(400,200);
Font font=new Font("华⽂⾏楷",Font.BOLD,20);
label1.setFont(font);
label1.ay);
label2.setFont(font);
label2.setFont(font);
label2.ay);
label3.setFont(font);
label3.ay);
txt1.setOpaque(false);
txt2.setOpaque(false);
txt3.setOpaque(false);
btn1.setContentAreaFilled(false);
btn1.setFont(font);
btn1.ay);
btn1.ateRaisedBevelBorder());
// JPanel bj = new JPanel() {
// protected void paintComponent(Graphics g) {
// Image bg;
// try {
// bg = ad(new File("src/image/开始背景.PNG")); // g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// };
label1.setBounds(100,50,100,100);
txt1.setBounds(180,90,150,20);
label2.setBounds(100,80,100,100);
txt2.setBounds(180,120,150,20);
label3.setBounds(100,110,100,100);
txt3.setBounds(180,150,150,20);
btn1.setBounds(100,200,80,20);
/
/frame.setContentPane(bj);
frame.setLayout(null);
frame.add(label1);
frame.add(txt1);
frame.add(label2);
frame.add(txt2);
frame.add(label3);
frame.add(txt3);
frame.add(btn1);
btn1.addMouseListener(this);
frame.setVisible(true);
}
public void mouseClicked(MouseEvent arg0){
Text();
Text();java开发可视化界面
Text();
FileOutputStream fos= null;
try{
fos =new FileOutputStream("1.xls");
}catch(FileNotFoundException e){
e.printStackTrace();
}
try{
createExcel(fos,text1,text2,text3);
}catch(WriteException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
e.printStackTrace();
}
}
public void createExcel(OutputStream os,String a,String b,String c)throws WriteException,IOException{ //创建⼯作薄
WritableWorkbook workbook = ateWorkbook(os);
//创建新的⼀页
WritableSheet sheet = ateSheet("First Sheet",0);
//创建要显⽰的内容,创建⼀个单元格,第⼀个参数为列坐标,第⼆个参数为⾏坐标,第三个参数为内容
Label name =new Label(0,0,"姓名");
sheet.addCell(name);
Label age =new Label(1,0,"年龄");
sheet.addCell(age);
Label home =new Label(2,0,"籍贯");
sheet.addCell(home);
Label xuexiao =new Label(0,1,a);
sheet.addCell(xuexiao);
Label zhuanye =new Label(1,1,b);
sheet.addCell(zhuanye);
Label jingzhengli =new Label(2,1,c);
sheet.addCell(jingzhengli);
//把创建的内容写⼊到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
public void mouseEntered(MouseEvent arg0){
Source()== btn1){
distinguish=1;
btn1.d);
btn1.ateLoweredBevelBorder());
}
}
public void mouseExited(MouseEvent arg0){
distinguish=0;
label1.ay);
label2.ay);
label3.ay);
txt1.setOpaque(false);
txt2.setOpaque(false);
txt3.setOpaque(false);
btn1.setContentAreaFilled(false);
btn1.ay);
btn1.ateRaisedBevelBorder());
}
public void mousePressed(MouseEvent arg0){
}
public void mouseReleased(MouseEvent arg0){
}
public String getText1(){
return text1;
}
public String getText2(){
public String getText2(){
return text2;
}
public String getText3(){
return text3;
}
public int getDistinguish(){
return distinguish;
}
}
可视化界⾯如上图所⽰,输⼊信息后,点击添加,会⽣成⼀个excel⽂件,⾥⾯有添加的信息。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论