发布网友 发布时间:2022-04-19 10:50
共1个回答
热心网友 时间:2022-05-13 05:02
java导出Excel java 代码 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.axon.fable.sams.view.action; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.serv ... java导出Excel例举方式 方法一:导出Excel数据的插件jexcelapi 程序实例如下: 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("导出文件出错"); } } 方法二:直接用Java代码实现导出Excel报表 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */