表格导出

//表格导出

@RequestMapping(value = "/****", method = RequestMethod.GET)
    public void exportTable(Model model, HttpServletResponse response) throws IOException {
        ServletOutputStream out = response.getOutputStream();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet("信息表.xls");
        HSSFCellStyle cellstyle = hssfWorkbook.createCellStyle();// 设置表头样式
        cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中

        HSSFCellStyle headerStyle = hssfWorkbook.createCellStyle();// 创建标题样式
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 设置垂直居中
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置水平居中
        HSSFFont headerFont = hssfWorkbook.createFont(); // 创建字体样式
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
        headerFont.setFontName("Times New Roman"); // 设置字体类型
        headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
        headerStyle.setFont(headerFont); // 为标题样式设置字体样式
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THICK); // 下边框
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THICK);// 左边框
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THICK);// 上边框
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_THICK);// 右边框
        headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置背景色
        headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//        sheet.setColumnWidth(5, (int) 50 * 100);
//        sheet.setColumnWidth(6, (int) 50 * 100);
//        sheet.setColumnWidth(7, (int) 40 * 100);
//        sheet.setColumnWidth(8, (int) 50 * 100);
        HSSFRow row0 = sheet.createRow(0);
        
        
        String[] content = new String[] { "序号", "姓名", "年龄", "性别", "出生地", "户籍", "生日","备注" };
        for (int i = 0; i < content.length; i++) {
            HSSFCell cell = row0.createCell(i);
            cell.setCellStyle(headerStyle);
            cell.setCellValue(content[i]);
        }

        for (数据类型 单元格数据 : 导出数据) {
            HSSFRow row2 = sheet.createRow(sheet.getLastRowNum() + 1);
            row2.createCell(0).setCellValue(sheet.getLastRowNum());
            row2.createCell(1).setCellValue(device.getName());......
            row2.createCell(11).setCellValue(device.getRemark());
        }
        response.setContentType("application/binary;charset=UTF-8");
        String fileName = new String(sheet.getSheetName().getBytes("utf-8"), "iso-8859-1");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        hssfWorkbook.write(out);
    }

你可能感兴趣的:(表格导出)