码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot & Mybatis 构建restful 服务四

时间:2017-03-07 17:01:09      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:文件名   cell   service   operation   nts   复制   count   catch   log   

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/ejtone/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/pengsijia/Documents/pro/xm/AccountBalance
mvn clean package
cd target
mkdir /Users/pengsijia/Desktop/t/
#将 tar 包复制到自己指定目录(/Users/pengsijia/Desktop/t/)
cp AccountBalance-0.0.1-SNAPSHOT.tar /Users/pengsijia/Desktop/t/
#cd 到上个操作指定的目录
cd /Users/pengsijia/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 结束服务
 

 

 

 

Springboot & Mybatis 构建restful 服务四

标签:文件名   cell   service   operation   nts   复制   count   catch   log   

原文地址:http://www.cnblogs.com/serena25/p/6515415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!