您的位置:首页 > Web前端 > CSS

通过jxl来生成,有单元格样式的excel

2016-06-15 10:35 429 查看
先贴代码,可以直接复制运行

package com;

import java.io.File;

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

public class CreateExcel {

public static void main(String[] args) {
File fi = new File("w:/a.xls");

try {

WritableWorkbook workBook = jxl.Workbook.createWorkbook(fi);

// 创建sheet页 给sheet页命名
WritableSheet sheet = workBook.createSheet("普通", 0);

// 创建表头
// 添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
sheet.mergeCells(0, 0, 10, 0);
// 设置第一行的高度
sheet.setRowView(0, 680, false);
// 设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableFont bold = new WritableFont(WritableFont.createFont("宋体"),16, WritableFont.BOLD);
// 生成一个单元格样式控制对象
WritableCellFormat titleFormate = new WritableCellFormat(bold);
// 单元格中的内容水平方向居中
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);
// 单元格的内容垂直方向居中
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
Label title = new Label(0, 0, "中国法律图书有限公司订单", titleFormate);
sheet.addCell(title);

bold = new WritableFont(WritableFont.createFont("宋体"),11);
titleFormate = new WritableCellFormat(bold);
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
title = new Label(10,10,"这里填写内容",titleFormate);
sheet.addCell(title);

workBook.write();
workBook.close();

} catch (Exception e) {
e.printStackTrace();
}

}

}


用jxl操作excel,根据实际情况,也可以选择poi.
poi内存占用比较大,但是,方法比较全,功能强大点.

jxl内存占用较小,可以处理大批量数据,相对之下,poi操作大数据容易报错.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: