首页 国际新闻正文

挣钱,作业必会技术 | Spring Boot 导出Excel,弹弹堂

本号首要用于共享企业中常用的技能,愈加侧重于有用,欢迎重视,便于阅读其它更多有用的历史文章。

工作中常常遇到导出Ecxel功用,这儿就给出完好示例代码,能够直接运用。

1.引进依靠


org.apache.poi
poi-ooxml
3.17

2. ExcelSheetSettingEnum

界说Excel的一些装备信息

public enum ExcelSheetSettingEnum {
REPORT_TEST("report_test", "Excel文件称号", new String[]{"Sheet称号1", "Sheet称号2"}, ne囚情索爱w 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[] sheetnam赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂es, String[] 包晓琳titl高仁彬es, String[][] headers) {
this.code = code;
this.filename = filename;
this.sheetnames = sheetnames;
this.titles = titles;
this.headers = headers;
}
/** 代码标识(必选) */
p古代家法rivate String code;
/** 代码标识(必选) */
private String filename;
/** S赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂heet称号(可选) */
private String[] sheetnames;
/** Sheet标题(可选) */
private String[] titles;
/** 表头称号(可选) */
private String[][] headers;
// Getter & Se赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂tter
}

3. ExcelView

界说Excel视图,承继自Abstrac黄衍仁tXlsxView或许AbstractXlsView, 需求完成一个abstract办法buildExcelDocument用于创立Sheet,结构Excel数据。

承继联系如下:

/**
* Excel视图
*
* 支撑多个Sheet, Sheet称号、标题和表头不是有必要的
* AbstractPdfView和Abstr明日南京气候actXlsxView原理大致相同
*
* @author mengday zhang
*/
public class ExcelView extends AbstractXlsxView {
@Override
protected void buildExcelDocument(Map map, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
ExcelSheetSettingEnum setting = (ExcelSheetSettingEnum) map.get("ExcelSheetSettin赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂g");
// 设置文件称号
String filename = setting.getFilename();
filename = new String(filename.getBytes("UTF-8"),"ISO8859-1");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=" + filename + ".xlsx");
List>> sheets = (List>>) 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);
// 假如标题不为空超高档的话,将表格的第赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂一行作为标题行,并兼并榜首行的N个单元格
int index = 0;
String[] titles = setting.getTitles();
String[][] headerss = setting.getHeaders();
List> rowsForTable = sheets.get(i);
if (titles != null && titles.length > 0) {
// 兼并标题单元格 下标从0开端 开始行号,停止行号, 开始列号,停止列号
CellRangeAddress region = new CellRangeA赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂ddress(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.len色夜gth > 0) {
Row h90010兔子eaderRow = 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)
);
});
}
}
}

4.controller

mvc办法中办法的返回值是ModelAndView, 当代码履行ne通职者第二季w ModelAndView(excelView, map)时会履行ExcelView#buil志广世纪集团dExcelDocument的办法

@RestController
@RequestM赵映环apping("/excel")
public class ExcelController {
@RequestMapping("/export")
public ModelAndView 我国洋媳妇村export(){
List> sheet1 = Arrays.asList(
Arrays.asList("1", "11", "111", "1111"),
Arrays.asList("2", "22", "222", "2222"),
Arrays.asList("3", "33", "333", "3333")
);
List> sheet2 = Arrays.asList(
Arrays.asList("4", "44", "444", "4444"),
Arrays.asList("5", "55", "555", "5555"),
Arrays.asList("6", "66", "666", "6666")
);
List>> sheets = Arrays.asList(sheet1, sheet2);
Map map = new HashMap<>();
map.put("ExcelSheetSettin赚钱,作业必会技能 | Spring Boot 导出Excel,弹弹堂g", ExcelSheetSettingEnum.REPORT_TEST2);
map.put("data", sheets);
ExcelView excelView = ne鄢陵邢莹莹w ExcelView();
return new ModelAndView(excelView, map);
}
}

本号首要用于共享企业中常用的技能,愈加侧重于有用,欢迎重视,便于阅读其它更多有用的历史文章。

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

afraid,无锡车联网演示小镇 一期出资20亿元,茶具

  • 平方米,华峰超纤11月6日快速反弹,混沌天体