jxl 創建表格(包括去掉網格線、字體設置、單元格設置、對齊方式等設置)

效果圖:

 

代碼如下:

  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. import jxl.format.Alignment;  
  5. import jxl.format.Border;  
  6. import jxl.format.BorderLineStyle;  
  7. import jxl.format.Colour;  
  8. import jxl.format.UnderlineStyle;  
  9. import jxl.write.Label;  
  10. import jxl.write.NumberFormats;  
  11. import jxl.write.WritableCellFormat;  
  12. import jxl.write.WritableFont;  
  13. import jxl.write.WritableSheet;  
  14. import jxl.write.WritableWorkbook;  
  15. import jxl.write.WriteException;  
  16. import jxl.write.biff.RowsExceededException;  
  17.   
  18. public class JxlTable {  
  19.   
  20.     private final static JxlTable jxlTable = new JxlTable();  
  21.   
  22.     public static JxlTable getInstance() {  
  23.         return jxlTable;  
  24.     }  
  25.       
  26.     public JxlTable(){}  
  27.       
  28.     /** 
  29.      * 根據輸入的內容創建一個表格 
  30.      * 要求: 
  31.      * 表頭表格線爲粗線,表體表格線爲細線; 
  32.      * 表頭背景色爲黃色且表頭字體加粗居中顯示,表體爲無色; 
  33.      * 表頭以及表體內容可以按照一定的格式輸入; 
  34.      *  
  35.      * 保留一個sheet且sheet的背景爲無網格線; 
  36.      *  
  37.      * @return 創建成功:true;創建失敗:false; 
  38.      */  
  39.     public boolean createTable(String header,String[] body,String filePath){  
  40.         boolean createFlag = true;  
  41.           
  42.         WritableWorkbook book;  
  43.         try {  
  44.             //根據路徑生成excel文件  
  45.             book = Workbook.createWorkbook(new File(filePath));  
  46.             //創建一個sheet名爲"表格"  
  47.             WritableSheet sheet = book.createSheet("表格"0);  
  48.               
  49.             //設置NO列寬度  
  50.             sheet.setColumnView(15);  
  51.             //去掉整個sheet中的網格線  
  52.             sheet.getSettings().setShowGridLines(false);  
  53.               
  54.             Label tempLabel = null;  
  55.             //表頭輸出  
  56.             String[] headerArr = header.split(",");  
  57.             int headerLen = headerArr.length;  
  58.             //循環寫入表頭內容  
  59.             for(int i=0; i < headerLen; i ++){  
  60.                 tempLabel = new Label(1+i,1,headerArr[i],getHeaderCellStyle());  
  61.                 sheet.addCell(tempLabel);  
  62.             }  
  63.             //表體輸出  
  64.             int bodyLen = body.length;  
  65.             //循環寫入表體內容  
  66.             for(int j=0; j < bodyLen; j ++){  
  67.                 String[] bodyTempArr = body[j].split(",");  
  68.                 for(int k=0; k < bodyTempArr.length; k ++){  
  69.                     WritableCellFormat tempCellFormat = null;  
  70.                     /* 
  71.                      * 表體內容的對齊設置 
  72.                      * 這裏將序號NO以及年齡居中對齊,姓名以及性別默認對齊方式 
  73.                      */  
  74.                     tempCellFormat = getBodyCellStyle();  
  75.                     if(tempCellFormat != null){  
  76.                         if(k == 0 || k == (bodyTempArr.length -1)){  
  77.                             tempCellFormat.setAlignment(Alignment.CENTRE);  
  78.                         }  
  79.                     }  
  80.                     tempLabel = new Label(1+k,2+j,bodyTempArr[k],tempCellFormat);  
  81.                     sheet.addCell(tempLabel);  
  82.                 }  
  83.                   
  84.             }  
  85.             book.write();  
  86.             book.close();  
  87.         } catch (IOException e) {  
  88.             createFlag = false;  
  89.             System.out.println("EXCEL創建失敗!");  
  90.             e.printStackTrace();  
  91.               
  92.         }catch (RowsExceededException e) {  
  93.             createFlag = false;  
  94.             System.out.println("EXCEL單元設置創建失敗!");  
  95.             e.printStackTrace();  
  96.         } catch (WriteException e) {  
  97.             createFlag = false;  
  98.             System.out.println("EXCEL寫入失敗!");  
  99.             e.printStackTrace();  
  100.         }  
  101.   
  102.         return createFlag;  
  103.           
  104.     }  
  105.     /** 
  106.      * 表頭單元格樣式的設定 
  107.      */  
  108.     public WritableCellFormat getHeaderCellStyle(){  
  109.           
  110.         /* 
  111.          * WritableFont.createFont("宋體"):設置字體爲宋體 
  112.          * 10:設置字體大小 
  113.          * WritableFont.BOLD:設置字體加粗(BOLD:加粗     NO_BOLD:不加粗) 
  114.          * false:設置非斜體 
  115.          * UnderlineStyle.NO_UNDERLINE:沒有下劃線 
  116.          */  
  117.         WritableFont font = new WritableFont(WritableFont.createFont("宋體"),  
  118.                                              10,   
  119.                                              WritableFont.BOLD,   
  120.                                              false,  
  121.                                              UnderlineStyle.NO_UNDERLINE);  
  122.           
  123.         WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT);  
  124.         try {  
  125.             //添加字體設置  
  126.             headerFormat.setFont(font);  
  127.             //設置單元格背景色:表頭爲黃色  
  128.             headerFormat.setBackground(Colour.YELLOW);  
  129.             //設置表頭表格邊框樣式  
  130.             //整個表格線爲粗線、黑色  
  131.             headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.BLACK);  
  132.             //表頭內容水平居中顯示  
  133.             headerFormat.setAlignment(Alignment.CENTRE);      
  134.         } catch (WriteException e) {  
  135.             System.out.println("表頭單元格樣式設置失敗!");  
  136.         }  
  137.         return headerFormat;  
  138.     }  
  139.     /** 
  140.      * 表頭單元格樣式的設定 
  141.      */  
  142.     public WritableCellFormat getBodyCellStyle(){  
  143.           
  144.         /* 
  145.          * WritableFont.createFont("宋體"):設置字體爲宋體 
  146.          * 10:設置字體大小 
  147.          * WritableFont.NO_BOLD:設置字體非加粗(BOLD:加粗     NO_BOLD:不加粗) 
  148.          * false:設置非斜體 
  149.          * UnderlineStyle.NO_UNDERLINE:沒有下劃線 
  150.          */  
  151.         WritableFont font = new WritableFont(WritableFont.createFont("宋體"),  
  152.                                              10,   
  153.                                              WritableFont.NO_BOLD,   
  154.                                              false,  
  155.                                              UnderlineStyle.NO_UNDERLINE);  
  156.           
  157.         WritableCellFormat bodyFormat = new WritableCellFormat(font);  
  158.         try {  
  159.             //設置單元格背景色:表體爲白色  
  160.             bodyFormat.setBackground(Colour.WHITE);  
  161.             //設置表頭表格邊框樣式  
  162.             //整個表格線爲細線、黑色  
  163.             bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);  
  164.               
  165.         } catch (WriteException e) {  
  166.             System.out.println("表體單元格樣式設置失敗!");  
  167.         }  
  168.         return bodyFormat;  
  169.     }  
  170.       
  171.     public static void main(String[] args) {  
  172.         String header = "NO,姓名,性別,年齡";  
  173.         String[] body = new String[4];  
  174.         body[0] = "1,歐陽鋒,男,68";  
  175.         body[1] = "2,黃藥師,男,67";  
  176.         body[2] = "3,洪七公,男,70";  
  177.         body[3] = "4,郭靖,男,32";  
  178.         String filePath = "e:/test.xls";  
  179.           
  180.         JxlTable testJxl = JxlTable.getInstance();  
  181.           
  182.         boolean flag = testJxl.createTable(header, body, filePath);  
  183.         if(flag){  
  184.             System.out.println("表格創建成功!!");  
  185.         }  
  186.     }  
  187. }  


原文:http://blog.csdn.net/mcpang/article/details/6834517
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章