zoukankan      html  css  js  c++  java
  • Springboot & Mybatis 构建restful 服务四

    Springboot & Mybatis 构建restful 服务四

    1 前置条件

    • 成功执行完Springboot & Mybatis 构建restful 服务三

    2 restful service 添加 Apache POI生成 Excel 文件

    1)修改 POM.xml文件

    添加 Apache POI 的依赖

            <dependency>
                 <groupId>org.apache.poi</groupId>  
                 <artifactId>poi-ooxml</artifactId>  
                 <version>3.5-FINAL</version>  
            </dependency>
     

    2)在SY里添加创建 excel 文件的方法

    ​ src/main/java/com/serena/controller/SY.java

     
        @ApiOperation(value="查询所有账户余额等信息,并导出在 excel 表格中")
        @RequestMapping(value="/accountsfile",method = RequestMethod.GET)
        public boolean wSelectAll(HttpServletResponse response){
            logg.info("write a file that select accounts "); 
            List<SettleAccount> list = null;
            boolean flag = false;
            // 获取所有账户信息
            list = iSY.selectAccounts();
            // 判断是否存在账户
            if(list == null)
                logg.warn("not found accounts");
            else{
                // 创建表对象
                HSSFWorkbook workBook = new HSSFWorkbook();
                ServletOutputStream fileOut = null;
                try {
                    // 获取输出流对象
                    fileOut = response.getOutputStream();
                    // 创建 sheet
                    HSSFSheet sheet = workBook.createSheet();
                    // 设置表格第一行的字段名
                    HSSFRow row = sheet.createRow(0);
                    int i =1;
                    row.createCell(0).setCellValue("账户编号");
                    row.createCell(1).setCellValue("账户名");
                    row.createCell(2).setCellValue("客户编号");
                    row.createCell(3).setCellValue("短信余额");
                    for (SettleAccount acc : list) {
                        HSSFRow rowi = sheet.createRow(i++);
                        rowi.createCell(0).setCellValue(acc.getAccountcode());
                        rowi.createCell(1).setCellValue(acc.getAccountname());
                        rowi.createCell(2).setCellValue(acc.getCustomercode());
                        rowi.createCell(3).setCellValue(acc.getSmsnum().doubleValue());
                    }
                    workBook.write(fileOut);
                    return true;
                } catch (FileNotFoundException e) {
                    logg.error("create table error ---- " + e.getMessage());
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    logg.error("create table error ---- " + e.getMessage());
                    e.printStackTrace();
                    return false;
                } finally {
                    try {
                        fileOut.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return flag;
        }

    5)在终端输入如下测试指令:

    #cd 项目所在目录
    cd /Users/psj/Documents/pro/xm/AccountBalance
    mvn clean package
    cd target
    mkdir /Users/psj/Desktop/t/
    #将 tar 包复制到自己指定目录(/Users/psj/Desktop/t/)
    cp AccountBalance-0.0.1-SNAPSHOT.tar /Users/psj/Desktop/t/
    #cd 到上个操作指定的目录
    cd /Users/psj/Desktop/t
    #解压 tar 包
    tar -xvf AccountBalance-0.0.1-SNAPSHOT.tar
    #此时可查看目录结构如要求所示
    ll
    #运行 可执行jar,测试结果
    java -jar AccountBalance-0.0.1-SNAPSHOT.jar
    #
    #打开浏览器
    http://localhost:8999/accountsfile
    #在下载对话框中指定文件名和保存路径
    #
    #返回上个 iterm 窗口,control+c 结束服务
     

     

  • 相关阅读:
    wiki iso88591字符表的解释
    [c]字符1一维数组求长度
    vim 用户配置
    PHP中向浏览器输出图片
    如何及时取消 BackgroundWorker 组件的后台工作
    python basic
    php5.1中的时区设置。
    MyBatis的深入原理分析之1架构设计以及实例分析
    hibernate缓存:一级缓存和二级缓存
    Spring 注解(Annotation)代替XML实现零配置
  • 原文地址:https://www.cnblogs.com/serena25/p/6515415.html
Copyright © 2011-2022 走看看