jxl使用簡介

最基本的生成excel文檔方法:

import java.io.File;
import jxl.Workbook;

import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelOper {

 public static void createExcel() {
  try {
   System.out.println("FHFGHGH");
   WritableWorkbook book = Workbook.createWorkbook(new File(
     "C://temp.xls"));
   WritableSheet sheet = book.createSheet("queryMessage", 0);

    WritableSheet sheet2 = book.createSheet("queryMessage2", 0);


   Label label1 = new Label(0, 0,"ASDFG");
   
   sheet.addCell(label1);
             book.write();
   book.close();

  } catch (Exception e) {
   System.out.println(e);
  }
 }

 public static void main(String args[]) {
  createExcel();
 }
}

一些格式設置:

private static WritableCellFormat wcf_maintitle = new WritableCellFormat(
font10B);
private static WritableCellFormat wcf_title = new WritableCellFormat(font10B);
private static WritableCellFormat wcf_center = new WritableCellFormat(font10);
private static WritableCellFormat wcf_left = new WritableCellFormat(font10);
private static WritableCellFormat wcf_left_B = new WritableCellFormat(font10);
private static WritableCellFormat wcf_left_A = new WritableCellFormat(font10);
private static WritableCellFormat wcf_left_C = new WritableCellFormat(font10);

static {
try {
//設置單元格字體
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD);

//設置幾種格式的單元格
//大標題
wcf_maintitle.setBorder(Border.NONE, BorderLineStyle.THIN); //邊框線條
wcf_maintitle.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直對齊
wcf_maintitle.setAlignment(Alignment.CENTRE); //水平對齊
wcf_maintitle.setWrap(false); //是否換行
wcf_maintitle.setBackground(Colour.CORAL);

//標題
wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN); //邊框線條
wcf_title.setBackground(Colour.GREY_25_PERCENT);
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直對齊
wcf_title.setAlignment(Alignment.CENTRE); //水平對齊
wcf_title.setWrap(false); //是否換行

//中
wcf_center.setBorder(Border.NONE, BorderLineStyle.THIN); //邊框線條
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直對齊
wcf_center.setAlignment(Alignment.CENTRE); //水平對齊
wcf_center.setWrap(false); //是否換行

//左
wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN);
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf_left.setAlignment(Alignment.LEFT);
wcf_left.setWrap(false);

/*產生標題*/
WritableSheet sheet = workbook.createSheet(reportName, 0);

/** 定義當前行指針 */
int currentLineNumber = 0;

/*定義表頭寬度*/
int[] colWidth = new int[colNameArray.length];
for(int k = 0; k < colNameArray.length; k++){
colWidth[k] = titleArray[k].length()*2 + 10;
}
/** ---------產生表格Title--------- */

sheet.mergeCells(0, 0, titleArray.length - 1, 0);
sheet.addCell(new Label(0, 0, reportName, wcf_maintitle));
currentLineNumber++;

/** ---------產生Headers--------- */
for (int i = 0; i < titleArray.length; i++) {
sheet.addCell(new Label(i, currentLineNumber, titleArray[i], wcf_title));
}

相關api參考http://www.andykhan.com/jexcelapi/

中文資料http://www-128.ibm.com/developerworks/cn/java/l-javaExcel/

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