效果圖:
代碼如下:
- import java.io.File;
- import java.io.IOException;
- import jxl.format.Alignment;
- import jxl.format.Border;
- import jxl.format.BorderLineStyle;
- import jxl.format.Colour;
- import jxl.format.UnderlineStyle;
- import jxl.write.Label;
- import jxl.write.NumberFormats;
- import jxl.write.WritableCellFormat;
- import jxl.write.WritableFont;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- import jxl.write.WriteException;
- import jxl.write.biff.RowsExceededException;
- public class JxlTable {
- private final static JxlTable jxlTable = new JxlTable();
- public static JxlTable getInstance() {
- return jxlTable;
- }
- public JxlTable(){}
- /**
- * 根據輸入的內容創建一個表格
- * 要求:
- * 表頭表格線爲粗線,表體表格線爲細線;
- * 表頭背景色爲黃色且表頭字體加粗居中顯示,表體爲無色;
- * 表頭以及表體內容可以按照一定的格式輸入;
- *
- * 保留一個sheet且sheet的背景爲無網格線;
- *
- * @return 創建成功:true;創建失敗:false;
- */
- public boolean createTable(String header,String[] body,String filePath){
- boolean createFlag = true;
- WritableWorkbook book;
- try {
- //根據路徑生成excel文件
- book = Workbook.createWorkbook(new File(filePath));
- //創建一個sheet名爲"表格"
- WritableSheet sheet = book.createSheet("表格", 0);
- //設置NO列寬度
- sheet.setColumnView(1, 5);
- //去掉整個sheet中的網格線
- sheet.getSettings().setShowGridLines(false);
- Label tempLabel = null;
- //表頭輸出
- String[] headerArr = header.split(",");
- int headerLen = headerArr.length;
- //循環寫入表頭內容
- for(int i=0; i < headerLen; i ++){
- tempLabel = new Label(1+i,1,headerArr[i],getHeaderCellStyle());
- sheet.addCell(tempLabel);
- }
- //表體輸出
- int bodyLen = body.length;
- //循環寫入表體內容
- for(int j=0; j < bodyLen; j ++){
- String[] bodyTempArr = body[j].split(",");
- for(int k=0; k < bodyTempArr.length; k ++){
- WritableCellFormat tempCellFormat = null;
- /*
- * 表體內容的對齊設置
- * 這裏將序號NO以及年齡居中對齊,姓名以及性別默認對齊方式
- */
- tempCellFormat = getBodyCellStyle();
- if(tempCellFormat != null){
- if(k == 0 || k == (bodyTempArr.length -1)){
- tempCellFormat.setAlignment(Alignment.CENTRE);
- }
- }
- tempLabel = new Label(1+k,2+j,bodyTempArr[k],tempCellFormat);
- sheet.addCell(tempLabel);
- }
- }
- book.write();
- book.close();
- } catch (IOException e) {
- createFlag = false;
- System.out.println("EXCEL創建失敗!");
- e.printStackTrace();
- }catch (RowsExceededException e) {
- createFlag = false;
- System.out.println("EXCEL單元設置創建失敗!");
- e.printStackTrace();
- } catch (WriteException e) {
- createFlag = false;
- System.out.println("EXCEL寫入失敗!");
- e.printStackTrace();
- }
- return createFlag;
- }
- /**
- * 表頭單元格樣式的設定
- */
- public WritableCellFormat getHeaderCellStyle(){
- /*
- * WritableFont.createFont("宋體"):設置字體爲宋體
- * 10:設置字體大小
- * WritableFont.BOLD:設置字體加粗(BOLD:加粗 NO_BOLD:不加粗)
- * false:設置非斜體
- * UnderlineStyle.NO_UNDERLINE:沒有下劃線
- */
- WritableFont font = new WritableFont(WritableFont.createFont("宋體"),
- 10,
- WritableFont.BOLD,
- false,
- UnderlineStyle.NO_UNDERLINE);
- WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT);
- try {
- //添加字體設置
- headerFormat.setFont(font);
- //設置單元格背景色:表頭爲黃色
- headerFormat.setBackground(Colour.YELLOW);
- //設置表頭表格邊框樣式
- //整個表格線爲粗線、黑色
- headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.BLACK);
- //表頭內容水平居中顯示
- headerFormat.setAlignment(Alignment.CENTRE);
- } catch (WriteException e) {
- System.out.println("表頭單元格樣式設置失敗!");
- }
- return headerFormat;
- }
- /**
- * 表頭單元格樣式的設定
- */
- public WritableCellFormat getBodyCellStyle(){
- /*
- * WritableFont.createFont("宋體"):設置字體爲宋體
- * 10:設置字體大小
- * WritableFont.NO_BOLD:設置字體非加粗(BOLD:加粗 NO_BOLD:不加粗)
- * false:設置非斜體
- * UnderlineStyle.NO_UNDERLINE:沒有下劃線
- */
- WritableFont font = new WritableFont(WritableFont.createFont("宋體"),
- 10,
- WritableFont.NO_BOLD,
- false,
- UnderlineStyle.NO_UNDERLINE);
- WritableCellFormat bodyFormat = new WritableCellFormat(font);
- try {
- //設置單元格背景色:表體爲白色
- bodyFormat.setBackground(Colour.WHITE);
- //設置表頭表格邊框樣式
- //整個表格線爲細線、黑色
- bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
- } catch (WriteException e) {
- System.out.println("表體單元格樣式設置失敗!");
- }
- return bodyFormat;
- }
- public static void main(String[] args) {
- String header = "NO,姓名,性別,年齡";
- String[] body = new String[4];
- body[0] = "1,歐陽鋒,男,68";
- body[1] = "2,黃藥師,男,67";
- body[2] = "3,洪七公,男,70";
- body[3] = "4,郭靖,男,32";
- String filePath = "e:/test.xls";
- JxlTable testJxl = JxlTable.getInstance();
- boolean flag = testJxl.createTable(header, body, filePath);
- if(flag){
- System.out.println("表格創建成功!!");
- }
- }
- }
原文:http://blog.csdn.net/mcpang/article/details/6834517