Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104174
  • 博文数量: 24
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-13 14:10
个人简介

文章分类

全部博文(24)

文章存档

2011年(1)

2010年(7)

2009年(14)

2008年(2)

我的朋友

分类:

2010-02-02 15:39:44

项目中做统计的时候有时需要生成统计execl报表,java生成execl可以通过jxl包来实现。很简单的说明一下其使用
    
Java代码 复制代码
  1. package com.gdapp.oa.app;   
  2.   
  3. import java.io.ByteArrayInputStream;   
  4. import java.io.ByteArrayOutputStream;   
  5. import java.io.File;   
  6. import java.io.FileInputStream;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.InputStream;   
  10. import java.io.OutputStream;   
  11. import java.text.SimpleDateFormat;   
  12. import java.util.Date;   
  13. import java.util.List;   
  14.   
  15. import jxl.Cell;   
  16. import jxl.Sheet;   
  17. import jxl.Workbook;   
  18. import jxl.format.Alignment;   
  19. import jxl.format.Border;   
  20. import jxl.format.BorderLineStyle;   
  21. import jxl.format.CellFormat;   
  22. import jxl.format.VerticalAlignment;   
  23. import jxl.write.Label;   
  24. import jxl.write.WritableCellFormat;   
  25. import jxl.write.WritableFont;   
  26. import jxl.write.WritableSheet;   
  27. import jxl.write.WritableWorkbook;   
  28. import jxl.write.WriteException;   
  29.   
  30. public class ExcelAPP   
  31. {   
  32.     private static void createTitle(WritableSheet wsheet) throws Exception   
  33.     {   
  34.         wsheet.addCell(new Label(02"信件号"));   
  35.         wsheet.addCell(new Label(12"信件类型"));   
  36.         wsheet.addCell(new Label(22"信件标题"));   
  37.         wsheet.addCell(new Label(32"信件内容"));   
  38.         wsheet.addCell(new Label(42"发信人"));   
  39.         wsheet.addCell(new Label(52"邮政编码"));   
  40.         wsheet.addCell(new Label(62"发信人地址"));   
  41.         wsheet.addCell(new Label(72"电话"));   
  42.         wsheet.addCell(new Label(82"发信人信箱"));   
  43.         wsheet.addCell(new Label(92"信件状态"));   
  44.         wsheet.addCell(new Label(102"转发/办理部门"));   
  45.         wsheet.addCell(new Label(112"来信时间"));   
  46.         wsheet.addCell(new Label(122"分办时间"));   
  47.         wsheet.addCell(new Label(132"办回时间"));   
  48.     }   
  49.   
  50.     private static String toUtf8String(String s)   
  51.     {   
  52.         StringBuffer sb = new StringBuffer();   
  53.         for (int i = 0; i < s.length(); i++)   
  54.         {   
  55.             char c = s.charAt(i);   
  56.             if (c >= 0 && c <= 255)   
  57.             {   
  58.                 sb.append(c);   
  59.             } else  
  60.             {   
  61.                 byte[] b;   
  62.                 try  
  63.                 {   
  64.                     b = Character.toString(c).getBytes("utf-8");   
  65.                 } catch (Exception ex)   
  66.                 {   
  67.                     System.out.println(ex);   
  68.                     b = new byte[0];   
  69.                 }   
  70.                 for (int j = 0; j < b.length; j++)   
  71.                 {   
  72.                     int k = b[j];   
  73.                     if (k < 0)   
  74.                         k += 256;   
  75.                     sb.append("%" + Integer.toHexString(k).toUpperCase());   
  76.                 }   
  77.             }   
  78.         }   
  79.         return sb.toString();   
  80.     }   
  81.   
  82.     //直接在本地生成文件   
  83.     static void writeXls()   
  84.     {   
  85.   
  86.         System.out.println("writing exls.");   
  87.         WritableWorkbook wwb = null;   
  88.         WritableSheet sheet = null;   
  89.         try  
  90.         {   
  91.             String xlsName = "c:\\excel2.xls";   
  92.             File afile = new File(xlsName);   
  93.             if (!afile.exists())   
  94.             {   
  95.                 afile.createNewFile();   
  96.             }   
  97.             wwb = Workbook.createWorkbook(afile);   
  98.             sheet = wwb.createSheet("bicashy的测试"0); //创建标签页   
  99.             WritableFont wf = new WritableFont(WritableFont.TIMES, 10,   
  100.                     WritableFont.BOLD, false);   
  101. //          WritableFont wf = new WritableFont(WritableFont.ARIAL, 16,   
  102. //                  WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,   
  103. //                  Colour.BLACK);   
  104.             WritableCellFormat format = new WritableCellFormat(wf);  //添加格式化样式   
  105.             format.setAlignment(Alignment.CENTRE);                   //设置水平对齐方式   
  106.             format.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置垂直对齐方式   
  107.             //remember,the first parameter is column index.   
  108.             Label cell = new Label(00"row 1,column 1", format);//set the value of cell (0,0)   
  109.             sheet.addCell(cell);   
  110.             cell = new Label(10"row 1,column 2", format);   
  111.             sheet.addCell(cell);   
  112.             cell = new Label(01"row 2,column 1", format);   
  113.             sheet.addCell(cell);   
  114.             createTitle(sheet);   
  115.             //将第四行合并前三列,行列默认都是从0开始的   
  116.             sheet.mergeCells(0,3,2,3);     
  117.             cell = new Label(0,3,"合并了三个单元格", format);    
  118.             sheet.addCell(cell);   
  119.             sheet.mergeCells(0,4,0,6);     
  120.             cell = new Label(0,4,"合并三列", format);    
  121.             sheet.addCell(cell);     
  122.             wwb.write();   
  123.             System.out.println("finished.");   
  124.   
  125.         } catch (WriteException e)   
  126.         {   
  127.             e.printStackTrace();   
  128.         } catch (Exception e)   
  129.         {   
  130.             e.printStackTrace();   
  131.         } finally  
  132.         {   
  133.             if (wwb != null)   
  134.             {   
  135.                 try  
  136.                 {   
  137.                     wwb.close();   
  138.                 } catch (Exception e1)   
  139.                 {   
  140.                     e1.printStackTrace();   
  141.                 }   
  142.             }   
  143.         }   
  144.     }   
  145.   
  146.     //直接在本地生成统计文件   
  147.     static void writeXlsTJ()   
  148.     {   
  149. //       准备设置excel工作表的标题   
  150.         String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};   
  151.         try {   
  152.             // 获得开始时间   
  153.             long start = System.currentTimeMillis();   
  154.             // 输出的excel的路径   
  155.             String filePath = "c:\\test.xls";   
  156.             // 创建Excel工作薄   
  157.             WritableWorkbook wwb;   
  158.             // 新建立一个jxl文件,即在C盘下生成test.xls   
  159.             OutputStream os = new FileOutputStream(filePath);   
  160.             wwb=Workbook.createWorkbook(os);    
  161.             // 添加第一个工作表并设置第一个Sheet的名字   
  162.             WritableSheet sheet = wwb.createSheet("产品清单"0);   
  163.             Label label;   
  164.             for(int i=0;i
  165.                 // Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是y   
  166.                 // 在Label对象的子对象中指明单元格的位置和内容   
  167.                 label = new Label(i,0,title[i]);   
  168.                 // 将定义好的单元格添加到工作表中   
  169.                 sheet.addCell(label);   
  170.             }   
  171.             // 下面是填充数据   
  172.             /*   
  173.              * 保存数字到单元格,需要使用jxl.write.Number  
  174.              * 必须使用其完整路径,否则会出现错误  
  175.              * */  
  176.             // 填充产品编号   
  177.             jxl.write.Number number = new jxl.write.Number(0,1,20071001);   
  178.             sheet.addCell(number);   
  179.             // 填充产品名称   
  180.             label = new Label(1,1,"盐水鸭");   
  181.             sheet.addCell(label);   
  182.             /*  
  183.              * 定义对于显示金额的公共格式  
  184.              * jxl会自动实现四舍五入  
  185.              * 例如 22.456会被格式化为22.46,22.454会被格式化为22.45  
  186.              * */  
  187.             jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");   
  188.             jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(nf);   
  189.             // 填充产品价格   
  190.             jxl.write.Number nb = new jxl.write.Number(2,1,22.45,wcf);   
  191.             sheet.addCell(nb);   
  192.             // 填充产品数量   
  193.             jxl.write.Number numb = new jxl.write.Number(3,1,200);   
  194.             sheet.addCell(numb);   
  195.             /*  
  196.              * 定义显示日期的公共格式  
  197.              * 如:yyyy-MM-dd hh:mm  
  198.              * */  
  199.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
  200.             String newdate = sdf.format(new Date());   
  201.             // 填充出产日期   
  202.             label = new Label(4,1,newdate);   
  203.             sheet.addCell(label);   
  204.             // 填充产地   
  205.             label = new Label(5,1,"南京");   
  206.             sheet.addCell(label);   
  207.             /*  
  208.              * 显示布尔值  
  209.              * */  
  210.             jxl.write.Boolean bool = new jxl.write.Boolean(6,1,true);   
  211.             sheet.addCell(bool);   
  212.             /*  
  213.              * 合并单元格  
  214.              * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的  
  215.              * 表示将从第x+1列,y+1行到m+1列,n+1行合并  
  216.              *   
  217.              * */  
  218.             sheet.mergeCells(0,3,2,3);   
  219.             label = new Label(0,3,"合并了三个单元格");   
  220.             sheet.addCell(label);   
  221.             /*  
  222.              *   
  223.              * 定义公共字体格式  
  224.              * 通过获取一个字体的样式来作为模板  
  225.              * 首先通过web.getSheet(0)获得第一个sheet  
  226.              * 然后取得第一个sheet的第二列,第一行也就是"产品名称"的字体   
  227.              * */  
  228.             CellFormat cf = wwb.getSheet(0).getCell(10).getCellFormat();   
  229.             WritableCellFormat wc = new WritableCellFormat();   
  230.             // 设置居中   
  231.             wc.setAlignment(Alignment.CENTRE);   
  232.             // 设置边框线   
  233.             wc.setBorder(Border.ALL, BorderLineStyle.THIN);   
  234.             // 设置单元格的背景颜色   
  235.             wc.setBackground(jxl.format.Colour.RED);   
  236.             label = new Label(1,5,"字体",wc);   
  237.             sheet.addCell(label);   
  238.   
  239.             // 设置字体   
  240.             jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("隶书"),20);   
  241.             WritableCellFormat font = new WritableCellFormat(wfont);   
  242.             label = new Label(2,6,"隶书",font);   
  243.             sheet.addCell(label);   
  244.                
  245.             // 写入数据   
  246.             wwb.write();   
  247.             // 关闭文件   
  248.             wwb.close();   
  249.             long end = System.currentTimeMillis();   
  250.             System.out.println("操作共用的时间:"+(end-start)+"毫秒");   
  251.         } catch (Exception e) {   
  252.             e.printStackTrace();   
  253.         }   
  254.     }   
  255.        
  256.     //生成流,以便进行下载(用时可将流返回)   
  257.     static void writeXlsStream()   
  258.     {   
  259.         try  
  260.         {   
  261.             ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  262.             WritableWorkbook wbook = Workbook.createWorkbook(baos);   
  263.             wbook.write();   
  264.             wbook.close();   
  265.             byte[] bs = baos.toByteArray();   
  266.             baos.close();   
  267.             //inputstream 可以用于返回   
  268.             InputStream inputstream = new ByteArrayInputStream(bs);   
  269.             String fileName = toUtf8String("数据统计.xls");   
  270.         } catch (WriteException e)   
  271.         {   
  272.             // TODO Auto-generated catch block   
  273.             e.printStackTrace();   
  274.         } catch (IOException e)   
  275.         {   
  276.             // TODO Auto-generated catch block   
  277.             e.printStackTrace();   
  278.         }   
  279.     }   
  280.        
  281.     //读本地文件   
  282.     static void readXls()   
  283.     {   
  284.         try  
  285.         {   
  286.             String filePath = "c:\\excel2.xls";   
  287.             InputStream is = new  FileInputStream(filePath);   
  288.             Workbook wb = Workbook.getWorkbook(is);   //得到工作薄     
  289.             Sheet sheet = wb.getSheet(0); //得到工作薄中的第一个工作表   
  290.             Cell cell = sheet.getCell(0,0);//得到工作表的第一个单元格,即A1   
  291.             String content=cell.getContents();//getContents()将Cell中的字符转为字符串   
  292.             System.out.println("A1内容=="+content);   
  293.                
  294.             System.out.println(sheet.getCell(01).getContents());   
  295.             System.out.println("总行数=="+sheet.getRows());   
  296.             System.out.println("总列数=="+sheet.getColumns());   
  297.             wb.close();//关闭工作薄   
  298.             is.close();//关闭输入流   
  299.                
  300.         } catch (Exception e)   
  301.         {   
  302.             // TODO Auto-generated catch block   
  303.             e.printStackTrace();   
  304.         }   
  305.     }   
  306.        
  307.     public static void main(String[] args)   
  308.     {   
  309.         //writeXls();   
  310.         readXls();   
  311.     }   
  312. }  
需要的jxl.ar
   
  • (669.4 KB)
  • 下载次数: 18
阅读(1072) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~