使用JXL创建Excel

  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. }  

你可能感兴趣的:(java,Excel,JXL)