参考代码
@GET
@Path("/downloadTemplate")
@Produces("*/*")
public String downloadTemplate(){
System.out.println("请进!");
try {
Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
String templatePath = request.getServletContext().getRealPath("/") + "\\make\\xlsprint\\";
//通过流打开模板grade.xls
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(templatePath + "userTemplate.xls")));
//打开一个工作表
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow nRow = null;
HSSFCell nCell = null;
//设置大标题
nRow = sheet.getRow(0);
nCell = nRow.getCell(1);
nCell.setCellStyle(nCell.getCellStyle());
// 生成流对象
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 将excel写入流
wb.write(byteArrayOutputStream);
// 利用工具类DownloadBase,封装弹出下载框
String outFileName = "用户信息表.xls";
DownloadBase down = new DownloadBase();
//获取response对象
HttpServletResponse response = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
down.download(byteArrayOutputStream, response, outFileName);
} catch (Exception e) {
return StringParam.returnPageFalse();
}
return StringParam.returnPageTrue();
}