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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值