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模板文件如下

springboot + poi word文档导出,Excel文件读取_第1张图片

第三步:上代码

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 datas = new HashMap();
        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 datas = new HashMap();
        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";
    }
}

你可能感兴趣的:(spring,boot)