java: springboot+elementui导出功能模板
前端代码:
1.js公共方法
exportExcel: function (url,ids) {
var form = document.createElement('form')
form.style.display = 'none'
form.action =项目路径+url;
form.method='post'
document.body.appendChild(form)
var input= document.createElement('input');
input.type='hidden';
input.name='ids';
input.value=ids;
form.appendChild(input);
form.submit()
form.remove()
},
2.调用
<script>
import commonJs from "@/pages/opinion/utils/common.js"
//方法
methods: {
exportTestPoint: function () {
let _this = this;
let message = "确定导出所选数据?";
if (_this.selectIds.length == 0) {
return this.$message.warning("请选择需要导出的数据!");
}
//提示框
this.$confirm(message, "提示", {
confirmButtonText: "确认",
cancelButtonText: "关闭",
type: "warning",
}).then(() => {
//列表所选的id
let ids = _this.selectIds.join(",");
commonJs.exportExcel("/api/testPoint/export", ids);
});
},
}
</script>
后端代码
0.导出工具类
public class ExportExcelUtil {
public static ResponseEntity<byte[]> export(String fileName, String[] titles, List<Map<String, Object>> dataList) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
workbook.createInformationProperties();
DocumentSummaryInformation info = workbook.getDocumentSummaryInformation();
info.setCategory(fileName);
HSSFCellStyle datecellStyle = workbook.createCellStyle();
datecellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
HSSFSheet sheet = workbook.createSheet();
HSSFRow r0 = sheet.createRow(0);
int columnNum = titles.length;
HSSFCellStyle headStyle = getHeaderStyle(workbook);
for (int i = 0; i < columnNum; i++) {
HSSFCell cell = r0.createCell(i);
cell.setCellValue(titles[i]);
cell.setCellStyle(headStyle);
}
for (int i = 0; i < dataList.size(); i++) {
Map<String, Object> map = dataList.get(i);
HSSFRow row = sheet.createRow(i + 1);
for (int j = 0; j < columnNum; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(map.get(titles[j]) == null ? "" : map.get(titles[j]).toString());
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment",
new String((fileName+".xls").getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
ResponseEntity<byte[]> entity = new ResponseEntity<>(baos.toByteArray(), headers, HttpStatus.CREATED);
baos.close();
return entity;
}
private static HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setColor(IndexedColors.AUTOMATIC.getIndex());
font.setBold(true);
font.setFontName("黑体");
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.AUTOMATIC.getIndex());
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.AUTOMATIC.getIndex());
style.setTopBorderColor(IndexedColors.AUTOMATIC.getIndex());
style.setFont(font);
style.setWrapText(false);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
return style;
}
}
1.controller
@PostMapping("/export")
@ResponseBody
public ResponseEntity<byte[]> exportExcel(HttpServletResponse response, @RequestParam String ids) throws IOException {
String[] idsArray = ids.split(",");
List<String> idList = Arrays.asList(idsArray);
List<TestPointBase> dataList = this.service.findAllByIds(idList);
String title[] = {"编号", "名称", "专业代号", "专业名称", "动作代号", "动作名称", "试验目的","试验方法"};
return ExportExcelUtil.export("表名", title, this.findExportDataMapFromList(dataList));
}
public List<Map<String, Object>> findExportDataMapFromList(List<TestPointBase> parametersList) {
List<Map<String, Object>> exportList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (null != parametersList && parametersList.size() > 0) {
for (TestPointBase data : parametersList) {
Map<String, Object> map = new HashedMap();
String title[] = {"", "", "", "", "", "", "", "", "","", "",""};
map.put("编号", data.getNumber());
map.put("名称", data.getName());
map.put("专业代号", data.getFirstSpecialtyNumber());
map.put("专业名称", data.getFirstSpecialtyName());
map.put("动作代号", data.getActionNumber());
map.put("动作名称", data.getActionName());
map.put("试验目的", data.getPurpose());
map.put("试验方法", data.getResult());
exportList.add(map);
}
}
return exportList;
}