zoukankan      html  css  js  c++  java
  • spring mvc项目中导出excel表格简单实现

    查阅了一些资料,才整理出spring mvc 项目导出excel表格的实现,其实很是简单,小计一下,方便以后查阅,也希望帮助有需要的朋友。

    1.导入所需要依赖(Jar包)。我使用的是maven,所以坐标如下:

        <!-- excel导出 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.10-FINAL</version>
            </dependency>

    2.配置spring mvc 基本环境.(略)

    3.编写方法,添加映射。具体实现如下:

    @RequestMapping("exportException")
        public void  exportExcel(ProblemPlace problemPlace,HttpServletRequest request,HttpServletResponse response){
        //设置响应类型 response.setContentType(
    "application/vnd.ms-excel"); String codedFileName = null; String fileName=null; OutputStream fOut = null; Date d=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); List<PPResult> list=new ArrayList<PPResult>(); if("nolink".equals(problemPlace.getExceptionType())){ fileName="警综关联异常清单"+sdf.format(d); list = ppService.getNoLinkPointException(problemPlace); }else{ fileName="地图撒点异常清单"+sdf.format(d); list = ppService.getNoMapPointException(problemPlace); } // 进行转码,使其支持中文文件名 try { codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls"); String[] headers={"派出所","场所名称","场所地址","场所类型"}; // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet("异常清单"); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth(50); // 生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); // 产生表格标题行 HSSFRow row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } if(list.size()>0){ for (int i = 0; i < list.size(); i++) { //新建一行 HSSFRow createRow = sheet.createRow(i+1); HSSFCell cell0 = createRow.createCell(0); HSSFRichTextString text = new HSSFRichTextString(list.get(i).getSspcs_mc()); cell0.setCellValue(text); HSSFCell cell1 = createRow.createCell(1); HSSFRichTextString text1 = new HSSFRichTextString(list.get(i).getCsmc()); cell1.setCellValue(text1); HSSFCell cell2 = createRow.createCell(2); HSSFRichTextString text2 = new HSSFRichTextString(list.get(i).getCsdz()); cell2.setCellValue(text2); HSSFCell cell3 = createRow.createCell(3); HSSFRichTextString text3 = new HSSFRichTextString(list.get(i).getXslx_mc()); cell3.setCellValue(text3); } } try { OutputStream out = response.getOutputStream(); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } }

    当然,还有很多样式都没有实现,这里只是一个简单的导出功能,有需求的时候再进一步学习。

    字体和样式设置:

    HSSFFont f  = wb.createFont();      
    f.setFontHeightInPoints((short) 11);//字号      
    f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗      
    
    HSSFCellStyle style = wb.createCellStyle();     
    style.setFont(f);      
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中      
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中      
    style.setRotation(short rotation);//单元格内容的旋转的角度      
    
    HSSFDataFormat df = wb.createDataFormat();      
    style1.setDataFormat(df.getFormat("0.00%"));//设置单元格数据格式      
    cell.setCellFormula(string);//给单元格设公式      
    style.setRotation(short rotation);//单元格内容的旋转的角度
  • 相关阅读:
    管理中的“变”与“不变”
    软件项目需求分析与管理的十大疑问
    小商家也要有O2O思维
    互联网时代CIO生存法则
    浅谈项目经理与部门经理之间的关系
    沃尔玛:“最后一公里”的致命伤
    大数据分析案例:永远别忘记天气这个变量
    IT项目中的6类知识转移
    C
    linu入门
  • 原文地址:https://www.cnblogs.com/sloveling/p/poi.html
Copyright © 2011-2022 走看看