poi jxl 生成EXCEL 报表

下面介绍POI 和JXL 生成报表的2种方式。

1.jxl 生成报表

Java代码 
  1. package excel;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.OutputStream;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. import jxl.Workbook;  
  9. import jxl.format.Alignment;  
  10. import jxl.format.Border;  
  11. import jxl.format.BorderLineStyle;  
  12. import jxl.format.CellFormat;  
  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. /***********************************************************************    
  19.  *    
  20.  *   jxlCreate.java      
  21.  *   @copyright       Copyright:   2009-2012      
  22.  *   @creator         周辉<br/>    
  23.  *   @create-time   Mar 9, 2010   1:35:19 PM    
  24.  *   @revision         $Id:     *    
  25.  ***********************************************************************/  
  26. public class jxlCreate {  
  27.   
  28.     /** 
  29.      * @param args 
  30.      */  
  31.     public static void main(String[] args) {  
  32.          // 准备设置excel工作表的标题     
  33.         String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};     
  34.         try {     
  35.             // 获得开始时间     
  36.             long start = System.currentTimeMillis();     
  37.             // 输出的excel的路径     
  38.             String filePath = "c://test.xls";     
  39.             // 创建Excel工作薄     
  40.             WritableWorkbook wwb;     
  41.             // 新建立一个jxl文件,即在C盘下生成test.xls     
  42.             OutputStream os = new FileOutputStream(filePath);     
  43.             wwb=Workbook.createWorkbook(os);      
  44.             // 添加第一个工作表并设置第一个Sheet的名字     
  45.             WritableSheet sheet = wwb.createSheet("产品清单"0);     
  46.             Label label;     
  47.             for(int i=0;i<title.length;i++){     
  48.                 // Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是y     
  49.                 // 在Label对象的子对象中指明单元格的位置和内容     
  50.                 label = new Label(i,0,title[i]);     
  51.                 // 将定义好的单元格添加到工作表中     
  52.                 sheet.addCell(label);     
  53.             }     
  54.             // 下面是填充数据     
  55.             /*    
  56.              * 保存数字到单元格,需要使用jxl.write.Number   
  57.              * 必须使用其完整路径,否则会出现错误   
  58.              * */    
  59.             // 填充产品编号     
  60.             jxl.write.Number number = new jxl.write.Number(0,1,20071001);     
  61.             sheet.addCell(number);     
  62.             // 填充产品名称     
  63.             label = new Label(1,1,"金鸽瓜子");     
  64.             sheet.addCell(label);     
  65.             /*   
  66.              * 定义对于显示金额的公共格式   
  67.              * jxl会自动实现四舍五入   
  68.              * 例如 2.456会被格式化为2.46,2.454会被格式化为2.45   
  69.              * */    
  70.             jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");     
  71.             jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(nf);     
  72.             // 填充产品价格     
  73.             jxl.write.Number nb = new jxl.write.Number(2,1,2.45,wcf);     
  74.             sheet.addCell(nb);     
  75.             // 填充产品数量     
  76.             jxl.write.Number numb = new jxl.write.Number(3,1,200);     
  77.             sheet.addCell(numb);     
  78.             /*   
  79.              * 定义显示日期的公共格式   
  80.              * 如:yyyy-MM-dd hh:mm   
  81.              * */    
  82.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
  83.             String newdate = sdf.format(new Date());     
  84.             // 填充出产日期     
  85.             label = new Label(4,1,newdate);     
  86.             sheet.addCell(label);     
  87.             // 填充产地     
  88.             label = new Label(5,1,"陕西西安");     
  89.             sheet.addCell(label);     
  90.             /*   
  91.              * 显示布尔值   
  92.              * */    
  93.             jxl.write.Boolean bool = new jxl.write.Boolean(6,1,true);     
  94.             sheet.addCell(bool);     
  95.             /*   
  96.              * 合并单元格   
  97.              * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的   
  98.              * 表示将从第x+1列,y+1行到m+1列,n+1行合并   
  99.              *    
  100.              * */    
  101.             sheet.mergeCells(0,3,2,3);     
  102.             label = new Label(0,3,"合并了三个单元格");     
  103.             sheet.addCell(label);     
  104.             /*   
  105.              *    
  106.              * 定义公共字体格式   
  107.              * 通过获取一个字体的样式来作为模板   
  108.              * 首先通过web.getSheet(0)获得第一个sheet   
  109.              * 然后取得第一个sheet的第二列,第一行也就是"产品名称"的字体    
  110.              * */    
  111.             CellFormat cf = wwb.getSheet(0).getCell(10).getCellFormat();     
  112.             WritableCellFormat wc = new WritableCellFormat();     
  113.             // 设置居中     
  114.             wc.setAlignment(Alignment.CENTRE);     
  115.             // 设置边框线     
  116.             wc.setBorder(Border.ALL, BorderLineStyle.THIN);     
  117.             // 设置单元格的背景颜色     
  118.             wc.setBackground(jxl.format.Colour.RED);     
  119.             label = new Label(1,5,"字体",wc);     
  120.             sheet.addCell(label);     
  121.     
  122.             // 设置字体     
  123.             jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("隶书"),20);     
  124.             WritableCellFormat font = new WritableCellFormat(wfont);     
  125.             label = new Label(2,6,"隶书",font);     
  126.             sheet.addCell(label);     
  127.                  
  128.             // 写入数据     
  129.             wwb.write();     
  130.             // 关闭文件     
  131.             wwb.close();     
  132.             long end = System.currentTimeMillis();     
  133.             System.out.println("----完成该操作共用的时间是:"+(end-start)/1000);     
  134.         } catch (Exception e) {     
  135.             System.out.println("---出现异常---");     
  136.             e.printStackTrace();     
  137.         }     
  138.   
  139.     }  
  140.   

 

 

 

 

 

 

2.POI 生成

 

Java代码 
  1. package excel;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6.   
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  8. import org.apache.poi.ss.usermodel.Cell;  
  9. import org.apache.poi.ss.usermodel.CellStyle;  
  10. import org.apache.poi.ss.usermodel.DataFormat;  
  11. import org.apache.poi.ss.usermodel.Row;  
  12. import org.apache.poi.ss.usermodel.Sheet;  
  13. import org.apache.poi.ss.usermodel.Workbook;  
  14. import org.apache.poi.ss.util.CellRangeAddress;  
  15.   
  16.   
  17. /***********************************************************************    
  18.  *    
  19.  *   poiCreate.java      
  20.  *   @copyright       Copyright:   2009-2012      
  21.  *   @creator         周辉<br/>    
  22.  *   @create-time   Mar 9, 2010   2:27:52 PM    
  23.  *   @revision         $Id:     *    
  24.  ***********************************************************************/  
  25. public class poiCreate {  
  26.   
  27.     /** 
  28.      * @param args 
  29.      */  
  30.     public static void main(String[] args) throws Exception {  
  31.         //创建一个EXCEL  
  32.         Workbook wb = new HSSFWorkbook();  
  33.         DataFormat format = wb.createDataFormat();  
  34.         CellStyle style;  
  35.         //创建一个SHEET  
  36.         Sheet sheet1 = wb.createSheet("产品清单");  
  37.         String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};  
  38.         int i=0;  
  39.         //创建一行  
  40.         Row row = sheet1.createRow((short)0);  
  41.         //填充标题  
  42.         for (String  s:title){  
  43.             Cell cell = row.createCell(i);  
  44.             cell.setCellValue(s);  
  45.             i++;  
  46.         }  
  47.         Row row1 = sheet1.createRow((short)1);  
  48.         //下面是填充数据  
  49.         row1.createCell(0).setCellValue(20071001);  
  50.         row1.createCell(1).setCellValue("金鸽瓜子");  
  51.         //创建一个单元格子  
  52.         Cell cell2=row1.createCell(2);  
  53.         // 填充产品价格  
  54.         cell2.setCellValue(2.45);  
  55.         style = wb.createCellStyle();  
  56.         style.setDataFormat(format.getFormat("#.##"));  
  57.         //设定样式  
  58.         cell2.setCellStyle(style);  
  59.         // 填充产品数量  
  60.         row1.createCell(3).setCellValue(200);  
  61.         /*   
  62.          * 定义显示日期的公共格式   
  63.          * 如:yyyy-MM-dd hh:mm   
  64.          * */  
  65.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
  66.         String newdate = sdf.format(new Date());   
  67.         // 填充出产日期     
  68.         row1.createCell(4).setCellValue(newdate);  
  69.         row1.createCell(5).setCellValue("陕西西安");  
  70.         /*   
  71.          * 显示布尔值   
  72.          * */   
  73.         row1.createCell(6).setCellValue(true);  
  74.         /*   
  75.          * 合并单元格   
  76.          * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的   
  77.          * 表示将first row, last row,first column,last column 
  78.          *    
  79.          * */    
  80.         Row row2 = sheet1.createRow((short2);  
  81.          Cell cell3 = row2.createCell((short0);  
  82.          cell3.setCellValue("合并了三个单元格");  
  83.         sheet1.addMergedRegion(new CellRangeAddress(2,2,0,2));  
  84.           
  85.         FileOutputStream fileOut = new FileOutputStream("d://test.xls");  
  86.         wb.write(fileOut);  
  87.         fileOut.close();  
  88.   
  89.   
  90.     }  
  91.   
  92. }  

   上面代码2中方式生成 2张报表,涉及到基本生成报表中的几种单元格类型。

   POI 用的JAR poi-3.6-20091214.jar   jxl 用到的jar  jxl-2.6.jar

   2 种方式都相对比较好用,个人推荐使用POI (apache的项目)

     相关参考资料可以去官方网站查看

    http://poi.apache.org/spreadsheet/quick-guide.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值