java读取Excel文件写入到elasticsearch

测试用例用了springboot 框架了spring-data-elasticsearch框架。有感兴趣的可以写一下

实体类

@JsonIgnoreProperties(ignoreUnknown = true)
@Document(indexName = “test-smq”, type = “test”, refreshInterval = “1s”, createIndex = false)
public class Labmaster {
/**
* 主键
*/
@Id
@JsonProperty(“id”)
private String id;

/**
 * 检验描述
 */
@JsonProperty("detail")
private String detail;

/**
 * 检验名字
 */
@JsonProperty("name")
private String name;

/**
 * 检验编码
 */
@JsonProperty("code")
private String code;

/**
 * 检验版本
 */
@JsonProperty("version")
private String version;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getDetail() {
    return detail;
}

public void setDetail(String detail) {
    this.detail = detail;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getVersion() {
    return version;
}

public void setVersion(String version) {
    this.version = version;
}

}

//dao层interface
@Repository
public interface LabmasterRepository extends ElasticsearchRepository {
}

//service层
@Service
public class LabmasterService {
@Autowired
LabmasterRepository repository;

public void importLabmaster() throws Exception {
    FileInputStream fis = null;
    Sheet sheet = null;
    List saveList = new ArrayList<>();
    try {
        //获取一个绝对地址的流
        fis = new FileInputStream("C:\\Users\\宋明启\\Desktop\\知识库\\医疗机构临床检验项目目录.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        // 获取每个Sheet表
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            sheet = workbook.getSheetAt(i);
            // getLastRowNum,获取最后一行的行标
            for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
                Row row = sheet.getRow(j);
                if (row != null) {
                    //赋值
                String detail = row.getCell(0).getStringCellValue().trim();
                String name = row.getCell(1).getStringCellValue().trim();
                String code = row.getCell(2).getStringCellValue().trim();
                String version = row.getCell(3).getStringCellValue().trim();

                Labmaster labmaster =  new Labmaster();
                   //id随机分配。
                labmaster.setId(UUID.randomUUID().toString());
                labmaster.setDetail(detail);
                labmaster.setName(name);
                labmaster.setCode(code);
                labmaster.setVersion(version);
                saveList.add(labmaster);
                }
            }
        }
        repository.saveAll(saveList);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

//controller层
@RestController
@RequestMapping("/labmaster")
public class LabmasterController extends BaseController{
@Autowired
LabmasterService labmasterService;

@RequestMapping("/importLabmaster")
@ResponseBody
@APILogicPart("上传Excel到elasticsearch上")
public void importDiagn(String allowKey)throws Exception{
    labmasterService.importLabmaster();
}

}

文件格式:
java读取Excel文件写入到elasticsearch_第1张图片

你可能感兴趣的:(elasticsearch,Java,Java,springboot)