jsp页面上的代码如下:
response.setContentType("application/vnd.ms-excel;charset=GBK");
String filename = new String(("excel名称.xls").getBytes(),
"ISO-8859-1");
response.setHeader("Content-Disposition", "attachment;"
+ " filename=" + filename);
<%
JavaBean bean = new JavaBean();
bean .loadDataToExcel(response);
%>
JavaBean 页面上的代码如下:
public boolean loadDataToExcel(HttpServletResponse response,
List items) {
boolean flag = false;
try {
WritableWorkbook wwb = Workbook.createWorkbook(response
.getOutputStream());
WritableSheet asheet = wwb.createSheet("处理信息统计表", 52);
// 定义一个新字体,参数说明:字体,大小,格式,是否斜体,颜色(默认黑色)
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false);
// 定义一个新字体,参数说明:字体,大小,格式,是否斜体,颜色(默认黑色)//不加粗
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD, false);
// 设置单元格格式
WritableCellFormat wchB = new WritableCellFormat(wfc);
// 使单元格居中
wchB.setAlignment(Alignment.CENTRE);
// 设置制表单位&制表日期格式
WritableCellFormat date = new WritableCellFormat(wf);
// 设置表头格式
WritableCellFormat tTitle = new WritableCellFormat(wf);
tTitle.setAlignment(Alignment.CENTRE);
tTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
tTitle.setWrap(true);
tTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置数据显示格式
WritableCellFormat data = new WritableCellFormat(wf);
data.setBorder(Border.ALL, BorderLineStyle.THIN);
// data.setb
// 分别设置单元格的高度&宽度
asheet.setColumnView(0, 12);
asheet.setColumnView(1, 12);
asheet.setColumnView(2, 8);
asheet.setColumnView(3, 8);
asheet.setColumnView(4, 8);
asheet.setColumnView(5, 8);
asheet.setColumnView(6, 8);
asheet.setColumnView(7, 7);
asheet.setColumnView(8, 7);
asheet.setColumnView(9, 7);
asheet.setColumnView(10, 7);
asheet.setColumnView(11, 7);
asheet.setColumnView(12, 7);
asheet.setColumnView(13, 7);
asheet.setColumnView(14, 7);
/* 开始写入xls文件 */
// 导出excel文件标题
Label labelC = new Label(0, 0, "处理信息统计表", wchB);
asheet.addCell(labelC);
// 合并第一行16个单元格为1个作为标题栏
asheet.mergeCells(0, 0, 15, 0);
// 设置表头
labelC = new Label(0, 1, "预报信息", tTitle);
asheet.addCell(labelC);
asheet.mergeCells(0, 1, 3, 1);
labelC = new Label(0, 2, "检查场", tTitle);
asheet.addCell(labelC);
labelC = new Label(1, 2, "车型", tTitle);
asheet.addCell(labelC);
labelC = new Label(2, 2, "车号", tTitle);
asheet.addCell(labelC);
labelC = new Label(3, 2, "轴位及左右", tTitle);
asheet.addCell(labelC);
labelC = new Label(4, 1, "作业场处理信息", tTitle);
asheet.addCell(labelC);
asheet.mergeCells(4, 1, 15, 1);
labelC = new Label(4, 2, "预警级别", tTitle);
asheet.addCell(labelC);
labelC = new Label(5, 2, "检查时间", tTitle);
asheet.addCell(labelC);
labelC = new Label(6, 2, "车次", tTitle);
asheet.addCell(labelC);
labelC = new Label(7, 2, "编组", tTitle);
asheet.addCell(labelC);
labelC = new Label(8, 2, "顺位", tTitle);
asheet.addCell(labelC);
labelC = new Label(9, 2, "轮位", tTitle);
asheet.addCell(labelC);
labelC = new Label(10, 2, "轴号", tTitle);
asheet.addCell(labelC);
labelC = new Label(11, 2, "故障原因", tTitle);
asheet.addCell(labelC);
labelC = new Label(12, 2, "实际测量数据", tTitle);
asheet.addCell(labelC);
labelC = new Label(13, 2, "测量人", tTitle);
asheet.addCell(labelC);
labelC = new Label(14, 2, "复测人", tTitle);
asheet.addCell(labelC);
labelC = new Label(15, 2, "处理方式", tTitle);
asheet.addCell(labelC);
wwb.write();
wwb.close();
flag = true;
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
return flag;
}
注:1. 说明asheet.mergeCells(列1, 行1, 列2, 行2)合并单元格,起始都是0
2 . 在Bean中需要导入两个jar包servlet.jar和jxl.jar