java把一堆数据写入Excel模板当中去 数据exceljava 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用的jstl给你个例子数据你自己搞定@RequestMapping(value = "/xiangmugl/toexportall") public void toExportAll(HttpServletRequest request, HttpServletResponse response, Model model) { List<Model> list = xiangMuService.queryList(0, 0, map); String bt[] = new String[19];// 表头 bt[0] = "办学单位(中心)"; bt[1] = "序号"; bt[2] = "项目编号"; bt[3] = "项目(班级)名称"; bt[4] = "人次"; bt[5] = "收费形式"; String wjm = "浙江大学教育培训项目经费分成结算清单汇总表"; exportexcel(bt, list, response, wjm); } /** * * @param bt表头名称 * @param list数据 * @param response * @param wjm表名 */ public boolean exportexcel(String bt[], List<Model> list, HttpServletResponse response, String wjm) { try { OutputStream os = response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 response.setHeader("Content-disposition", "attachment; filename=" + new String(wjm.getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头 response.setContentType("application/msexcel");// 定义输出类型 WritableWorkbook book = Workbook.createWorkbook(os); // 建立excel文件 WritableSheet sheet = book.createSheet("Sheet1", 0); // 创建一个工作表 // 设置表格第一行的样式属性 WritableFont titleFont = new WritableFont(WritableFont .createFont("宋体"), 14, WritableFont.BOLD); WritableCellFormat titleFormat = new WritableCellFormat(titleFont); titleFormat.setAlignment(Alignment.CENTRE);// 设置为水平居中 titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置为垂直居中 sheet.setRowView(0, 1000); sheet.mergeCells(0, 0, 15, 0); Label titleLabel = new Label(0, 0, bt[0], titleFormat); sheet.addCell(titleLabel); // 设置表格第二行的样式属性 WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD); WritableCellFormat format = new WritableCellFormat(font); format.setWrap(true);// 设置自动换行 // format.setBorder(Border.ALL, BorderLineStyle.THIN); format.setAlignment(Alignment.CENTRE);// 设置为居中 format.setVerticalAlignment(VerticalAlignment.CENTRE); sheet.setRowView(1, 800); sheet.mergeCells(0, 1, 1, 1); sheet.mergeCells(2, 1, 15, 1); // 设置表格第三行的样式属性 for (int i = 2; i < bt.length - 1; i++) { sheet.setColumnView(i - 2, 12); sheet.addCell(new Label(i - 2, 2, bt[i], format)); } sheet.setColumnView(2, 20); sheet.setColumnView(15, 20); sheet.setRowView(2, 800); // 设置表格下面主要数据区域的样式属性 WritableCellFormat format2 = new WritableCellFormat(font); format2.setVerticalAlignment(VerticalAlignment.CENTRE); WritableCellFormat renciFormat = new WritableCellFormat( new NumberFormat("0")); WritableCellFormat feiyongFormat = new WritableCellFormat( new NumberFormat("0.00")); for (int i = 0; i < list.size(); i++) { sheet.addCell(new Label(i, 0, list.getName(), format)); // ... } book.write(); book.close(); } catch (IOException e) { return false; } catch (RowsExceededException e) { return false; } catch (WriteException e) { return false; } return true; } rs.next()为空 关于static{init();}??? 一个HIBERNATE多表查询的问题,困扰我半个月了 Hibernate-Many-to-one 求助 请教一个服务器的基础问题 axis2中可否传递HashMap? 高分悬赏-poi访问加密excel 文档管理系统jLibrary 有人用過嗎? 把人整疯的一个关于struts乱码的问题。高手请帮忙。 将字符串转化为Date类型问题 poi word07 表格问题
给你个例子
数据你自己搞定@RequestMapping(value = "/xiangmugl/toexportall")
public void toExportAll(HttpServletRequest request,
HttpServletResponse response, Model model) {
List<Model> list = xiangMuService.queryList(0, 0, map);
String bt[] = new String[19];// 表头
bt[0] = "办学单位(中心)";
bt[1] = "序号";
bt[2] = "项目编号";
bt[3] = "项目(班级)名称";
bt[4] = "人次";
bt[5] = "收费形式";
String wjm = "浙江大学教育培训项目经费分成结算清单汇总表";
exportexcel(bt, list, response, wjm);
} /**
*
* @param bt表头名称
* @param list数据
* @param response
* @param wjm表名
*/
public boolean exportexcel(String bt[], List<Model> list,
HttpServletResponse response, String wjm) {
try {
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ new String(wjm.getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
WritableWorkbook book = Workbook.createWorkbook(os); // 建立excel文件
WritableSheet sheet = book.createSheet("Sheet1", 0); // 创建一个工作表
// 设置表格第一行的样式属性
WritableFont titleFont = new WritableFont(WritableFont
.createFont("宋体"), 14, WritableFont.BOLD);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
titleFormat.setAlignment(Alignment.CENTRE);// 设置为水平居中
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置为垂直居中
sheet.setRowView(0, 1000);
sheet.mergeCells(0, 0, 15, 0);
Label titleLabel = new Label(0, 0, bt[0], titleFormat);
sheet.addCell(titleLabel);
// 设置表格第二行的样式属性
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
10, WritableFont.NO_BOLD);
WritableCellFormat format = new WritableCellFormat(font);
format.setWrap(true);// 设置自动换行
// format.setBorder(Border.ALL, BorderLineStyle.THIN);
format.setAlignment(Alignment.CENTRE);// 设置为居中
format.setVerticalAlignment(VerticalAlignment.CENTRE);
sheet.setRowView(1, 800);
sheet.mergeCells(0, 1, 1, 1);
sheet.mergeCells(2, 1, 15, 1);
// 设置表格第三行的样式属性
for (int i = 2; i < bt.length - 1; i++) {
sheet.setColumnView(i - 2, 12);
sheet.addCell(new Label(i - 2, 2, bt[i], format));
}
sheet.setColumnView(2, 20);
sheet.setColumnView(15, 20);
sheet.setRowView(2, 800);
// 设置表格下面主要数据区域的样式属性
WritableCellFormat format2 = new WritableCellFormat(font);
format2.setVerticalAlignment(VerticalAlignment.CENTRE);
WritableCellFormat renciFormat = new WritableCellFormat(
new NumberFormat("0"));
WritableCellFormat feiyongFormat = new WritableCellFormat(
new NumberFormat("0.00"));
for (int i = 0; i < list.size(); i++) {
sheet.addCell(new Label(i, 0, list.getName(), format));
// ...
}
book.write();
book.close();
} catch (IOException e) {
return false;
} catch (RowsExceededException e) {
return false;
} catch (WriteException e) {
return false;
}
return true;
}