首页 | 旅游 | 健康 | 时尚 | 下载 | 论坛 | 图文 | 专题 | 地图
资讯 IT人 电脑入门 操作系统 上网 办公 技巧 硬件 软件 网络 图像 多媒体 程序 数据库 网页制作 网站开发 网游 安全 加密 企业

JavaEE中把数据导出为Excel

巧巧读书 2008-06-30 中国IT实验室 佚名 技术论坛

网上又好几种用于Java环境中导入,导出Excel数据的插件。我用的是jexcelapi,下载地址:http://www.andykhan.com/jexcelapi/download.html 当前最高版本为2.6

程序实例如下:

view plaincopy to clipboardprint?
public void exportClassroom(OutputStream os) throws PaikeException {

try {

WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件

WritableSheet wsheet = wbook.createSheet("教室信息表", 0); //工作表名称

//设置Excel字体

WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,

WritableFont.BOLD, false,

jxl.format.UnderlineStyle.NO_UNDERLINE,

jxl.format.Colour.BLACK);

WritableCellFormat titleFormat = new WritableCellFormat(wfont);

String[] title = { "教室名", "容 量", "类 型", "其他说明" };

//设置Excel表头

for (int i = 0; i < title.length; i++) {

Label excelTitle = new Label(i, 0, title[i], titleFormat);

wsheet.addCell(excelTitle);

}

int c = 1; //用于循环时Excel的行号

ClassroomService cs = new ClassroomService();

List list = cs.findAllClassroom(); //这个是从数据库中取得要导出的数据

Iterator it = list.iterator();

while (it.hasNext()) {

ClassroomDTO crdto = (ClassroomDTO) it.next();

Label content1 = new Label(0, c, crdto.getRoomname());

Label content2 = new Label(1, c, crdto.getCapicity().toString());

Label content3 = new Label(2, c, crdto.getRoomTypeId()

.toString());

Label content4 = new Label(3, c, crdto.getRemark());

wsheet.addCell(content1);

wsheet.addCell(content2);

wsheet.addCell(content3);

wsheet.addCell(content4);

c++;

}

wbook.write(); //写入文件

wbook.close();

os.close();

} catch (Exception e) {

throw new PaikeException("导出文件出错");

}

}

本类最热图文
巧巧读书图文推荐
Google
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章