java读写excel(jxl.jar)

有时候我们会需要做excel的报表,下面这个例子详细的给出了读写及定义样式的方法。

  1. package com.test;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import jxl.Cell;   
  7. import jxl.Sheet;   
  8. import jxl.Workbook;   
  9. import jxl.format.Border;   
  10. import jxl.format.BorderLineStyle;   
  11. import jxl.format.Colour;   
  12. import jxl.read.biff.BiffException;   
  13. import jxl.write.Label;   
  14. import jxl.write.WritableCellFormat;   
  15. import jxl.write.WritableFont;   
  16. import jxl.write.WritableSheet;   
  17. import jxl.write.WritableWorkbook;   
  18. import jxl.write.WriteException;   
  19. import jxl.write.biff.RowsExceededException;   
  20.   
  21. /**  
  22.  *   
  23.  * CopyRight (C) www.blogjava.net/ilovezmh  All rights reserved.<p>  
  24.  *   
  25.  * WuHan Inpoint Information Technology Development,Inc.<p>  
  26.  *   
  27.  * Author zhu<p>  
  28.  *  
  29.  * @version 1.0    2007-2-6  
  30.  *  
  31.  * <p>Base on : JDK1.5<p>  
  32.  *  
  33.  */  
  34.   
  35. public class JexcelSample {   
  36.     
  37.  /**  
  38.   * 写excel文件  
  39.   *  
  40.   */  
  41.  public void writeExc(File filename){   
  42.   WritableWorkbook wwb = null;   
  43.   try    
  44.   {    
  45.    wwb = Workbook.createWorkbook(filename);      
  46.   }   
  47.   catch (Exception e){    
  48.    e.printStackTrace();    
  49.   }    
  50.      
  51.   //创建Excel工作表    
  52.   WritableSheet ws = wwb.createSheet("通讯录"0);//创建sheet   
  53.   try {   
  54.    ws.mergeCells(0021);//合并单元格(左列,左行,右列,右行)从第1行第1列到第2行第3列   
  55.    Label header = new Label(00"通讯录(191026班)", getHeader());    
  56.    ws.addCell(header);//写入头   
  57.    Label l = new Label(02"姓名", getTitle());//第3行   
  58.    ws.addCell(l);   
  59.    l = new Label(12"电话", getTitle());   
  60.    ws.addCell(l);   
  61.    l = new Label(22"地址", getTitle());   
  62.    ws.addCell(l);   
  63.    l = new Label(03"小祝", getNormolCell());//第4行   
  64.    ws.addCell(l);   
  65.    l = new Label(13"1314***0974", getNormolCell());   
  66.    ws.addCell(l);   
  67.    l = new Label(23"武汉武昌", getNormolCell());   
  68.    ws.addCell(l);   
  69.    l = new Label(04"小施", getNormolCell());//第5行   
  70.    ws.addCell(l);   
  71.    l = new Label(14"1347***5057", getNormolCell());   
  72.    ws.addCell(l);   
  73.    l = new Label(24"武汉武昌", getNormolCell());   
  74.    ws.addCell(l);   
  75.    ws.setColumnView(0,20);//设置列宽   
  76.    ws.setColumnView(1,20);   
  77.    ws.setColumnView(2,40);   
  78.    ws.setRowView(0,400);//设置行高   
  79.    ws.setRowView(1,400);   
  80.    ws.setRowView(2,500);   
  81.    ws.setRowView(3,500);   
  82.    ws.setRowView(4,500);   
  83.   } catch (RowsExceededException e1) {   
  84.    e1.printStackTrace();   
  85.   } catch (WriteException e1) {   
  86.    e1.printStackTrace();   
  87.   }   
  88.      
  89.   //输出流   
  90.   try {   
  91.    wwb.write();   
  92.   } catch (IOException ex) {   
  93.    // TODO 自动生成 catch 块   
  94.    ex.printStackTrace();   
  95.   }   
  96.   //关闭流   
  97.   try {   
  98.    wwb.close();   
  99.   } catch (WriteException ex) {   
  100.    // TODO 自动生成 catch 块   
  101.    ex.printStackTrace();   
  102.   } catch (IOException ex) {   
  103.    // TODO 自动生成 catch 块   
  104.    ex.printStackTrace();   
  105.   }   
  106.   //outStream.close();   
  107.   System.out.println("写入成功!/n");   
  108.  }   
  109.     
  110.  public void readExc(File filename) throws BiffException, IOException{   
  111.   
  112.     Workbook wb = Workbook.getWorkbook(filename);   
  113.     Sheet s = wb.getSheet(0);//第1个sheet   
  114.     Cell c = null;   
  115.     int row = s.getRows();//总行数   
  116.     int col = s.getColumns();//总列数   
  117.     for(int i=0;i<row;i++){   
  118.      for(int j=0;j<col;j++){   
  119.       c = s.getCell(j,i);   
  120.       System.out.print(c.getContents()+"  ");   
  121.      }   
  122.      System.out.println();   
  123.     }      
  124.  }   
  125.     
  126.  /**  
  127.   * 设置头的样式  
  128.   * @return   
  129.   */  
  130.  public static WritableCellFormat getHeader(){   
  131.   WritableFont font = new  WritableFont(WritableFont.TIMES, 24 ,WritableFont.BOLD);//定义字体   
  132.   try {   
  133.    font.setColour(Colour.BLUE);//蓝色字体   
  134.   } catch (WriteException e1) {   
  135.    // TODO 自动生成 catch 块   
  136.    e1.printStackTrace();   
  137.   }   
  138.   WritableCellFormat format = new  WritableCellFormat(font);   
  139.   try {   
  140.    format.setAlignment(jxl.format.Alignment.CENTRE);//左右居中   
  141.    format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中   
  142.    format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//黑色边框   
  143.    format.setBackground(Colour.YELLOW);//黄色背景   
  144.   } catch (WriteException e) {   
  145.    // TODO 自动生成 catch 块   
  146.    e.printStackTrace();   
  147.   }   
  148.   return format;   
  149.  }   
  150.     
  151.  /**  
  152.   * 设置标题样式  
  153.   * @return  
  154.   */  
  155.  public static WritableCellFormat getTitle(){   
  156.   WritableFont font = new  WritableFont(WritableFont.TIMES, 14);   
  157.   try {   
  158.    font.setColour(Colour.BLUE);//蓝色字体   
  159.   } catch (WriteException e1) {   
  160.    // TODO 自动生成 catch 块   
  161.    e1.printStackTrace();   
  162.   }   
  163.   WritableCellFormat format = new  WritableCellFormat(font);   
  164.      
  165.   try {   
  166.    format.setAlignment(jxl.format.Alignment.CENTRE);   
  167.    format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);   
  168.    format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);   
  169.   } catch (WriteException e) {   
  170.    // TODO 自动生成 catch 块   
  171.    e.printStackTrace();   
  172.   }   
  173.   return format;   
  174.  }   
  175.     
  176.  /**  
  177.   * 设置其他单元格样式  
  178.   * @return  
  179.   */  
  180.  public static WritableCellFormat getNormolCell(){//12号字体,上下左右居中,带黑色边框   
  181.   WritableFont font = new  WritableFont(WritableFont.TIMES, 12);   
  182.   WritableCellFormat format = new  WritableCellFormat(font);   
  183.   try {   
  184.    format.setAlignment(jxl.format.Alignment.CENTRE);   
  185.    format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);   
  186.    format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);   
  187.   } catch (WriteException e) {   
  188.    // TODO 自动生成 catch 块   
  189.    e.printStackTrace();   
  190.   }   
  191.   return format;   
  192.  }   
  193.     
  194.  public static void main(String[] args) throws IOException, BiffException{   
  195.   JexcelSample js = new JexcelSample();   
  196.   File f = new File("D://address.xls");   
  197.   f.createNewFile();   
  198.   js.writeExc(f);   
  199.   js.readExc(f);   
  200.  }   
  201.   
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值