异常:java.util.LinkedHashMap无法转换为com.excel.entity.ClassA

时间:2019-04-09 10:52:29

标签: mysql excel hibernate

  

这是我接受以下列表的通用方法   对象并向下投射到特定对象

public Response generate(List<?> list){


        List<ClassA> List1 = new ArrayList<ClassA>();
        List<ClassB> List2 = new ArrayList<ClassB>();
        List<ClassC> List3 = new ArrayList<ClassC>();



        if(list instanceof List<?>){

            List1=(List<ClassA>) list;//in this line i am getting error
            addDataToExcel(List1);
        }
        else if(list instanceof List<?>){
            List2=(List<ClassB>) list;
            addDataToExcel(List1);
        }
        else if(list instanceof List<?>){
            List3=(List<ClassC>) list;
            addDataToExcel(List1);
        }
  

这是classA

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="ClassA")
public class ClassA  {
     @Id
    @Column(name="rollNo")
    private int rollNo;

    @Column(name="name")
    private String name;

    @Column(name="english")
    private double english;

    @Column(name="maths")
    private double maths;

    @Column(name="science")
    private double science;

    @Column(name="totalMarks")
    private double totalMarks;

    @Column(name="percentage")
    private double percentage;

    @Column(name="status")
    private boolean status;

    @Lob
    @Column(name="file", columnDefinition="BLOB")
    private byte[] file;



    public ClassA() {
        // TODO Auto-generated constructor stub
    }



    public ClassA(int rollNo, String name, double english, double maths, double science, double totalMarks,
            double percentage, boolean status, byte[] file) {
        super();
        this.rollNo = rollNo;
        this.name = name;
        this.english = english;
        this.maths = maths;
        this.science = science;
        this.totalMarks = totalMarks;
        this.percentage = percentage;
        this.status = status;
        this.file = file;
    }



    public int getRollNo() {
        return rollNo;
    }



    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }



    public String getName() {
        return name;
    }



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



    public double getEnglish() {
        return english;
    }



    public void setEnglish(double english) {
        this.english = english;
    }



    public double getMaths() {
        return maths;
    }



    public void setMaths(double maths) {
        this.maths = maths;
    }



    public double getScience() {
        return science;
    }



    public void setScience(double science) {
        this.science = science;
    }



    public double getTotalMarks() {
        return totalMarks;
    }



    public void setTotalMarks(double totalMarks) {
        this.totalMarks = totalMarks;
    }



    public double getPercentage() {
        return percentage;
    }



    public void setPercentage(double percentage) {
        this.percentage = percentage;
    }



    public boolean isStatus() {
        return status;
    }



    public void setStatus(boolean status) {
        this.status = status;
    }



    public byte[] getFile() {
        return file;
    }



    public void setFile(byte[] file) {
        this.file = file;
    }   
}
  

这是接受List并生成excel的方法

public void add(List<ClassA> classA) {
    System.out.println("entering add");
    String excelFilePath = "D:/eclipse_neon/StudentInfo.xlsx";

    try {
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
        Workbook workbook = WorkbookFactory.create(inputStream);

        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = 1;
        int a=2;
        for(ClassA info: classA){
            System.out.println("netering loop");
             Row row = sheet.getRow(rowNum++);
             row.createCell(2).setCellValue(info.getEnglish());
                row.createCell(3).setCellValue(info.getMaths());
                row.createCell(4).setCellValue(info.getScience());
                row.createCell(5).setCellFormula("SUM(C"+a+","+"D"+a+","+"E"+a+")");
                row.createCell(6).setCellFormula("("+"F"+a+"*"+"100"+")"+"/"+"300");
                row.createCell(7).setCellValue(info.isStatus());
                a++;
        }

System.out.println(“循环后”);             inputStream.close();

        FileOutputStream outputStream = new FileOutputStream("D:/eclipse_neon/StudentInfo.xlsx");
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();

    } catch (IOException | EncryptedDocumentException
            | InvalidFormatException ex) {
        ex.printStackTrace();
    }
}
  

我无法从通用列表中向下转换至我的特定课程列表,因此欢迎您提出任何建议,谢谢

1 个答案:

答案 0 :(得分:0)

这可能是因为您要发送包含作为List的参数的列表,并且类A与LinkedHashMap不相关(例如,不是子类),因此无法进行转换。如果您可以提供更多详细信息,则什么是A类,什么是作为参数发送给方法的列表。

选中https://www.baeldung.com/java-type-casting