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

主程序:

/*   
 */
 

 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 + " ");
 }
 }

阅读更多
下一篇编写的一个.bat文件,用于清除系统中的垃圾
想对作者说点什么? 我来说一句

jxl.jar包简介/java操作excel

2010年03月05日 119KB 下载

poi,jxl.jar

2017年07月19日 2.58MB 下载

java操作 Excel 的jar包--jxl.jar

2009年11月03日 1.59MB 下载

jxl.jar_java操作excel表格的jar包

2018年01月12日 667KB 下载

poi操作excel java源代码分享

2011年08月30日 3KB 下载

操作excel的java包

2015年12月31日 736KB 下载

excel 导出用的jxl.jar

2010年03月24日 1.99MB 下载

java对excel操作

2008年12月23日 640KB 下载

Java操作Exceljxl.jar

2012年09月21日 622KB 下载

Android 开发Excel需要的jar包

2016年12月18日 1.45MB 下载

没有更多推荐了,返回首页

关闭
关闭