直接複製不一定能用
@Getter
@Setter
public class Material{
@ExcelIgnore
private Long id;
/** 所屬部門 */
@ExcelProperty(value = {"一級合併標題","所屬部門"})
private String department;
/** 所屬部室/專案部/站區 */
@ExcelProperty(value = {"一級合併標題","所屬部室/專案部/站區"})
private String area;
}
AnalysisEventListener<Object>
類實現介面。public class MaterialListener extends AnalysisEventListener<Material> {
// 反序列化注入bean工廠
ISysDictDataService dictDataService = SpringUtils.getBean(ISysDictDataService.class);
//可以通過範例獲取該值
private List<Material> datas = new ArrayList<>();
//錯誤資訊
private List<String> errorInfo = new ArrayList<>();
@Override
public void invoke(Material material, AnalysisContext analysisContext) {
doSomething(material,analysisContext);//根據自己業務做處理
datas.add(material);//資料儲存到list,供批次處理,或後續自己業務邏輯處理。
}
private void doSomething(Material object, AnalysisContext analysisContext) {
//獲取當前行數
Integer row = analysisContext.readRowHolder().getRowIndex();
SysDictData sysDictData=new SysDictData();
if (ObjectUtils.isBlank(object.getType())){
errorInfo.add(" 第" + (row+1) + "行:物資種類不能為空,請重新填寫後再匯入!<br>");
}else {
sysDictData.setDictType("material_type");
sysDictData.setDictLabel(object.getType());
List<SysDictData> list3= dictDataService.selectDictDataList(sysDictData);
if (ObjectUtils.isBlank(list3)){
errorInfo.add(" 第" + (row+1) + "行:物資種類:"+object.getType()+"未在資料字典中找到,請先填寫後再匯入!<br>");
}
}
}
//返回結果集物件
public List<Material> getDatas() {
return datas;
}
public void setDatas(List<Material> datas) {
this.datas = datas;
}
//返回錯誤訊息
public List<String> getErrorInfo() {
return errorInfo;
}
public void setErrorInfo(List<String> errorInfo) {
this.errorInfo = errorInfo;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
materialService.saveList(materialList)
中進行處理匯入獲取過來的資料,用於存取業務資料 @PostMapping("/importData")
@ResponseBody
public AjaxResult importData(@RequestParam MultipartFile file) throws Exception
{
MaterialListener excelListener = new MaterialListener();
// 這裡 需要指定讀用哪個class去讀,然後讀取第一個sheet 檔案流會自動關閉
//EasyExcel.read(哪個檔案).sheet(那張sheet表).head(表頭什麼樣子).headRowNumber(表頭佔幾行).registerReadListener(處理資料的監聽器類).doRead()
EasyExcel.read(file.getInputStream(), Material.class, excelListener).sheet().head(Material.class).headRowNumber(2).doRead();
if(ObjectUtils.isBlank(excelListener.getErrorInfo())){
if(ObjectUtils.isNotBlank(excelListener.getDatas())){
List<Material> materialList = excelListener.getDatas();
materialService.saveList(materialList);
}
return AjaxResult.success(1);
}else{
return AjaxResult.error("操作失敗",excelListener.getErrorInfo());
}
}
@GetMapping("/exportData")
public void exportData(Material material, HttpServletResponse response){
//換方法連表查詢
// List<Material> list = materialService.selectMaterialListByDict(material);
List<Material> materialList=new ArrayList<>();
List<Material> list = materialService.selectMaterialList(material);
String[] ids=new String[list.size()];
for (Material p:list) {
//用於修改匯出的資料
String list7= dictDataService.selectDictLabel("material_type",p.getType());
if (ObjectUtils.isNotBlank(list7)){
p.setType(list7);
}
materialList.add(p);
}
List<MaterialSolid> materialSolidList=new ArrayList<>();
List<MaterialSolid> materialSolidListS = materialSolidService.selectMaterialSolidListByIds(ids);
for (MaterialSolid solid:materialSolidListS) {
String list7= dictDataService.selectDictLabel("material_type",solid.getType());
if (ObjectUtils.isNotBlank(list7)){
solid.setType(list7);
}
materialSolidList.add(solid);
}
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 這裡URLEncoder.encode可以防止中文亂碼 當然和EASYEXCEL沒有關係
String fileName = URLEncoder.encode("物料資訊臺賬", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
//呼叫方法進行寫的操作
// EasyExcel.write(response.getOutputStream(),Material.class).sheet("物料資訊臺賬").doWrite(materialList);
//多sheet頁匯出
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "物料資訊總賬").head(Material.class).build();
WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "物料資訊詳情").head(MaterialSolid.class).build();
excelWriter.write(materialList, writeSheet1);
excelWriter.write(materialSolidList, writeSheet2);
excelWriter.finish();
} catch (Exception e) {
e.printStackTrace();
}
}