您的位置:首页 > 编程语言 > Java开发

springboot整合poi,excel导出

2019-08-21 19:46 956 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/G_whang/article/details/99989657

springboot导出excel首先添加依赖

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

然后是工具类,用于创建返回的视图

import com.example.springboot06.com.hx.pojo.ExcelSheetSettingEnum;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Excel视图
*
* 支持多个Sheet, Sheet名称、标题和表头不是必须的
* AbstractPdfView和AbstractXlsxView原理大致相同
* @Author: Administrator
* @Description:
* @Date: 2019-07-29 14:06
* @Modified By:
*
*/
public class ExcelView extends AbstractXlsxView {

@Override
protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
ExcelSheetSettingEnum setting = (ExcelSheetSettingEnum) map.get("ExcelSheetSetting");
// 设置文件名称
String filename = setting.getFilename();
filename = new String(filename.getBytes("UTF-8"),"ISO8859-1");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=" + filename + ".xlsx");

List<List<List<String>>> sheets = (List<List<List<String>>>) map.get("data");
for (int i = 0; i < sheets.size(); i++) {
// 创建sheet
String[] sheetNames = setting.getSheetnames();
String sheetName = "Sheet" + (i + 1);
if (sheetNames != null && sheetNames.length > 0) {
sheetName = sheetNames[i];
}
Sheet sheet = workbook.createSheet(sheetName);

// 如果标题不为空的话,将表格的第一行作为标题行,并合并第一行的N个单元格
int index = 0;
String[] titles = setting.getTitles();
String[][] headerss = setting.getHeaders();
List<List<String>> rowsForTable = sheets.get(i);
if (titles != null && titles.length > 0) {
// 合并标题单元格 下标从0开始 起始行号,终止行号, 起始列号,终止列号
CellRangeAddress region = new CellRangeAddress(0, 0, 0, rowsForTable.get(0).size() - 1);
sheet.addMergedRegion(region);

Row titleRow = sheet.createRow(index++);
Cell titleCell = titleRow.createCell(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Font font = workbook.createFont();
font.setFontName("黑体");
font.setBold(true);
font.setFontHeightInPoints((short) 15);
cellStyle.setFont(font);
titleCell.setCellStyle(cellStyle);
titleCell.setCellValue(titles[i]);
}

// 创建表头行
if (headerss != null && headerss.length > 0) {
Row headerRow = sheet.createRow(index++);
String[] headers = headerss[i];
for(int j = 0; j < headers.length; j++) {
headerRow.createCell(j).setCellValue(headers[j]);
}
}

// 创建数据行
AtomicInteger rowIndex = new AtomicInteger(index);
rowsForTable.forEach(rowList -> {
Row row = sheet.createRow(rowIndex.getAndIncrement());
AtomicInteger x = new AtomicInteger();
rowList.forEach(cell ->
row.createCell(x.getAndIncrement()).setCellValue(cell)
);
});
}
}
}

然后是枚举类

/**
* @Author: Administrator
* @Description:
* @Date: 2019-07-29 14:03
* @Modified By:
*/
public enum ExcelSheetSettingEnum {
REPORT_TEST("report_test", "Excel文件名称", new String[]{"Sheet名称1", "Sheet名称2"}, new String[]{"标题1", "标题2"}, new String[][]{
{"字段名称A", "字段名称B", "字段名称C", "字段名称D"},
{"字段名称A", "字段名称B", "字段名称C", "字段名称D"}
}),
REPORT_TEST2("report_test2", "Excel文件名称", new String[]{"标题1", "标题2"}),
REPORT_TEST3("report_test3", "Excel文件名称")
;

ExcelSheetSettingEnum(String code, String filename) {
this.code = code;
this.filename = filename;
}

ExcelSheetSettingEnum(String code, String filename, String[] titles) {
this.code = code;
this.filename = filename;
this.titles = titles;
}

ExcelSheetSettingEnum(String code, String filename, String[] sheetnames, String[] titles, String[][] headers) {
this.code = code;
this.filename = filename;
this.sheetnames = sheetnames;
this.titles = titles;
this.headers = headers;
}

/** 代码标识(必选) */
private String code;

/** 代码标识(必选) */
private String filename;

/** Sheet名称(可选) */
private String[] sheetnames;

/** Sheet标题(可选) */
private String[] titles;

/** 表头名称(可选) */
private String[][] headers;

// Getter & Setter

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String[] getSheetnames() {
return sheetnames;
}

public void setSheetnames(String[] sheetnames) {
this.sheetnames = sheetnames;
}

public String[] getTitles() {
return titles;
}

public void setTitles(String[] titles) {
this.titles = titles;
}

public String[][] getHeaders() {
return headers;
}

public void setHeaders(String[][] headers) {
this.headers = headers;
}
}

然后是接口访问

import com.example.springboot06.com.hx.pojo.ExcelSheetSettingEnum;
import com.example.springboot06.com.hx.util.ExcelView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author: Administrator
* @Description:
* @Date: 2019-07-29 14:10
* @Modified By:
*/
@RestController
@RequestMapping("/excel")
public class ExcelController {

@RequestMapping("/export")
public ModelAndView export(){

List<List<String>> sheet1 = Arrays.asList(
Arrays.asList("1", "11", "111", "1111"),
Arrays.asList("2", "22", "222", "2222"),
Arrays.asList("3", "33", "333", "3333")
);

List<List<String>> sheet2 = Arrays.asList(
Arrays.asList("4", "44", "444", "4444"),
Arrays.asList("5", "55", "555", "5555"),
Arrays.asList("6", "66", "666", "6666")
);
List<List<List<String>>> sheets = Arrays.asList(sheet1, sheet2);

Map<String, Object> map = new HashMap<>();
map.put("ExcelSheetSetting", ExcelSheetSettingEnum.REPORT_TEST2);
map.put("data", sheets);

ExcelView excelView = new ExcelView();
return new ModelAndView(excelView, map);
}
}

执行导出成功

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: