导出
public void importTemplateExcel(HttpServletResponse response, String sheetName, String title)
{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
this.init(null, sheetName, title, Type.IMPORT);
exportExcel(response);
}
public void init(List<T> list, String sheetName, String title, Type type)
{
if (list == null)
{
list = new ArrayList<T>();
}
this.list = list;
this.sheetName = sheetName;
this.type = type;
this.title = title;
createExcelField();
createWorkbook();
createTitle();
createSubHead();
}
private void createExcelField()
{
this.fields = getFields();
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
this.maxHeight = getRowHeight();
}
public void createWorkbook()
{
this.wb = new SXSSFWorkbook(500);
this.sheet = wb.createSheet();
wb.setSheetName(0, sheetName);
this.styles = createStyles(wb);
}
private Map<String, CellStyle> createStyles(Workbook wb)
{
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font titleFont = wb.createFont();
titleFont.setFontName("Arial");
titleFont.setFontHeightInPoints((short) 16);
titleFont.setBold(true);
style.setFont(titleFont);
DataFormat dataFormat = wb.createDataFormat();
style.setDataFormat(dataFormat.getFormat("@"));
styles.put("title", style);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font totalFont = wb.createFont();
totalFont.setFontName("Arial");
totalFont.setFontHeightInPoints((short) 10);
style.setFont(totalFont);
styles.put("total", style);
styles.putAll(annotationHeaderStyles(wb, styles));
styles.putAll(annotationDataStyles(wb));
return styles;
}
- 想要该表头,在createExcelField方法中该,
- 所有的表格样式都在createStyles方法中
- 想要自定义属性表格中下拉框列表,使用反射获取到该属性的Field对象,然后使用反射获取该属性的Excel注解,获取Excel注解的memberValues属性的Field对象,hField使用get方法获取所有注解Map,然后动态给map添加数据
举例:
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response)
throws IOException, NoSuchFieldException, IllegalAccessException {
List<String> importFields = Arrays.asList("memberId", "name",
"belongBranchName","idNumber","sex","educationLevel","politicalStatus","nationality","regularStatus","joinTime","phone","mail");
Field field = PartyMember.class.getDeclaredField("belongBranchName");
Excel foo = field.getAnnotation(Excel.class);
InvocationHandler h = Proxy.getInvocationHandler(foo);
Field hField = h.getClass().getDeclaredField("memberValues");
hField.setAccessible(true);
Map memberValues = (Map)hField.get(h);
PartyBranch query = new PartyBranch();
query.setDelFlag(0);
List<PartyBranch> list = partyBranchService.selectPartyBranchList(query);
if(CollectionUtils.isNotEmpty(list)) {
String[] strs1 = list.stream().map(PartyBranch::getBranchName).toArray(String[]::new);
memberValues.put("combo",strs1);
}
ExcelUtil<PartyMember> util = new ExcelUtil<>(PartyMember.class);
util.importTemplateExcel(response, "党员数据", null,importFields);
}