导出一个excel文件
首先是要获取到导出excel文件的数据,下面的一个action就是导出一个excel文件的操作
public ActionForward exportExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
KmssMessages messages = new KmssMessages();//记录消息
List<Map> list = null;
String title = (String) request.getSession().getAttribute("exceltitlenyb");// Excel文件名
list= (List<Map>) request.getSession().getAttribute("maplist1");//获取要导出的数据
response.setContentType("application/vnd.ms-excel; charset=UTF-8");//设置头文件
response.addHeader("Content-Disposition", "attachment;filename=\""
+ new String(title.getBytes("GBK"), "ISO-8859-1") + ".xls\"");
ServletOutputStream out = response.getOutputStream();//将输出流建在response上,到处的时候就是直接下载到客户端
try {
HSSFWorkbook workbook = buildWorkBook(list, title);//调用创建excel表的方法
workbook.write(out);
} catch (Exception e) {
messages.addError(e);
e.printStackTrace();
} finally {
out.flush();
out.close();
}
TimeCounter.logCurrentTime("Action-exportCalendar", false, getClass());
if (messages.hasError()) {//判断记录的消息是否有一些error如果有就返回到失败页面
KmssReturnPage.getInstance(request).addMessages(messages)
.addButton(KmssReturnPage.BUTTON_CLOSE).save(request);
return getActionForward("failure", mapping, form, request, response);
} else {
return null;
}
}
private HSSFWorkbook buildWorkBook(List<Map> list, String title) {
// TODO Auto-generated method stub
/* 创建一个excel工作簿对象 */
HSSFWorkbook workbook = new HSSFWorkbook();
/* 创建一个工作表对象 */
//此次下载的表格以两列为例
HSSFSheet sheet = workbook.createSheet();
sheet.setColumnWidth(0, 500);//第一列表格的宽度
sheet.setColumnWidth(1, 500);//第二列表格的宽度
workbook.setSheetName(0, title);
int rowIndex = 0;
HSSFCellStyle row1CellStyle = workbook.createCellStyle();
row1CellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中对齐
row1CellStyle.setVerticalAlignment((short) 1);// 垂直j
HSSFFont font1 = workbook.createFont();
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
font1.setFontHeightInPoints((short)16);
row1CellStyle.setFont(font1);
row1CellStyle.setWrapText(true);
/* 标题行 */
HSSFRow titlerow = sheet.createRow(rowIndex++);
titlerow.setHeight((short)500); //设置表头单元格的高度
HSSFCellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中对齐
titleCellStyle.setVerticalAlignment((short) 1);// 垂直j
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
font.setFontHeightInPoints((short)14);
titleCellStyle.setFont(font);
HSSFCell[] cells = new HSSFCell[2];
for (int i = 0; i < cells.length; i++) {
cells[i] = titlerow.createCell(i);
cells[i].setCellStyle(titleCellStyle);
}
//设置单元格的宽度
sheet.setColumnWidth(0, 6000);// 机构
sheet.setColumnWidth(1, 6000);// 传稿数量
cells[0].setCellValue("传稿机构");//标题行第一列数据
cells[1].setCellValue("传稿数量");//标题行第二列数据
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
hssfCellStyle.setVerticalAlignment((short)0);
hssfCellStyle.setWrapText(true);
/* 内容行 */
if (list != null && !list.isEmpty()) {//把list里面的数据依次输入到内容行里面
for (int i = 0; i < list.size(); i++) {
Map map = list.get(i);
HSSFRow contentrow = sheet.createRow(rowIndex++);
contentrow.setHeight((short)400);
HSSFCell[] contentcells = new HSSFCell[2];
for (int j = 0; j < contentcells.length; j++) {
contentcells[j] = contentrow.createCell(j);
contentcells[j].setCellStyle(hssfCellStyle);
}
contentcells[0].setCellValue(map.get("jg").toString());
contentcells[1].setCellValue(map.get("count").toString());
}}
return workbook;
}
}