jxl操作EXCEL

/*
 * NewJPanel.java
 *
 * Created on 2008年1月24日, 上午 10:51
 
*/


package  hsf;

import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.sql.Connection;
import  java.sql.ResultSet;
import  java.sql.Statement;
import  javax.swing.JFileChooser;
import  javax.swing.JFrame;
import  javax.swing.JOptionPane;
import  jxl.CellType;
import  jxl.Sheet;
import  jxl.Workbook;
import  jxl.format.CellFormat;
import  jxl.read.biff.BiffException;
import  jxl.write.Border;
import  jxl.write.BorderLineStyle;
import  jxl.write.Label;
import  jxl.write.WritableCell;
import  jxl.write.WritableCellFormat;
import  jxl.write.WriteException;

/**
 *
 * 
@author  joelhe
 
*/

public   class  NewJPanel  extends  javax.swing.JPanel  {
    
    
/** Creates new form NewJPanel */
    JFrame j
=null;
    Dbcon con
=new Dbcon();
    
public NewJPanel(JFrame j) {
        initComponents();
        
this.j=j;
    }

    
    
/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     
*/

    
// <editor-fold defaultstate="collapsed" desc=" 建置的代碼 ">                          
    private void initComponents() {
        jLabel1 
= new javax.swing.JLabel();
        jScrollPane1 
= new javax.swing.JScrollPane();
        jTable1 
= new javax.swing.JTable();
        jLabel3 
= new javax.swing.JLabel();
        jTextField2 
= new javax.swing.JTextField();
        jButton3 
= new javax.swing.JButton();
        jButton1 
= new javax.swing.JButton();
        jButton2 
= new javax.swing.JButton();

        jLabel1.setFont(
new java.awt.Font("新細明體"124));
        jLabel1.setText(
"ROHS檢驗報告");

        jTable1.setFont(
new java.awt.Font("新細明體"016));
        jTable1.setModel(
new javax.swing.table.DefaultTableModel(
            
new Object [][] {
                
{nullnullnullnullnullnullnullnullnullnullnullnull},
                
{nullnullnullnullnullnullnullnullnullnullnullnull},
                
{nullnullnullnullnullnullnullnullnullnullnullnull},
                
{nullnullnullnullnullnullnullnullnullnullnullnull},
                
{nullnullnullnullnullnullnullnullnullnullnullnull}
            }
,
            
new String [] {
                
"MISC""LINE""SAMPLE""TIME""LOCATION""Cd""Pb""Hg""Cr""Br""RESULT""INSPECT"
            }

        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel3.setFont(
new java.awt.Font("新細明體"016));
        jLabel3.setText(
"MISC:");

        jTextField2.setFont(
new java.awt.Font("新細明體"016));

        jButton3.setText(
"查詢");
        jButton3.addActionListener(
new java.awt.event.ActionListener() {
            
public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }

        }
);

        jButton1.setText(
"jButton1");

        jButton2.setText(
"導出EXCEL");
        jButton2.addActionListener(
new java.awt.event.ActionListener() {
            
public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }

        }
);

        javax.swing.GroupLayout layout 
= new javax.swing.GroupLayout(this);
        
this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 
692, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(
169169169)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 
49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 
183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addGap(
101101101)
                .addComponent(jButton2)
                .addContainerGap(
30, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(
229229229)
                .addComponent(jLabel1)
                .addContainerGap(
299, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(
454545)
                .addComponent(jLabel1)
                .addGap(
282828)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 
121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(
323232)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jButton3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(
75, Short.MAX_VALUE))
        );
    }
// </editor-fold>                        

    
public void addcell(jxl.write.WritableSheet wws,int m,String a,String b,String c,String d,String e,String f){
        
try {
            WritableCellFormat format 
= new  WritableCellFormat();
            format.setBorder(Border.ALL,BorderLineStyle.MEDIUM);
            wws.addCell(
new jxl.write.Label(3,m,a,format));
            wws.addCell(
new jxl.write.Label(4,m,b,format));
            wws.addCell(
new jxl.write.Label(5,m,c,format));
            wws.addCell(
new jxl.write.Label(6,m,d,format));
            wws.addCell(
new jxl.write.Label(8,m,e,format));
            wws.addCell(
new jxl.write.Label(9,m,f,format));
        }
 catch (WriteException ex) {
            ex.printStackTrace();
        }

    }


    
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
try {
            JFileChooser jFileChoose
=new javax.swing.JFileChooser();
            FileOutputStream fos;
            String cell1
="";
            String cell2
="";
            String cell3
="";
            String cell4
="";
            String cell5
="";
            String tempmisc
="";
            String templocation
="";
            String tempCd
="";
            String tempPb
="";
            String tempHg
="";
            String tempCr
="";
            String tempBr
="";
            String tempResult
="";
            WritableCellFormat format 
= new  WritableCellFormat();
            format.setBorder(Border.ALL,BorderLineStyle.MEDIUM);
            jFileChoose.showSaveDialog(
null);
            File file
=jFileChoose.getSelectedFile();
            fos
=new FileOutputStream(file+".xls");
            Workbook rw;
            rw 
= Workbook.getWorkbook(new File("HSF.xls"));
            jxl.write.WritableWorkbook wb
=Workbook.createWorkbook(fos,rw);
            jxl.write.WritableSheet ws
=wb.getSheet(0);
            cell1
=(String) jTable1.getValueAt(0,0);
            
if(cell1==null){
                cell1
="";
            }

            System.out.println(cell1);
            
for(int k=0;k<5;k++){
                cell2
=(String) jTable1.getValueAt(k,1);
                
if(cell2==null){
                    cell2
="";
                }
else if(cell2.trim().equals("")){
                }
else{
                    
break;
                }

            }

            
for(int k=0;k<5;k++){
                cell3
=(String) jTable1.getValueAt(k,2);
                
if(cell3==null){
                    cell3
="";
                }
else if(cell3.trim().equals("")){
                }
else{
                    
break;
                }

            }

            cell4
=(String) jTable1.getValueAt(0,3);
            
if(cell4==null){
                cell4
="";
            }

            cell5
=(String) jTable1.getValueAt(0,11);
            
if(cell5==null){
                cell5
="";
            }
           
                System.out.println(jTable1.getValueAt(k,4).toString());
                if(jTable1.getValueAt(k,4).toString().trim().indexOf("14")!=-1){
//            try {
                ws.addCell(new jxl.write.Label(9,2,cell4));
                ws.addCell(
new jxl.write.Label(2,3,cell2,format));
                ws.addCell(
new jxl.write.Label(7,3,cell3,format));
                ws.addCell(
new jxl.write.Label(7,4,cell1,format));
                ws.addCell(
new jxl.write.Label(1,36,cell5));
//            } catch (WriteException ex) {
//                ex.printStackTrace();
//            }
            for(int n=0;n<5;n++){
                tempmisc
=(String) jTable1.getValueAt(n,0);
                templocation
=(String) jTable1.getValueAt(n,4);
                tempCd
=(String) jTable1.getValueAt(n,5);
                tempPb
=(String) jTable1.getValueAt(n,6);
                tempHg
=(String) jTable1.getValueAt(n,7);
                tempCr
=(String) jTable1.getValueAt(n,8);
                tempBr
=(String) jTable1.getValueAt(n,9);
                tempResult
=(String) jTable1.getValueAt(n,10);
                
if(tempmisc==null){
                    
break;
                }
else if(tempmisc.trim().equals("")){
                    
break;
                }
else{
                    
if(templocation.indexOf("PCI1 SLOT")!=-1){
                       addcell(ws,
7,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCI2 SLOT")!=-1){
                      addcell(ws,
8,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCI3 SLOT")!=-1){
                       addcell(ws,
9,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCIE16 SLOT")!=-1){
                       addcell(ws,
10,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCIE1 SLOT")!=-1){
                       addcell(ws,
11,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                   }
else if(templocation.indexOf("VGA")!=-1){
                       addcell(ws,
12,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                   }
else if(templocation.indexOf("COM")!=-1){
                       addcell(ws,
13,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("LPT")!=-1){
                      addcell(ws,
14,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                   }
else if(templocation.indexOf("ATX1 SLOT")!=-1){
                       addcell(ws,
15,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("IDE SLOT")!=-1){
                       addcell(ws,
16,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("FDD SLOT")!=-1){
                       addcell(ws,
17,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("DIMM SLOT")!=-1){
                       addcell(ws,
18,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("USB LAN")!=-1){
                       addcell(ws,
19,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("AUDIO")!=-1){
                       addcell(ws,
20,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PSKBM")!=-1){
                       addcell(ws,
21,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCI1 底板")!=-1){
                       addcell(ws,
22,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCI2 底板")!=-1){
                       addcell(ws,
23,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCI3 底板")!=-1){
                       addcell(ws,
24,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCIE16底板")!=-1){
                       addcell(ws,
25,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("PCIE1底板")!=-1){
                       addcell(ws,
26,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("DIMM 底板")!=-1){
                       addcell(ws,
27,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("IDE 底板")!=-1){
                      addcell(ws,
28,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("FDD 底板")!=-1){
                      addcell(ws,
29,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("序號 LABEL")!=-1){
                      addcell(ws,
30,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("制程 LABEL")!=-1){
                       addcell(ws,
31,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("卡號 LABEL")!=-1){
                      addcell(ws,
32,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }
else if(templocation.indexOf("機種 LABEL")!=-1){
                       addcell(ws,
33,tempCd,tempPb,tempHg,tempCr,tempBr,tempResult);
                    }

                    
                }

                
            }

            
            
            wb.write();
            wb.close();
            rw.close();
            fos.close();
        }
catch (WriteException ex) {
                ex.printStackTrace();
        }
catch (BiffException ex) {
            ex.printStackTrace();
        }
 catch (IOException ex) {
            ex.printStackTrace();
        }

    
// TODO 將在此處加入您的處理代碼:
    }
                                        

    
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
if(jTextField2.getText().trim().equals("")){
            JOptionPane.showMessageDialog(
this,"請先輸入MISC","提示",JOptionPane.INFORMATION_MESSAGE);
        }
else{
             
try {
                    Connection conn
=con.getConnection();
                    Statement stmt
=conn.createStatement();
                    String misc
=jTextField2.getText().trim();
                    String sql
="select MISC,LINE,SAMPLE,TIME_T,LOCATION_T,CD,PB,HG,CR,BR,RESULT,INSPECTOR FROM " +
                            
"SFISUSER.ECSM_OQC_ROHS WHERE TRIM(MISC)='"+misc+"'";
//                    System.out.println(sql);
                    ResultSet rs=stmt.executeQuery(sql);
                    
int i=0;
                    
while(rs.next()){
                        
if(i>=5){
                            
break;
                        }

                        jTable1.setValueAt(rs.getString(
1),i,0);
                        jTable1.setValueAt(rs.getString(
2),i,1);
                        jTable1.setValueAt(rs.getString(
3),i,2);
                        jTable1.setValueAt(rs.getString(
4),i,3);
                        jTable1.setValueAt(rs.getString(
5),i,4);
                        jTable1.setValueAt(rs.getString(
6),i,5);
                        jTable1.setValueAt(rs.getString(
7),i,6);
                        jTable1.setValueAt(rs.getString(
8),i,7);
                        jTable1.setValueAt(rs.getString(
9),i,8);
                        jTable1.setValueAt(rs.getString(
10),i,9);
                        jTable1.setValueAt(rs.getString(
11),i,10);
                        jTable1.setValueAt(rs.getString(
12),i,11);
//                        System.out.println(rs.getString(1));
                        i++;
                    }

                    rs.close();
                    stmt.close();
                    conn.close();
                    
                }

                
catch(Exception e) {
                    e.printStackTrace();
                }
  
        }

        
        
// TODO 將在此處加入您的處理代碼:
    }
                                        
    
    
    
// 變數宣告 - 不進行修改                     
    private javax.swing.JButton jButton1;
    
private javax.swing.JButton jButton2;
    
private javax.swing.JButton jButton3;
    
private javax.swing.JLabel jLabel1;
    
private javax.swing.JLabel jLabel3;
    
private javax.swing.JScrollPane jScrollPane1;
    
private javax.swing.JTable jTable1;
    
private javax.swing.JTextField jTextField2;
    
// 變數宣告結束                   
    
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值