读取xls并上传表实体(apache poi)

时间:2019-10-26 18:02:30

标签: java apache-poi mapping entity

我有这两个表:

@Entity
@Table(name="Drug")
public class Drug implements Serializable {
    private static final long serialVersionUID = 1L;


    @Id
    @SequenceGenerator(name="DRUG_IDDRUG_GENERATOR", sequenceName="SEQ_DRUG", allocationSize = 1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DRUG_IDDRUG_GENERATOR")
    @Column(name="ID_DRUG", unique=true, nullable=false, precision=14)
    @NotNull
    private long idDrug;

    @Column(name="ORIGINAL_CODE", length=100)
    private String originalCode;

    @Column(name="NM_USER", length=30)
    private String nmUser;

    @Column(name="NM_USER_LAST_MODIFY", length=30)
    private String nmUserLastModify;

    @Column(name="TS_INSERT")
    private Timestamp tsInsert;

    @Column(name="TS_LAST_MODIFY")
    private Timestamp tsLastModify;

    //bi-directional many-to-one association to StateDrug
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name="ID_STATE_DRUG", nullable=false)
    @NotNull
    @OrderColumn
    private StateDrug stateDrug;

    //bi-directional many-to-one association to StateDrug
    @ManyToOne
    @JoinColumn(name="ID_NUM", nullable=false)
    @NotNull
    @OrderColumn
    @JsonManagedReference
    private StateDrug codeNum;

    //bi-directional many-to-one association to StateDrug
    @ManyToOne
    @JoinColumn(name="ID_Drug_Shape", nullable=false)
    @NotNull
    @OrderColumn
    @JsonManagedReference
    private DrugState drugShape;

    //bi-directional many-to-one association to DrugState
    @ManyToOne
    @JoinColumn(name="ID_MISURE", nullable=false)
    @NotNull
    @OrderColumn
    @JsonManagedReference
    private DrugState misure;

    //bi-directional many-to-one association to DrugState
    @ManyToOne
    @JoinColumn(name="ID_UNITY", nullable=false)
    @NotNull
    @OrderColumn
    @JsonManagedReference
    private DrugState unity;

    //bi-directional many-to-one association to DrugState
    @ManyToOne
    @JoinColumn(name="ID_ACTIVE_PRINCIPLE", nullable=false)
    @NotNull
    @OrderColumn
    @JsonManagedReference
    private DrugState activePrinciple;


    public Drug() {
    }

    public long getIdDrug() {
        return this.idDrug;
    }

    public void setIdDrug(long idDrug) {
        this.idDrug = idDrug;
    }

    public String getOriginalCode() {
        return this.originalCode;
    }

    public void setOriginalCode(String OriginalCode) {
        this.originalCode = originalCode;
    }

    public String getNmuser() {
        return this.nmUser;
    }

    public void setNmUser(String nmUser) {
        this.nmUser = nmUser;
    }

    public String getNmUserLastModify() {
        return this.nmUserLastModify;
    }

    public void setNmUserLastModify(String nmUserLastModify) {
        this.nmUserLastModify = nmUserLastModify;
    }

    public Timestamp getTsInsert() {
        return this.tsInsert;
    }

    public void setTsInsert(Timestamp tsInsert) {
        this.tsInsert = tsInsert;
    }

    public Timestamp getTsLastModify() {
        return this.tsLastModify;
    }

    public void setTsLastModify(Timestamp tsLastModify) {
        this.tsLastModify = tsLastModify;
    }

    public DrugState getDrugState() {
        return this.drugState;
    }

    public void setDrugState(DrugState drugState) {
        this.drugState = drugState;
    }

    public DrugState getcodeNum() {
        return this.codeNum;
    }

    public void setcodeNum(DrugState codeNum) {
        this.codeNum = codeNum;
    }

    public DrugState getDrugShape() {
        return this.drugShape;
    }

    public void setDrugShape(DrugState drugShape) {
        this.drugShape = drugShape;
    }

    public DrugState getMisure() {
        return this.misure;
    }

    public void setDosaggio(DrugState misure) {
        this.misure = misure;
    }

    public DrugState getUnity() {
        return this.unity;
    }

    public void setUnity(DrugState unity) {
        this.unity = unity;
    }

    public DrugState getActivePrinciple() {
        return this.activePrinciple;
    }

    public void setActivePrinciple(DrugState activePrinciple) {
        this.activePrinciple = activePrinciple;
    }

}

DrugState是另一个在某些字段上带有@OneToMany注释的实体,getter方法将返回List。

现在,我想创建一个方法: -存储用户在我的服务器上加载的xls文件(包含药品列表); -检查上载的文件xls的列数,如果发现的行多于Drug表中的行数,请通过在表中添加Drug实体来映射xls文件。

我的问题有两个: -有些Drug字段具有ManyToOne表示法,因此设置该字段并不容易; Apache Cell类只能返回Double(getNumericCellValue())或String(getStringCellValue())。

我开始编写该方法,但是我无法最终实现...

@PostMapping("/importXls")
    public void importXls (@RequestParam("pathFile") String pathFile) throws IOException {
        try ( FileInputStream file = new FileInputStream(new File("pathFile/UserFile.xls"));){
            HSSFWorkbook workbook = new HSSFWorkbook(file);

            HSSFSheet sheet = workbook.getSheetAt(0); 

            List<Drug> list = drugService.getAll();

            int totalDrugs = list.size();   
            int lastRow = sheet.getLastRowNum();

            int newDrugs = lastRow-totalDrugs;
            Drug entityToSave = new Drug();
            if (newDrugs>0) {
                Row row = sheet.getRow(totalDrugs++);
                Iterator<Cell> iterCell =row.cellIterator();
                while (iterCell.hasNext()) {
                    Cell cell = iterCell.next();
                    if (cell.getColumnIndex()==0) {
                 /* here problems start     
                  entityToSave.setOriginalCode(cell.getStringCellValue());
                    }
                    if (cell.getColumnIndex()==1) {
                        entityToSave.setCodeNum((cell.getNumericCellValue());
                    }
                    if (cell.getColumnIndex()==2) {

                entityToSave.setActivePrinciple((cell.getStringCellValue()));
                    }
                    if (cell.getColumnIndex()==3) {
                            entityToSave.setDrugShape(cell.getStringCellValue());
                    }
                    if (cell.getColumnIndex()==4) {

                    entityToSave.setMisure(cell.getStringCellValue());
                    }
                    if (cell.getColumnIndex()==5) {
                  entityToSave.setUnity(cell.getStringCellValue());
                    }
                   drugService.saveOrUpdate(entityToSave);
                   newDrugs--;
                  */
               }
           }
           FileOutputStream newFile = new FileOutputStream(new File("PATH_DB/Drugs.xls")); 
           workbook.write(newFile);
        } catch(Exception e) {
            new IOException (e.getMessage(), e);
        }
    }

如何解决此方法?

0 个答案:

没有答案