关闭

今天写了个用jxl.jar操作excel文件的java程序

1027人阅读 评论(1) 收藏 举报

主程序:

/*   
 */
 

 import java.awt.* ;
 import java.awt.event.* ;
 import javax.swing.* ;
 import java.io.File;
 import java.io.IOException;
 import jxl.*;
 
 @SuppressWarnings("serial")
public class mainForm extends JFrame
 {
  /**
  *
  */
 // initialize general buttons
  JButton generalBtn = new JButton("begin") ;
  JButton exitBtn = new JButton("exit") ;
  JLabel message = new JLabel( "Press begin to general fomulas." );
  JLabel page = new JLabel( "Input page numbers:" );
  JTextField pageNum = new JTextField(2);
  JLabel col = new JLabel( "Input colume numbers:" );
  JTextField colNum = new JTextField(2);
  JLabel row = new JLabel( "Input row numbers:" );
  JTextField rowNum = new JTextField(2);
  
  public mainForm()
  {
   setLayout( new BorderLayout() ) ;
   
   JPanel panel = new JPanel() ;
   panel.setLayout( new GridLayout(5, 4) ) ;
   panel.add( page );
   panel.add( pageNum );
   panel.add( row );
   panel.add( rowNum );
   panel.add( col );
   panel.add( colNum );
   panel.add( generalBtn ) ;
   panel.add( exitBtn ) ;
   panel.add( message );
   add( panel, BorderLayout.CENTER ) ;
 
      /*
       *  generalBtn and exitBtn use one action listener
       */
      generalBtn.addActionListener( new operListener() ) ;
      exitBtn.addActionListener( new operListener() ) ;
 }
 
 
 /**
  *  operListener:
  *  to define the general button actionlistener
  *  through the button's clicking, system run
  *  different way.
  *
  */
 class operListener implements ActionListener
 {
  public void actionPerformed( ActionEvent evtOp )
  {
   // get the name of each button
   // when it is pressed
   String buttonName = evtOp.getActionCommand() ;
   if( buttonName.equals("begin") )
   {
    // firstly, general three random numbers
    begin();
   }
   if( buttonName.equals("exit") )
   {
    System.exit(0) ;
   }   
  }
 }
   
 public void begin()
 {
  int page = Integer.parseInt( pageNum.getText() );
  int rows = Integer.parseInt( rowNum.getText() );
  int cols = Integer.parseInt( colNum.getText() );
  try
  {
   message.setText( "开始!!" );
   String targetfile = "formula.xls" ;//输出的Excel文件URL
   jxl.write.WritableFont wf = new jxl.write.WritableFont(jxl.write.WritableFont.TIMES, 18, jxl.write.WritableFont.BOLD, true);
   jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat( wf );
   jxl.write.WritableWorkbook wwb = Workbook.createWorkbook( new File( targetfile ) );//创建可写工作薄
   for( int p = 0; p < page; p ++ )
   {
    jxl.write.WritableSheet ws = wwb.createSheet("fornula" + p, p);
    for( int i = 0; i < rows; i ++ )
    {
     for( int j = 0; j < cols; j ++ )
     {
      formula formula = new formula();
      formula.generalFormula();
      ws.addCell(new jxl.write.Label(j, i, formula.toString(), wcfF));
     }
    }
   }
   wwb.write();
   wwb.close();
   message.setText( "完成" );
  }catch( IOException ioe )
  {
   message.setText( "写入文件异常!!" );
  }catch(jxl.write.biff.RowsExceededException ree)
  {
   message.setText( "写入文件异常!!" );
  }catch( jxl.write.WriteException we )
  {
   message.setText( "写入文件异常!!" );
  }
 }

 public static void main( String[] args )
 {
  mainForm form = new mainForm() ;
  form.setSize( 400, 200 ) ;
  form.setVisible(true) ;
  form.setResizable(false) ; // the users can not modify the window
  form.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
 }
 }

子程序

/*
 * general one formula, like num1 + num2 + num3 = result   
 */
 

 
 public class formula
 {  
  // defined three radome numbers
 private int num1, num2, num3;
 // record the formula string
 private String resultRec;
 private long result;
 
  public formula()
  {
   num1 = (int)(Math.random()*1000);
  num2 = (int)(Math.random()*1000);
  num3 = (int)(Math.random()*1000);
  resultRec = null;
 }
 
 // when geeral formula have some errors, use this function regeneral formula
 public void reGeneralNumbers()
 {
   num1 = (int)(Math.random()*1000 + 1);
  num2 = (int)(Math.random()*1000 + 1);
  num3 = (int)(Math.random()*1000 + 1);
  resultRec = null;
  generalFormula();  
 }
  
 public void generalFormula()
 {
  int operation = (int)(Math.random()*15) + 1;
  
  switch( operation )
  {
   // num1 + num2 + num3
   case 1:
       result = num1 + num2 + num3;
    resultRec = num1 + "+" + num2 + "+" + num3 + "=" + "  ";
    break;
   // num1 + num2 - num3
   case 2:
    result = num1 + num2 - num3;
    resultRec = num1 + "+" + num2 + "-" + num3 + "=" + "  ";
    break;
   // num1 + num2 * num3
   case 3:
    num2 = formateNum( num2 );
    num3 = formateNum( num3 );
    result = num1 + num2 * num3;
    resultRec = num1 + "+" + num2 + "*" + num3 + "=" + "  ";
    break;
   // num1 + num2 / num3
   case 4: 
    if( judgeDiv(num2,num3) )
    { 
     result = num1 + num2 / num3;
     resultRec = num1 + "+" + num2 + "/" + num3 + "=" + "  ";
    }
    else
     reGeneralNumbers();
    break;
   // num1 - num2 + num3
   case 5:
    result = num1 - num2 + num3;
    resultRec = num1 + "-" + num2 + "+" + num3 + "=" + "  ";
    break;
   // num1 - num2 - num3
   case 6:
    result = num1 - num2 - num3;
    resultRec = num1 + "-" + num2 + "-" + num3 + "=" + "  ";
    break;
   // num1 - num2 * num3
   case 7:
    num3 = formateNum( num3 );
    num2 = formateNum( num2 );
    result = num1 - num2 * num3;
    resultRec = num1 + "-" + num2 + "*" + num3 + "=" + "  ";
    break;
   // num1 - num2 / num3
   case 8:
    if( judgeDiv(num2,num3) )
    { 
     result = num1 - num2 / num3;
     resultRec = num1 + "-" + num2 + "/" + num3 + "=" + "  ";
    }
    else
     reGeneralNumbers();
    break;
   // num1 * num2 + num3
   case 9: 
    num1 = formateNum( num1 );
    num2 = formateNum( num2 );
    result = num1 * num2 + num3;
    resultRec = num1 + "*" + num2 + "+" + num3 + "=" + "  ";
    break;
   // num1 * num2 - num3
   case 10:
    num1 = formateNum( num1 );
    num2 = formateNum( num2 );
    result = num1 * num2 - num3;
    resultRec = num1 + "*" + num2 + "-" + num3 + "=" + "  ";
    break;
   // num1 * num2 * num3
   case 11:
    num1 = formateNum( num1 );
    num2 = formateNum( num2 );
    num3 = formateNum( num3 );
    result = num1 * num2 * num3;
    resultRec = num1 + "*" + num2 + "*" + num3 + "=" + "  ";
    break;
   // num1 * num2 / num3
   case 12:
    num1 = formateNum( num1 );
    num2 = formateNum( num2 );
    int temp = num1 * num2;
    if( judgeDiv( temp, num3 ) )
    { 
     result = temp / num3;
     resultRec = num1 + "*" + num2 + "/" + num3 + "=" + "  ";
    }
    else
     reGeneralNumbers(); 
    break;
   // num1 / num2 + num3
   case 13:
    if( judgeDiv(num1,num2) )
    {
     result = num1 / num2 + num3;
     resultRec = num1 + "/" + num2 + "+" + num3 + "=" + "  ";
    }
    else
     reGeneralNumbers();
    break;
   // num1 / num2 - num3
   case 14:
    if( judgeDiv(num1,num2) )
    {
     result = num1 / num2 - num3;
     resultRec = num1 + "/" + num2 + "-" + num3 + "=" + "  ";
    }
    else
     reGeneralNumbers();
    break;
   // NUM1 / num2 * num3
   case 15:
    if( judgeDiv(num1,num2) )
    {
     temp = formateNum( num1/num2 );
     num3 = formateNum( num3 );
     result = temp * num3;
     resultRec = num1 + "/" + num2 + "*" + num3 + "=" + "  ";
    }
    else
     reGeneralNumbers();
    break;
   // num1 / num2 / num3
   case 16:
    if( judgeDiv(num1,num2) )
    {
     temp = num1 / num2;
     if( judgeDiv(temp,num3) )
     { 
      result = temp / num3;
      resultRec = num1 + "/" + num2 + "/" + num3 + "=" + "  ";
     }
     else
      reGeneralNumbers();
    }
    else
     reGeneralNumbers();
    break;    
  }
  if( result > 10000 || result < 0 )
  {
   reGeneralNumbers();
  }
 }
 
 public int formateNum( int a )
 {
  if( a > 10 )
  {
   if( a > 300 )
    return (a /30 * 10);
   return ( a - (a%10) );
  }
  else
   return a;
 }
 
 public  boolean judgeDiv(int a, int b)
 {
  if( b != 0 )
  {
   if( a%b == 0 && a/b != 0)
       return true;
   else
    return false;
  }
  else
  {
   return false;
  }
 }
 
 public String toString()
 {
  return resultRec;
 }
 
 public String resultString()
 {
  return (result + " ");
 }
 }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3458次
    • 积分:63
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章存档
    最新评论