风之语

posts(201) comments(182) trackbacks(1)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • ajax(1)
  • android(1)
  • apache(1)
  • AppFuse(1)
  • BIRT(1)
  • iText(1)
  • JSF(8)
  • kettle(1)
  • linux(5)
  • mac(1)
  • maven(1)
  • MSTR(3)
  • Open XML(1)
  • Oracle(21)
  • RCP
  • Struts(2)
  • Struts2(2)
  • SybaseIQ(6)
  • tapestry
  • tomcat(2)
  • weblogic(1)
  • webservice(1)
  • weka(1)
  • 云计算(1)
  • 收藏(31)
  • 数据仓库(11)
  • 架构设计(3)
  • 生活(2)
  • 集群(1)
  • 项目管理(6)

随笔档案

  • 2012年12月 (4)
  • 2012年11月 (1)
  • 2012年7月 (2)
  • 2011年8月 (1)
  • 2011年7月 (1)
  • 2011年3月 (1)
  • 2010年12月 (2)
  • 2010年11月 (4)
  • 2010年10月 (3)
  • 2010年9月 (5)
  • 2010年8月 (1)
  • 2010年7月 (4)
  • 2010年3月 (1)
  • 2010年2月 (3)
  • 2009年12月 (3)
  • 2009年11月 (4)
  • 2009年9月 (3)
  • 2009年6月 (5)
  • 2009年5月 (3)
  • 2009年4月 (2)
  • 2009年3月 (5)
  • 2009年2月 (4)
  • 2009年1月 (2)
  • 2008年11月 (2)
  • 2008年9月 (1)
  • 2008年7月 (2)
  • 2008年6月 (4)
  • 2008年5月 (6)
  • 2008年4月 (1)
  • 2008年3月 (1)
  • 2007年12月 (2)
  • 2007年11月 (5)
  • 2007年10月 (2)
  • 2007年9月 (3)
  • 2007年8月 (3)
  • 2007年4月 (1)
  • 2007年3月 (2)
  • 2007年2月 (2)
  • 2007年1月 (2)
  • 2006年12月 (3)
  • 2006年8月 (2)
  • 2006年7月 (2)
  • 2006年6月 (2)
  • 2006年4月 (2)
  • 2006年3月 (1)
  • 2006年2月 (3)
  • 2006年1月 (6)
  • 2005年12月 (6)
  • 2005年11月 (4)
  • 2005年10月 (17)
  • 2005年9月 (25)
  • 2005年8月 (16)
  • 2005年7月 (8)

相册

  • 技术图片

收藏夹

  • java

link

My wife

  • My wife's blog

最新随笔

  • 1. apache修改最大连接并用ab网站压力测试
  • 2. Vm虚拟机访问本地硬盘文件
  • 3. NFS文件无法写入的权限问题
  • 4. weblogic设置上传文件访问权限
  • 5. 在android上动态实现ichartjs的3D柱形图
  • 6. 使用Oracle trunc 来指定精确的年月日时分秒
  • 7. Quartz 2.1.5 web应用配置
  • 8. LoginAny 使用笔记
  • 9. MyEclipse 无响应的几种解决办法
  • 10. java.sql.SQLException: No more data to read from socket

搜索

  •  

积分与排名

  • 积分 - 402197
  • 排名 - 139

最新评论

  • 1. re: 使用Oracle trunc 来指定精确的年月日时分秒[未登录]
  • 政治
  • --张三
  • 2. re: 干掉流氓软件vrvrf_c.exe,vrvedp_m.exe[未登录]
  • `
  • --1
  • 3. re: 无需刻录DMG光盘,教你在VMWare下安装MAC OS X Snow Leopard 10.6
  • 我走到了换DMG映像的时候,然后就没有反应了,这个是什么情况,是不是我的映像文件有问题,还是。。。。
  • --玉竹常青
  • 4. re: LoginAny 使用笔记
  • 怎么没一个人留言,现在来是不是在挖坟啊
  • --ellipse
  • 5. MSTR web应用的部署[未登录]
  • 请问,如何用oracle Application Server服务器部署mstr web应用?
  • --rocky

阅读排行榜

评论排行榜

View Post

jxl操作excel例子(转)

jxl操作excel例子(转)

package com.dbs.vote.common.test.excel;

import java.io.File;  
import java.io.FileOutputStream;  
import java.io.OutputStream;  
import java.util.ArrayList;  
import java.util.Date;  
 
import jxl.Cell;  
import jxl.CellType;  
import jxl.Sheet;  
import jxl.Workbook;  
import jxl.WorkbookSettings;  
import jxl.format.Alignment;  
import jxl.format.Border;  
import jxl.format.BorderLineStyle;  
import jxl.format.Colour;  
import jxl.format.VerticalAlignment;  
import jxl.write.Formula;  
import jxl.write.Label;  
import jxl.write.NumberFormat;  
import jxl.write.WritableCellFeatures;  
import jxl.write.WritableCellFormat;  
import jxl.write.WritableFont;  
import jxl.write.WritableSheet;  
import jxl.write.WritableWorkbook;  
import jxl.write.WriteException;  
 
public class JExcelUtils {  
 
    /** 
     * 生成Excel文件 
     * @param path         文件路径 
     * @param sheetName    工作表名称 
     * @param dataTitles   数据标题 
     */ 
   public void createExcelFile(String path,String sheetName,String[] dataTitles){  
       WritableWorkbook workbook;  
       try{  
           OutputStream os=new FileOutputStream(path);   
           workbook=Workbook.createWorkbook(os);   
 
           WritableSheet sheet = workbook.createSheet(sheetName, 0); //添加第一个工作表  
           initialSheetSetting(sheet);  
             
           Label label;  
           for (int i=0; i<dataTitles.length; i++){  
               //Label(列号,行号,内容,风格)  
               label = new Label(i, 0, dataTitles[i],getTitleCellFormat());   
               sheet.addCell(label);   
           }  
 
           //插入一行  
           insertRowData(sheet,1,new String[]{"200201001","张三","100","60","100","260"},getDataCellFormat(CellType.STRING_FORMULA));  
             
           //一个一个插入行  
           label = new Label(0, 2,"200201002",getDataCellFormat(CellType.STRING_FORMULA));   
           sheet.addCell(label);  
             
           label = new Label(1, 2,"李四",getDataCellFormat(CellType.STRING_FORMULA));   
           sheet.addCell(label);  
             
           insertOneCellData(sheet,2,2,70.5,getDataCellFormat(CellType.NUMBER));  
           insertOneCellData(sheet,3,2,90.523,getDataCellFormat(CellType.NUMBER));  
           insertOneCellData(sheet,4,2,60.5,getDataCellFormat(CellType.NUMBER));  
 
           insertFormula(sheet,5,2,"C3+D3+E3",getDataCellFormat(CellType.NUMBER_FORMULA));  
             
           //插入日期  
           mergeCellsAndInsertData(sheet, 0, 3, 5, 3, new Date(), getDataCellFormat(CellType.DATE));  
             
           workbook.write();   
           workbook.close();  
       }catch(Exception e){  
           e.printStackTrace();  
       }  
   }  
     
   /** 
    * 初始化表格属性 
    * @param sheet 
    */ 
   public void initialSheetSetting(WritableSheet sheet){  
      try{  
           //sheet.getSettings().setProtected(true); //设置xls的保护,单元格为只读的  
           sheet.getSettings().setDefaultColumnWidth(10); //设置列的默认宽度  
           //sheet.setRowView(2,false);//行高自动扩展   
           //setRowView(int row, int height);--行高   
           //setColumnView(int  col,int width); --列宽  
           sheet.setColumnView(0,20);//设置第一列宽度  
      }catch(Exception e){  
          e.printStackTrace();  
      }  
   }  
     
   /** 
    * 插入公式 
    * @param sheet 
    * @param col 
    * @param row 
    * @param formula 
    * @param format 
    */ 
   public void insertFormula(WritableSheet sheet,Integer col,Integer row,String formula,WritableCellFormat format){  
       try{  
           Formula f = new Formula(col, row, formula, format);  
           sheet.addCell(f);  
       }catch(Exception e){  
           e.printStackTrace();  
       }  
   }  
     
   /** 
    * 插入一行数据 
    * @param sheet       工作表 
    * @param row         行号 
    * @param content     内容 
    * @param format      风格 
    */ 
   public void insertRowData(WritableSheet sheet,Integer row,String[] dataArr,WritableCellFormat format){  
       try{  
           Label label;  
           for(int i=0;i<dataArr.length;i++){  
               label = new Label(i,row,dataArr[i],format);  
               sheet.addCell(label);  
           }  
       }catch(Exception e){  
           e.printStackTrace();  
       }  
   }  
     
   /** 
    * 插入单元格数据 
    * @param sheet 
    * @param col 
    * @param row 
    * @param data 
    */ 
   public void insertOneCellData(WritableSheet sheet,Integer col,Integer row,Object data,WritableCellFormat format){  
       try{  
           if(data instanceof Double){  
               jxl.write.Number  labelNF = new jxl.write.Number(col,row,(Double)data,format);   
               sheet.addCell(labelNF);   
           }else if(data instanceof Boolean){  
               jxl.write.Boolean labelB = new jxl.write.Boolean(col,row,(Boolean)data,format);   
               sheet.addCell(labelB);   
           }else if(data instanceof Date){  
               jxl.write.DateTime labelDT = new jxl.write.DateTime(col,row,(Date)data,format);   
               sheet.addCell(labelDT);   
               setCellComments(labelDT, "这是个创建表的日期说明!");  
           }else{  
               Label label = new Label(col,row,data.toString(),format);  
               sheet.addCell(label);                 
           }  
       }catch(Exception e){  
           e.printStackTrace();  
       }  
 
  }  
     
   /** 
    * 合并单元格,并插入数据 
    * @param sheet 
    * @param col_start 
    * @param row_start 
    * @param col_end 
    * @param row_end 
    * @param data 
    * @param format 
    */ 
   public void mergeCellsAndInsertData(WritableSheet sheet,Integer col_start,Integer row_start,Integer col_end,Integer row_end,Object data, WritableCellFormat format){  
      try{  
          sheet.mergeCells(col_start,row_start,col_end,row_end);//左上角到右下角  
          insertOneCellData(sheet, col_start, row_start, data, format);  
      }catch(Exception e){  
          e.printStackTrace();  
      }  
 
   }  
     
   /** 
    * 给单元格加注释 
    * @param label 
    * @param comments 
    */ 
   public void setCellComments(Object label,String comments){  
       WritableCellFeatures cellFeatures = new WritableCellFeatures();  
       cellFeatures.setComment(comments);  
       if(label instanceof jxl.write.Number){  
           jxl.write.Number num = (jxl.write.Number)label;  
           num.setCellFeatures(cellFeatures);  
       }else if(label instanceof jxl.write.Boolean){  
           jxl.write.Boolean bool = (jxl.write.Boolean)label;  
           bool.setCellFeatures(cellFeatures);  
       }else if(label instanceof jxl.write.DateTime){  
           jxl.write.DateTime dt = (jxl.write.DateTime)label;  
           dt.setCellFeatures(cellFeatures);  
       }else{  
           Label _label = (Label)label;  
           _label.setCellFeatures(cellFeatures);  
       }  
   }  
     
   /** 
   * 读取excel 
   * @param inputFile 
   * @param inputFileSheetIndex 
   * @throws Exception 
   */ 
   public ArrayList<String> readDataFromExcel(File inputFile, int inputFileSheetIndex){  
      ArrayList<String> list = new ArrayList<String>();  
      Workbook book = null;  
      Cell cell = null;  
      WorkbookSettings setting = new WorkbookSettings();   
      java.util.Locale locale = new java.util.Locale("zh","CN");   
      setting.setLocale(locale);  
      setting.setEncoding("ISO-8859-1");  
      try{  
          book = Workbook.getWorkbook(inputFile, setting);  
      }catch(Exception e){  
          e.printStackTrace();    
      }  
 
      Sheet sheet = book.getSheet(inputFileSheetIndex);  
      for (int rowIndex = 0; rowIndex < sheet.getRows(); rowIndex++) {//行  
       for (int colIndex = 0; colIndex < sheet.getColumns(); colIndex++) {//列  
           cell = sheet.getCell(colIndex, rowIndex);  
           //System.out.println(cell.getContents());  
           list.add(cell.getContents());  
       }  
      }  
      book.close();  
 
      return list;  
   }  
 
   /** 
    * 得到数据表头格式 
    * @return 
    */ 
   public WritableCellFormat getTitleCellFormat(){  
       WritableCellFormat wcf = null;  
       try {  
           //字体样式  
           WritableFont wf = new WritableFont(WritableFont.TIMES,12, WritableFont.NO_BOLD,false);//最后一个为是否italic  
           wf.setColour(Colour.RED);  
           wcf = new WritableCellFormat(wf);  
           //对齐方式  
           wcf.setAlignment(Alignment.CENTRE);  
           wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  
           //边框  
           wcf.setBorder(Border.ALL,BorderLineStyle.THIN);  
             
           //背景色  
           wcf.setBackground(Colour.GREY_25_PERCENT);  
       } catch (WriteException e) {  
        e.printStackTrace();  
       }  
       return wcf;  
   }  
     
   /** 
    * 得到数据格式 
    * @return 
    */ 
   public WritableCellFormat getDataCellFormat(CellType type){  
       WritableCellFormat wcf = null;  
       try {  
           //字体样式  
           if(type == CellType.NUMBER || type == CellType.NUMBER_FORMULA){//数字  
              NumberFormat nf = new NumberFormat("#.00");  
              wcf = new WritableCellFormat(nf);   
           }else if(type == CellType.DATE || type == CellType.DATE_FORMULA){//日期  
               jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd hh:mm:ss");   
               wcf = new jxl.write.WritableCellFormat(df);   
           }else{  
               WritableFont wf = new WritableFont(WritableFont.TIMES,10, WritableFont.NO_BOLD,false);//最后一个为是否italic  
               wcf = new WritableCellFormat(wf);  
           }  
           //对齐方式  
           wcf.setAlignment(Alignment.CENTRE);  
           wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  
           //边框  
           wcf.setBorder(Border.LEFT,BorderLineStyle.THIN);  
           wcf.setBorder(Border.BOTTOM,BorderLineStyle.THIN);  
           wcf.setBorder(Border.RIGHT,BorderLineStyle.THIN);  
           //背景色  
           wcf.setBackground(Colour.WHITE);  
             
           wcf.setWrap(true);//自动换行  
             
       } catch (WriteException e) {  
        e.printStackTrace();  
       }  
       return wcf;  
   }  
     
   /** 
    * 打开文件看看 
    * @param exePath 
    * @param filePath 
    */ 
   public void openExcel(String exePath,String filePath){  
       Runtime r=Runtime.getRuntime();   
       String cmd[]={exePath,filePath};   
       try{   
           r.exec(cmd);   
       }catch(Exception e){  
           e.printStackTrace();  
       }  
   }  
     
   public static void main(String[] args){  
       String[] titles = {"学号","姓名","语文","数学","英语","总分"};   
       JExcelUtils jxl = new JExcelUtils();  
       String filePath = "E:/test.xls";  
       jxl.createExcelFile(filePath,"成绩单",titles);  
       jxl.readDataFromExcel(new File(filePath),0);  
       jxl.openExcel("C:/Program Files/Microsoft Office/OFFICE11/EXCEL.EXE",filePath);  
   }  
} 

posted on 2009-05-18 22:15 风 阅读(1053) 评论(0)  编辑  收藏 所属分类: 收藏

新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • LoginAny 使用笔记
  • 资源管理器搜索包含文字的文件找不到的解决方法
  • xp下删除windows7,无法删除windows7文件夹,无法删除windows7文件,双系统卸载,取得文件权限
  • 利用windows系统ftp命令编写的BAT文件上传[转]
  • jxl操作excel例子(转)
  • I18N-国际化消息和日志
  • 使用 AppFuse 的七个理由
  • Office (2007) Open XML 文件格式简介
  • [转]Reporting Services与相关产品的比较
  • 自定义file类型input框样式的方法
 
 
Powered by:
BlogJava
Copyright © 风