//1.查询要导出的数据
List<Area> areas = areaService.findAll();
//2.创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//3.创建一个sheet
HSSFSheet sheet = workbook.createSheet("区域信息");
//4.创建一个行
HSSFRow row = sheet.createRow(0);
//5.创建Cell ---标题
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("省");
row.createCell(2).setCellValue("市");
row.createCell(3).setCellValue("区");
row.createCell(4).setCellValue("邮编");
//遍历areas去创建行数据
for(int i = 0 ; i < areas.size(); i++){
Area area = areas.get(i);
HSSFRow temp = sheet.createRow(i+1);
temp.createCell(0).setCellValue(area.getId());
temp.createCell(1).setCellValue(area.getProvince());
temp.createCell(2).setCellValue(area.getCity());
temp.createCell(3).setCellValue(area.getDistrict());
temp.createCell(4).setCellValue(area.getPostcode());
}
//提供页面下载
OutputStream out = ServletActionContext.getResponse().getOutputStream();
//设置文件名称
String filename = "区域信息.xls";
filename = new String(filename.getBytes(),"ISO8859-1");
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");
//以附件的方式下载文件
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename="+filename);
List<Area> areas = areaService.findAll();
//2.创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//3.创建一个sheet
HSSFSheet sheet = workbook.createSheet("区域信息");
//4.创建一个行
HSSFRow row = sheet.createRow(0);
//5.创建Cell ---标题
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("省");
row.createCell(2).setCellValue("市");
row.createCell(3).setCellValue("区");
row.createCell(4).setCellValue("邮编");
//遍历areas去创建行数据
for(int i = 0 ; i < areas.size(); i++){
Area area = areas.get(i);
HSSFRow temp = sheet.createRow(i+1);
temp.createCell(0).setCellValue(area.getId());
temp.createCell(1).setCellValue(area.getProvince());
temp.createCell(2).setCellValue(area.getCity());
temp.createCell(3).setCellValue(area.getDistrict());
temp.createCell(4).setCellValue(area.getPostcode());
}
//提供页面下载
OutputStream out = ServletActionContext.getResponse().getOutputStream();
//设置文件名称
String filename = "区域信息.xls";
filename = new String(filename.getBytes(),"ISO8859-1");
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");
//以附件的方式下载文件
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename="+filename);
workbook.write(out);
}