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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章