controller层
@Controller
public class UseCaseController{
@Autowired
private UseCaseService useCaseService;
/**
*进入到useCase_excel页面
*/
@RequestMapping(value="useCase_excel.xhtml",method="RequestMethod.GET")
public ModelAndView download(){
ModelAndView model = new ModelAndView("useCase_excel");
}
/**
*实现下载导出excel功能
*/
@RequestMapping(value="useCase_excel_download",method="RequestMethod.GET")
publice String download(HttpServletRequest request,HttpServletResponse response,String requirementId,String status,String stage){
String fileName="测试案例报表";
List<UseCase> useCases = useCaseService.createData(requirement,status,stage);
List<Map<String,Object>> list =createExcelRecord(useCases);
//列名
String columnNames[]={
"测试案例编号","需求编号","测试案例负责人","测试人","功能描述","完成率","状态","阶段","计划执行日期","更新日期","建立日期","备注"};
//Map中的key
String keys[]={
"useCaseId","requirementId","useCaseOwner","useCaseTester","functionDesc","percent","status","stage","startTimeDesc","updateTimeDesc","createTimeDesc","remark"};
ByteArrayOutputStream os = new ByteArrayOutputStream();
try{
UseCaseExcelUtil.createWorkBook(list,keys,columnNames).write(os);
}catch(IOException e){
e.printStackTrace();
}
byte content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(is);
bos = new BufferesOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}catch (final IOException e) {
throw e; } finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return null;
}
private List<Map<String, Object>> createExcelRecord(List<UseCase> useCases) {
List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("sheetName", "sheet1");
listmap.add(map);
UseCase useCase=null;
for (int j = 0; j < useCases.size(); j++) {
useCase=useCases.get(j);
Map<String, Object> mapValue = new HashMap<String, Object>();
mapValue.put("useCaseId", useCase.getUseCaseId());
mapValue.put("requirementId", useCase.getRequirementId());
mapValue.put("useCaseOwner", useCase.getUseCaseOwner());
mapValue.put("