Java Excel导出

@RequestMapping(value = "/exp/exportExcelofEnv")
    public void exportExcelofEnv(String title, String yqlx, String glqy, String shzt, String qyjc,
            HttpServletResponse rep, HttpServletRequest req) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            title = parseStr(title);
            yqlx = parseStr(yqlx);
            glqy = parseStr(glqy);
            shzt = parseStr(shzt);
            qyjc = parseStr(qyjc);
            
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
            String time = format.format(date);
            String fileName = "企业基础信息统计" + time ;
            
            HSSFSheet sheet = workbook.createSheet(fileName);

            // 产生表格标题行
            HSSFRow rowm = sheet.createRow(0);
            HSSFCell cellTitle = rowm.createCell(0);

            // 导出表的列名
            String[] rowName = {"区域", "企业名称", "行业类别", "企业类型", "企业规模", "关注程度", "统一信用代码", "企业状态"};

            List<EnterpriseInfoEntity> dataList = enterpriseRealMonitorService.queryEnterpriseInfos(title, yqlx, glqy, shzt, qyjc);
            
            // sheet样式定义【】
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook,16);
            HSSFCellStyle style = this.getStyle(workbook);
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
            cellTitle.setCellStyle(columnTopStyle);
            cellTitle.setCellValue(fileName);

            // 定义所需列数
            int columnNum = rowName.length;
            HSSFRow rowRowName = sheet.createRow(2);
            columnTopStyle = this.getColumnTopStyle(workbook,14);
            // 将列头设置到sheet的单元格中
            for (int n = 0; n < columnNum; n++) {
                HSSFCell cellRowName = rowRowName.createCell(n);
                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
                HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
                cellRowName.setCellValue(text);
                cellRowName.setCellStyle(columnTopStyle);

            }
            // 将查询到的数据设置到sheet对应的单元格中
            for (int i = 0; i < dataList.size(); i++) {
                EnterpriseInfoEntity entity = dataList.get(i);// 遍历每个对象
                HSSFRow row = sheet.createRow(i + 3);// 创建所需的行数

                HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(entity.getGlqy());
                cell.setCellStyle(style); 
                HSSFCell cell2 = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
                cell2.setCellValue(entity.getQymc());
                cell2.setCellStyle(style); 
                HSSFCell cell3 = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
                cell3.setCellValue(entity.getHylb());
                cell3.setCellStyle(style); 
                HSSFCell cell4 = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
                cell4.setCellValue(entity.getQylb());
                cell4.setCellStyle(style); 
                HSSFCell cell5 = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
                cell5.setCellValue(entity.getQygm());
                cell5.setCellStyle(style); 
                HSSFCell cell6 = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
                cell6.setCellValue(entity.getGzcd());
                cell6.setCellStyle(style); 
                HSSFCell cell7 = row.createCell(6, HSSFCell.CELL_TYPE_STRING);
                cell7.setCellValue(entity.getShxydm());
                cell7.setCellStyle(style); 
                HSSFCell cell8 = row.createCell(7, HSSFCell.CELL_TYPE_STRING);
                cell8.setCellValue(entity.getQyzt());
                cell8.setCellStyle(style); 
            }

            // 让列宽随着导出的列长自动适应
            for (int colNum = 0; colNum < columnNum; colNum++) {
                int columnWidth = sheet.getColumnWidth(colNum) / 256;
                for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
                    HSSFRow currentRow;
                    if (sheet.getRow(rowNum) == null) {
                        currentRow = sheet.createRow(rowNum);
                    } else {
                        currentRow = sheet.getRow(rowNum);
                    }
                    if (currentRow.getCell(colNum) != null) {
                        HSSFCell currentCell = currentRow.getCell(colNum);
                        if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            int length = currentCell.getStringCellValue().getBytes().length;
                            if (columnWidth < length) {
                                columnWidth = length;
                            }
                        }
                    }
                }
                sheet.setColumnWidth(colNum, (columnWidth + 6) * 256);
            }
            sheet.setColumnWidth(0, 20 * 256);
            sheet.setColumnWidth(1, 35 * 256);
            outputFile(fileName, rep, workbook);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void outputFile(String fileName, HttpServletResponse rep, HSSFWorkbook workbook) {
        if (workbook != null) {
            OutputStream ouputStream = null;
            try {
                
                // 设置信息给客户端不解析
                String type = new MimetypesFileTypeMap().getContentType(fileName);
                // 设置content-type,即告诉客户端所发送的数据属于什么类型
                rep.setContentType(type);
                rep.setContentType("application/msexcel");
                // 设置编码
                String name = URLEncoder.encode(fileName, "UTF-8");
                // 设置扩展头,当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
                rep.setHeader("Content-Disposition", "attachment;filename=" + name+ ".xls");
                ouputStream = rep.getOutputStream();  
                workbook.write(ouputStream); 
                ouputStream.flush();
                ouputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                    if(null != ouputStream) {
                    try {
                        ouputStream.close();
                    } catch (Exception e) {
                        ouputStream =null;
                    }
                   
               }
           }
        }
    }

    /*
     * 列头单元格样式
     */
    public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook,int fontSize) {
        // 设置字体
        HSSFFont font = workbook.createFont();

        // 设置字体大小
        font.setFontHeightInPoints((short) fontSize);
        // 字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 设置字体名字
        font.setFontName("Courier New");
        // 设置样式
        HSSFCellStyle style = workbook.createCellStyle();
        // 设置低边框
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        // 设置低边框颜色
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        // 设置右边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 设置顶边框
        style.setTopBorderColor(HSSFColor.BLACK.index);
        // 设置顶边框颜色
        style.setTopBorderColor(HSSFColor.BLACK.index);
        // 在样式中应用设置的字体
        style.setFont(font);
        // 设置自动换行
        style.setWrapText(false);
        // 设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        return style;

    }

    public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
        // 设置字体
        HSSFFont font = workbook.createFont();
        // 设置字体大小
        font.setFontHeightInPoints((short) 10);
        // 字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        // 设置字体名字
        font.setFontName("Courier New");
        // 设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        // 设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        // 设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        // 设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        // 设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        // 设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        // 在样式用应用设置的字体;
        style.setFont(font);
        // 设置自动换行;
        style.setWrapText(false);
        // 设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        return style;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值