springboot + poi word文档导出,Excel文件读取

第一步:加入poi相关的依赖

    compile('org.apache.poi:poi:3.9')
    compile('org.apache.poi:poi-scratchpad:3.9')
    compile('org.apache.poi:poi-ooxml:3.9')  //当excel文档是2007版的需导入此依赖,使用XSSF...

第二步:在resources目录下创建xlsx(xls)和word文件
word模板文件如下

这里写图片描述

第三步:上代码

package com.hj;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.ResourceUtils;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

@SpringBootApplication
@EnableSwagger2
public class TestApplication {

    private static String fileName="web-info1.xls";
    public static void main(String[] args) throws Exception {
        SpringApplication.run(TestApplication.class, args);
try {
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:"+fileName)));
    //Excel工作表
    HSSFSheet sheet = wb.getSheetAt(0);

    //表头那一行
    HSSFRow titleRow = sheet.getRow(0);

    //表头那个单元格
    HSSFCell titleCell = titleRow.getCell(0);

    String title = titleCell.getStringCellValue();

    System.out.println("标题是:" + title);
}catch (Exception e){
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:web-info1.xls")));
    //Excel工作表
    XSSFSheet sheet = wb.getSheetAt(0);

    //表头那一行
    XSSFRow titleRow = sheet.getRow(0);

    //表头那个单元格
    XSSFCell titleCell = titleRow.getCell(0);

    String title = titleCell.getStringCellValue();

    System.out.println("标题是:"+title);
}

    }
}
package com.hj.controller;

import com.hj.utils.POIWord;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huangjie on 2017/11/1.
 */
@RestController
public class POIWordController {
    @GetMapping("/outword")
    public String outWord() throws Exception {

        String tmpFile = "E:/temp/template.doc";
        String expFile = "E:/temp/result.doc";
        Map<String, String> datas = new HashMap<String, String>();
        datas.put("title", "标题部份test1");
        datas.put("content", "这里是内容,测试使用POI导出到Word的内容!test1");
        datas.put("author", "hjj");
        datas.put("url", "http://www.zslin.com");
        File file= ResourceUtils.getFile("classpath:template.doc");
        POIWord.build(file, datas, expFile);
        return "word";
    }
}
package com.hj.controller;

import com.hj.utils.POIWord;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huangjie on 2017/11/1.
 */
@RestController
public class POIWordController {
    @GetMapping("/outword")
    public String outWord() throws Exception {

        String tmpFile = "E:/temp/template.doc";
        String expFile = "E:/temp/result.doc";
        Map<String, String> datas = new HashMap<String, String>();
        datas.put("title", "标题部份test1");
        datas.put("content", "这里是内容,测试使用POI导出到Word的内容!test1");
        datas.put("author", "hjj");
        datas.put("url", "http://www.zslin.com");
        File file= ResourceUtils.getFile("classpath:template.doc");
        POIWord.build(file, datas, expFile);
        return "word";
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值