标签:
import java.io.OutputStream; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import jxl.Workbook; import jxl.format.UnderlineStyle; import jxl.write.Label; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExportExcel { /** * 导出 */ public static void exportExcel(HttpServletResponse response, List<Map<String, Object>> list) { OutputStream os; try { os = response.getOutputStream(); response.reset();// 清空输出流 response.setHeader("Content-disposition", "attachment; filename="+new Date().getTime()+".xls");// 设定输出文件头 response.setContentType("application/msexcel");// 定义输出类型 WritableWorkbook book = Workbook.createWorkbook(os); // 建立excel文件 WritableSheet sheet = book.createSheet("Sheet1", 0); // 创建一个工作表 jxl.write.WritableFont wfc = new jxl.write.WritableFont( WritableFont.ARIAL, 13, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE); jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc); sheet.setColumnView(0, 15);//设置列宽度 sheet.setColumnView(1, 15); sheet.setColumnView(2, 20); sheet.setColumnView(3, 20); sheet.setColumnView(4, 20); sheet.setColumnView(5, 15); Label label_0 = new Label(0, 0, "请假类型", wcfFC);// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0) sheet.addCell(label_0); Label label_1 = new Label(1, 0, "姓名", wcfFC); sheet.addCell(label_1); Label label_2 = new Label(2, 0, "部门", wcfFC); sheet.addCell(label_2); Label label_3 = new Label(3, 0, "请假开始时间", wcfFC); sheet.addCell(label_3); Label label_4 = new Label(4, 0, "请假结束时间", wcfFC); sheet.addCell(label_4); Label label_5 = new Label(5, 0, "请假天数", wcfFC); sheet.addCell(label_5); if(list != null && list.size() > 0) { wfc = new jxl.write.WritableFont( WritableFont.ARIAL, 13, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE); wcfFC = new jxl.write.WritableCellFormat(wfc); int i = 1; List<Dictionary> leaveTypeList = DictinaryCache.getDictionarysByType("LeaveType"); for(Map<String, Object> dataMap : list) { label_0 = new Label(0, i, dataMap.get("type")+"", wcfFC);// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0) sheet.addCell(label_0); label_1 = new Label(1, i, dataMap.get("creator_name")+"", wcfFC); sheet.addCell(label_1); label_2 = new Label(2, i, dataMap.get("dept_name")+"", wcfFC); sheet.addCell(label_2); label_3 = new Label(3, i, dataMap.get("begin_time")+"", wcfFC); sheet.addCell(label_3); label_4 = new Label(4, i, dataMap.get("end_time")+"", wcfFC); sheet.addCell(label_4); label_5 = new Label(5, i, dataMap.get("day")+"", wcfFC); sheet.addCell(label_5); i++; } } // 写入数据并关闭文件 book.write(); book.close(); System.out.println("Excel创建成功"); } catch (Exception e) { e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/yushouling/p/5036688.html