springmvc
service层
public void exportDataArea(HttpServletResponse resp, List<DataArea> list)
throws IOException {
// TODO Auto-generated method stub
OutputStream out = null;
try {
out = resp.getOutputStream();
String exlName = "投放地域.xls";
resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
resp.setHeader("Content-disposition", "attachment; filename="
+ new String(exlName.getBytes("GBK"), "ISO8859-1"));
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("投放地域");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle headerStyle = wb.createCellStyle();
Font font = wb.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerStyle.setFont(font);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
String[] excelHeader = { "投放平台", "类别", "省份", "指数", "日期", "创建人",
"创建时间" };
int[] excelHeaderWidth = { 150, 150, 50, 50, 150, 100, 150 };
// 设置列宽
for (int i = 0; i < excelHeaderWidth.length; i++) {
sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
}
// 创建标头
for (int i = 0; i < excelHeader.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(headerStyle);
}
// 表内容设置样式
for (int i = 0; i < excelHeaderWidth.length; i++) {
}
// 创建表内容
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
DataArea area = list.get(i);
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue(area.getPlatformName());
cell0.setCellStyle(style);
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue(area.getCategoryName());
cell1.setCellStyle(style);
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue(area.getProvince());
cell2.setCellStyle(style);
HSSFCell cell3 = row.createCell(3);
cell3.setCellValue(area.getIndexNum());
cell3.setCellStyle(style);
String dateStr = null;
if(area.getDate()!=null){
dateStr = DateUtils.format(area.getDate(), "yyyy/MM/dd");
}
HSSFCell cell4 = row.createCell(4);
cell4.setCellValue(dateStr);
cell4.setCellStyle(style);
HSSFCell cell5 = row.createCell(5);
cell5.setCellValue(area.getCreatorName());
cell5.setCellStyle(style);
String createTimeStr = null;
if(area.getCreateTime()!=null){
createTimeStr = DateUtils.format(area.getCreateTime(), "yyyy/MM/dd");
}
HSSFCell cell6 = row.createCell(6);
cell6.setCellValue(createTimeStr);
cell6.setCellStyle(style);
}
wb.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
controller 层
调用时 方法返回值是String 返回 null
@RequestMapping("/exportExcel.jhtml")
public String exportExcel(HttpServletRequest req, HttpServletResponse resp,
HttpSession session, DataArea dataArea) {
// 从session中获取投放平台map 查询时使用
Map<Integer, String> dataPlatformMap = (Map<Integer, String>) session
.getAttribute("platformMap");
if (dataPlatformMap!=null && dataArea != null && dataArea.getPlatformName() != null) {
for (Map.Entry<Integer, String> map : dataPlatformMap.entrySet()) {
if (map.getValue().equals(dataArea.getPlatformName())) {
dataArea.setPlatformId(map.getKey());
}
}
}
// 从session中获取类别map 查询时使用
Map<Integer, String> dataCategoryMap = (Map<Integer, String>) session
.getAttribute("dataCategoryMap");
if (dataCategoryMap!=null && dataArea != null && dataArea.getCategoryName() != null) {
for (Map.Entry<Integer, String> map : dataCategoryMap.entrySet()) {
if (map.getValue().equals(dataArea.getCategoryName())) {
dataArea.setCategoryId(map.getKey());
}
}
}
List<DataArea> list = dataAreaService.exportList(dataArea);
try {
dataAreaService.exportDataArea(resp, list);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}