JEXCEL的使用

 
  1. package excel.jxl;   
  2.   
  3. import java.io.FileOutputStream;   
  4. import java.io.OutputStream;   
  5. import java.util.Date;   
  6.   
  7. import jxl.Workbook;   
  8. import jxl.format.Alignment;   
  9. import jxl.format.Border;   
  10. import jxl.format.BorderLineStyle;   
  11. import jxl.format.Colour;   
  12. import jxl.format.VerticalAlignment;   
  13. import jxl.write.Label;   
  14. import jxl.write.NumberFormat;   
  15. import jxl.write.WritableCellFormat;   
  16. import jxl.write.WritableFont;   
  17. import jxl.write.WritableSheet;   
  18. import jxl.write.WritableWorkbook;   
  19. import jxl.write.WriteException;   
  20.   
  21. public class JExcelUtils {   
  22.   
  23.     /**  
  24.      * 生成Excel文件  
  25.      * @param path         文件路径  
  26.      * @param sheetName    工作表名称  
  27.      * @param dataTitles   数据标题  
  28.      */  
  29.    public void createExcelFile(String path,String sheetName,String[] dataTitles){   
  30.        WritableWorkbook workbook;   
  31.        try{   
  32.            OutputStream os=new FileOutputStream(path);    
  33.            workbook=Workbook.createWorkbook(os);    
  34.   
  35.            WritableSheet sheet = workbook.createSheet(sheetName, 0); //添加第一个工作表   
  36.   
  37.            //setRowView(int row, int height);--行高    
  38.            //sheet.setRowView(0,100);   
  39.            //setColumnView(int  col,int width); --列宽   
  40.            sheet.setColumnView(0,20);   
  41.               
  42.            Label label;   
  43.            for (int i=0; i<dataTitles.length; i++){   
  44.                //Label(列号,行号,内容,风格)   
  45.                label = new Label(i, 0, dataTitles[i],getTitleCellFormat());    
  46.                sheet.addCell(label);    
  47.            }   
  48.   
  49.            //插入一行   
  50.            insertRowData(sheet,1,new String[]{"200201001","张三","100","60","100","260"},getDataCellFormat(String.class));   
  51.               
  52.            //一个一个插入行   
  53.            label = new Label(02,"200201002",getDataCellFormat(String.class));    
  54.            sheet.addCell(label);   
  55.               
  56.            label = new Label(12,"李四",getDataCellFormat(String.class));    
  57.            sheet.addCell(label);   
  58.               
  59.            insertOneCellData(sheet,2,2,70.5,getDataCellFormat(Double.class));   
  60.            insertOneCellData(sheet,3,2,90.5,getDataCellFormat(Double.class));   
  61.            insertOneCellData(sheet,4,2,60.5,getDataCellFormat(Double.class));   
  62.            insertOneCellData(sheet,5,2,221.5,getDataCellFormat(Double.class));   
  63.               
  64.            //插入日期   
  65.            mergeCellsAndInsertData(sheet, 0353new Date(), getDataCellFormat(Date.class));   
  66.               
  67.            workbook.write();    
  68.            workbook.close();   
  69.        }catch(Exception e){   
  70.            e.printStackTrace();   
  71.        }   
  72.    }   
  73.       
  74.    /**  
  75.     * 插入一行数据  
  76.     * @param sheet       工作表  
  77.     * @param row         行号  
  78.     * @param content     内容  
  79.     * @param format      风格  
  80.     */  
  81.    public void insertRowData(WritableSheet sheet,Integer row,String[] dataArr,WritableCellFormat format){   
  82.        try{   
  83.            Label label;   
  84.            for(int i=0;i<dataArr.length;i++){   
  85.                label = new Label(i,row,dataArr[i],format);   
  86.                sheet.addCell(label);   
  87.            }   
  88.        }catch(Exception e){   
  89.            e.printStackTrace();   
  90.        }   
  91.    }   
  92.       
  93.    /**  
  94.     * 插入单元格数据  
  95.     * @param sheet  
  96.     * @param col  
  97.     * @param row  
  98.     * @param data  
  99.     */  
  100.    public void insertOneCellData(WritableSheet sheet,Integer col,Integer row,Object data,WritableCellFormat format){   
  101.        try{   
  102.            if(data instanceof Double){   
  103.                jxl.write.Number  labelNF = new jxl.write.Number(col,row,(Double)data,format);    
  104.                sheet.addCell(labelNF);    
  105.            }else if(data instanceof Boolean){   
  106.                jxl.write.Boolean labelB = new jxl.write.Boolean(col,row,(Boolean)data,format);    
  107.                sheet.addCell(labelB);    
  108.            }else if(data instanceof Date){   
  109.                jxl.write.DateTime labelDT = new jxl.write.DateTime(col,row,(Date)data,format);    
  110.                sheet.addCell(labelDT);    
  111.            }else{   
  112.                Label label = new Label(col,row,data.toString(),format);   
  113.                sheet.addCell(label);                  
  114.            }   
  115.        }catch(Exception e){   
  116.            e.printStackTrace();   
  117.        }   
  118.   
  119.   }   
  120.       
  121.    /**  
  122.     * 合并单元格,并插入数据  
  123.     * @param sheet  
  124.     * @param col_start  
  125.     * @param row_start  
  126.     * @param col_end  
  127.     * @param row_end  
  128.     * @param data  
  129.     * @param format  
  130.     */  
  131.    public void mergeCellsAndInsertData(WritableSheet sheet,Integer col_start,Integer row_start,Integer col_end,Integer row_end,Object data, WritableCellFormat format){   
  132.       try{   
  133.           sheet.mergeCells(col_start,row_start,col_end,row_end);//左上角到右下角   
  134.           insertOneCellData(sheet, col_start, row_start, data, format);   
  135.       }catch(Exception e){   
  136.           e.printStackTrace();   
  137.       }   
  138.   
  139.    }   
  140.    /**  
  141.     * 得到数据表头格式  
  142.     * @return  
  143.     */  
  144.    public WritableCellFormat getTitleCellFormat(){   
  145.        WritableCellFormat wcf = null;   
  146.        try {   
  147.            //字体样式   
  148.            WritableFont wf = new WritableFont(WritableFont.TIMES,12, WritableFont.NO_BOLD,false);//最后一个为是否italic   
  149.            wf.setColour(Colour.RED);   
  150.            wcf = new WritableCellFormat(wf);   
  151.            //对齐方式   
  152.            wcf.setAlignment(Alignment.CENTRE);   
  153.            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);   
  154.            //边框   
  155.            wcf.setBorder(Border.ALL,BorderLineStyle.THIN);   
  156.               
  157.            //背景色   
  158.            wcf.setBackground(Colour.GREY_25_PERCENT);   
  159.        } catch (WriteException e) {   
  160.         e.printStackTrace();   
  161.        }   
  162.        return wcf;   
  163.    }   
  164.       
  165.    /**  
  166.     * 得到数据格式  
  167.     * @return  
  168.     */  
  169.    public WritableCellFormat getDataCellFormat(Class clazz){   
  170.        WritableCellFormat wcf = null;   
  171.        try {   
  172.            //字体样式   
  173.            if(clazz.getName().endsWith("Double")){//数字   
  174.               NumberFormat nf = new NumberFormat("#.#");   
  175.               wcf = new WritableCellFormat(nf);    
  176.            }else if(clazz.getName().endsWith("Date")){//日期   
  177.                jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd hh:mm:ss");    
  178.                wcf = new jxl.write.WritableCellFormat(df);    
  179.            }else{   
  180.                WritableFont wf = new WritableFont(WritableFont.TIMES,10, WritableFont.NO_BOLD,false);//最后一个为是否italic   
  181.                wcf = new WritableCellFormat(wf);   
  182.            }   
  183.            //对齐方式   
  184.            wcf.setAlignment(Alignment.CENTRE);   
  185.            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);   
  186.            //边框   
  187.            wcf.setBorder(Border.LEFT,BorderLineStyle.THIN);   
  188.            wcf.setBorder(Border.BOTTOM,BorderLineStyle.THIN);   
  189.            wcf.setBorder(Border.RIGHT,BorderLineStyle.THIN);   
  190.            //背景色   
  191.            wcf.setBackground(Colour.WHITE);   
  192.               
  193.            wcf.setWrap(true);//自动换行   
  194.               
  195.        } catch (WriteException e) {   
  196.         e.printStackTrace();   
  197.        }   
  198.        return wcf;   
  199.    }   
  200.       
  201.    /**  
  202.     * 打开文件看看  
  203.     * @param exePath  
  204.     * @param filePath  
  205.     */  
  206.    public void openExcel(String exePath,String filePath){   
  207.        Runtime r=Runtime.getRuntime();    
  208.        String cmd[]={exePath,filePath};    
  209.        try{    
  210.            r.exec(cmd);    
  211.        }catch(Exception e){   
  212.            e.printStackTrace();   
  213.        }   
  214.    }   
  215.       
  216.    public static void main(String[] args){   
  217.        String[] titles = {"学号","姓名","语文","数学","英语","总分"};    
  218.        JExcelUtils jxl = new JExcelUtils();   
  219.        String filePath = "E:/test.xls";   
  220.        jxl.createExcelFile(filePath,"成绩单",titles);   
  221.        jxl.openExcel("C:/Program Files/Microsoft Office/OFFICE11/EXCEL.EXE",filePath);   
  222.    }   
  223. }  
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值