导出
1.编写Excel 工具类
public static void writeExcel(HttpServletResponse response, List<List<String>> head, List<List<String>> list, String fileName, String sheetName)
throws IOException {
String filePath = fileName + ".xlsx";
File dbfFile = new File(filePath);
if (!dbfFile.exists() || dbfFile.isDirectory()) {
dbfFile.createNewFile();
}
fileName = new String(filePath.getBytes(), "ISO-8859-1");
OutputStream out = response.getOutputStream();
try {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet = new Sheet(0, 1);
sheet.setStartRow(0);
sheet.setSheetName(sheetName);
sheet.setHead(head);
writer.write0(list, sheet);
writer.finish();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.编写导出业务
@RequestMapping("/export")
public void exportCash(TaskOrderQuery taskOrderQuery, @LoginUser UserInfo userInfo, HttpServletResponse responses) throws Exception {
responses.setContentType("application/vnd.ms-excel;charset=utf-8");
responses.setHeader("Content-Disposition", "attachment;filename=taskOrder.xlsx");
OutputStream out = null;
try {
out = responses.getOutputStream();
List<List<String>> datas = Lists.newArrayList();
List<List<String>> heads = Lists.newArrayList(Lists.newArrayList("列一"), Lists.newArrayList("列二"), Lists.newArrayList("列三"));
BaseResponseVo<List<TaskOrderSpec>> taskOrder = 请求接口拿到需要导出的数据;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
taskOrder.getData().forEach(t -> {
List<String> list = Lists.newArrayList(列一值, 列二值,列三值);
datas.add(list);
});
EasyExcelUtils.writeExcel(responses, heads, datas, "TaskOrder", "sheet1");
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
2.导入
1.编写导入工具类
public static List<Object> readExcel(InputStream inputStream) {
return readExcel((InputStream) inputStream, 1);
}
public static List<Object> readExcel(InputStream inputStream, int sheetNo) {
if (inputStream == null) {
return null;
} else {
List data = EasyExcelFactory.read(inputStream, new Sheet(sheetNo, 0));
try {
inputStream.close();
} catch (IOException var4) {
var4.printStackTrace();
}
return data;
}
}
public static List<Object> readExcel(MultipartFile excel, BaseRowModel object) {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
for (Sheet sheet : reader.getSheets()) {
if (object != null) {
sheet.setClazz(object.getClass());
}
reader.read(sheet);
}
return excelListener.getDatas();
}
public static List<Object> readExcel(MultipartFile excel, BaseRowModel object, int sheetNo) {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
Sheet sheet = new Sheet(sheetNo);
sheet.setClazz(object.getClass());
reader.read(sheet);
return excelListener.getDatas();
}
2.编写导入业务
@ResponseBody
@RequestMapping(value = "/read", method = RequestMethod.POST, consumes = "multipart/form-data")
@IgnoreLogin
public BaseResponseVo<List<List<String>>> readPayProductExcel(@RequestParam MultipartFile file) throws Exception {
List<List<String>> data = null;
ImportCreditVo importCreditVo = new ImportCreditVo(null, 0, 0);
try {
data = (List) EasyExcelUtils.readExcel(file.getInputStream());
data.remove(0);
if (null == data || data.size() < 1) {
return BaseResponseVo.err("1", "导入的文件没有数据", null);
}
for (List<String> pa : data) {
if (pa == null || pa != null && pa.size() < 3) {
return BaseResponseVo.err("1", "导入数据为空,请检查后导入", null);
}
}
importCreditVo =
String rechargeLog = null;
if (importCreditVo != null && importCreditVo.getErr() != 0) {
rechargeLog = MessageFormat.format("导入完成,成功导入\"{0}\"条,失败\"{1}\"条;以下账号失败:\"{2}\";请检查失败数据是否正确并重新导入失败数据", importCreditVo.getSuccess(),
importCreditVo.getErr(), importCreditVo.getFailList());
log.info(rechargeLog);
return BaseResponseVo.err("1", rechargeLog, importCreditVo.getFailList());
}
rechargeLog = MessageFormat.format("导入完成,成功导入\"{0}\"条,失败\"{1}\"条", importCreditVo.getSuccess(), importCreditVo.getErr());
log.info(rechargeLog);
return BaseResponseVo.succ("0", rechargeLog, null);
} catch (IOException e) {
return BaseResponseVo.err("1", "上传错误");
}
}
3.导入业务逻辑 (具体看自己)
public ImportCreditVo read(List<List<String>> accountCredit) throws Exception {
List<List<String>> failList = new ArrayList<>();
int success = 0;
int err = 0;
for (List<String> pa : accountCredit) {
try {
AccountCreditService beanByClass = ApplicationContextUtil.getBeanByClass(AccountCreditService.class);
beanByClass.addAccountCredit(参数);
} catch (Exception e) {
failList.add(pa);
err += 1;
e.printStackTrace();
}
}
success = accountCredit.size() - err;
return new ImportCreditVo(failList, success, err);
}
处理回滚业务
@Transactional(rollbackFor = Exception.class)
public void addAccountCredit(List<String> pa, LabelVo labelVo, CreditQo creditQo, List<List<String>> failList, int err) throws Exception {
}