利用HSSFSheet创建下载Excel模板(xxx.xls)

--------------------------------html------------------------------------------------
   
 <a href="<%=basePath%>suspect/TemplateSelect.do">下载excel模板</a>
--------------------------------java------------------------------------------------

@RequestMapping(value = "/TemplateDownload", method =  RequestMethod.GET)
    @ResponseBody
    public void TemplateDownload(HttpServletResponse response) throws IOException {

        try {
            //创建Excel文档
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("Sheet1");

            // 设置行宽为20
            sheet.setDefaultColumnWidth(20);
            // 创建样式并设置背景颜色为灰色
            HSSFCellStyle style = workbook.createCellStyle();
            style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            //创建表头
            HSSFRow row = sheet.createRow(0);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue("姓名");
            cell.setCellStyle(style);
            cell = row.createCell(1);
            cell.setCellValue("身份证号码");
            cell.setCellStyle(style);
            cell = row.createCell(2);
            cell.setCellValue("照片地址");
            cell.setCellStyle(style);
            cell = row.createCell(3);
            cell.setCellValue("关系人数");
            cell.setCellStyle(style);
            cell = row.createCell(4);
            cell.setCellValue("关系人");
            cell.setCellStyle(style);

            //设置文件名和文件类型
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=ZDRY.xls" );

            //写入Excel文档并输出到输出流
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值