jxl使用简介

来源:互联网 发布:ldc1000中文数据手册 编辑:程序博客网 时间:2024/05/07 00:55

最基本的生成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/

原创粉丝点击