上传excel文件以在Jtable上显示后出现参数类型不匹配错误(Eclipse-JavaSwing)

上传excel文件以在Jtable上显示后出现参数类型不匹配错误(Eclipse-JavaSwing),java,excel,eclipse,swing,jtable,Java,Excel,Eclipse,Swing,Jtable,这个项目有三个.java文件。Product.java;ExcelHelper.java;jframeimpo.java 运行应用程序后,它将要求您导入excel(.xls)文件,然后打开一个选项卡,说明“参数类型不匹配” Product.java package entities; import java.util.*; public class Product { private String id; private String name; private lon

这个项目有三个.java文件。Product.java;ExcelHelper.java;jframeimpo.java 运行应用程序后,它将要求您导入excel(.xls)文件,然后打开一个选项卡,说明“参数类型不匹配”

Product.java

package entities;
import java.util.*;
public class Product {

    private String id;
    private String name;
    private long price;     
    private int quatity;
    private boolean status;
    private Date creationDate;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getPrice() {
        return price;
    }
    public void setPrice(long price) {
        this.price = price;
    }
    public int getQuatity() {
        return quatity;
    }
    public void setQuatity(int quatity) {
        this.quatity = quatity;
    }
    public boolean isStatus() {
        return status;
    }
    public void setStatus(boolean status) {
        this.status = status;
    }
    public Date getCreationDate() {
        return creationDate;
    }
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
    public Product(String id, String name, long price, int quatity, boolean status, Date creationDate) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.quatity = quatity;
        this.status = status;
        this.creationDate = creationDate;
    }
    public Product() {
        super();
    }
}
ExcelHelper.java

package helper;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

public class ExcelHelper {

    public List<String> fieldNames = new ArrayList<String>();
    private Workbook workbook = null;
    private String workbookName = "";

    public ExcelHelper(String workbookName){
        this.workbookName = workbookName;
        initialize();

    }

    private void initialize() {
        setWorkbook(new HSSFWorkbook());

    }

    public void closeWorksheet(){
        FileOutputStream fileOut;
        try{
            fileOut = new FileOutputStream(getWorkbookName());
            getWorkbook().write(fileOut);
            fileOut.close();            
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    private boolean setupFieldsForClass(Class<?> clazz) throws Exception{
        Field[] fields = clazz.getDeclaredFields();
        for(int i=0; i < fields.length ; i++){
            fieldNames.add(fields[i].getName());
        }
        return true;
    }

    private Sheet getSheetWithName(String name){
        Sheet sheet = null;
        for(int i=0; i < workbook.getNumberOfSheets() ; i++){
            if(name.compareTo(workbook.getSheetName(i)) == 0){
                sheet = workbook.getSheetAt(i);
                break;
            }
        }
        return sheet;
    }

    private void initializeForRead() throws InvalidFormatException, IOException{
        InputStream inp = new FileInputStream(getWorkbookName());
        workbook = WorkbookFactory.create(inp);
        }

    @SuppressWarnings({"unchecked", "rawtypes"})

    public <T> List<T> readData(String classname) throws Exception{
        initializeForRead();
        Sheet sheet = getSheetWithName(classname);
        Class clazz = Class.forName(workbook.getSheetName(0));
        setupFieldsForClass(clazz);
        List<T> result = new ArrayList<T>();
        Row row;
        for(int rowCount=1; rowCount<3; rowCount++){
            T one = (T) clazz.newInstance();
            row = sheet.getRow(rowCount);
            int colCount = 0;
            result.add(one);
            for(Cell cell : row){
                CellType type = cell.getCellTypeEnum();
                String fieldName = fieldNames.get(colCount++);
                Method method = constructMethod(clazz, fieldName);
                if(type ==  CellType.STRING ){
                    String value = cell.getStringCellValue();
                    Object[] values = new Object[1];
                    values[0] = value;
                    method.invoke(one,  values);
                } else if(type==  CellType.NUMERIC){
                    Double num = cell.getNumericCellValue();
                            Class<?>returnType = getGetterReturnClass(clazz, fieldName);
                            if(returnType == int.class || returnType == Integer.class){
                                method.invoke(one, num.intValue());
                            }else if(returnType == double.class || returnType == Double.class){
                                method.invoke(one, num);
                            }else if(returnType == float.class || returnType == Float.class){
                                method.invoke(one, num.floatValue());
                            }else if(returnType == long.class || returnType == Long.class){
                                method.invoke(one, num.longValue());
                            }else if(returnType == Date.class ){
                                Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                                method.invoke(one, date);
                            }
                            }else if (type == CellType.BOOLEAN){
                                boolean num1 = cell.getBooleanCellValue();
                                Object[] values = new Object[1];
                                values[0] = num1;
                                method.invoke(one, values);
                            }
                }
            }

            return result;
        }


    private Class<?> getGetterReturnClass(Class<?> clazz, String fieldName) {
        String methodName = "get" + capitalize(fieldName);
        String methodIsName = "is" + capitalize(fieldName);
        Class<?> returnType = null;
        for(Method method : clazz.getMethods()){
            if(method.getName().equals(methodName)|| method.getName().equals(methodIsName)){
                returnType = method.getReturnType();
                break;
            }
        }
        return returnType;
    }
    @SuppressWarnings({"unchecked", "rawtypes"})
    private Method constructMethod(Class clazz, String fieldName) throws SecurityException, NoSuchMethodException {
        Class<?> fieldClass = getGetterReturnClass(clazz, fieldName);
        return clazz.getMethod("set" + capitalize(fieldName), fieldClass );
    }

    public <T> void writeData(List<T> data) throws Exception {
       try{
        Sheet sheet = getWorkbook().createSheet(data.get(0).getClass().getName());
        setupFieldsForClass(data.get(0).getClass());
        int rowCount = 0;
        int columnCount = 0;
        Row row = sheet.createRow(rowCount++);
        for (String fieldName : fieldNames) {
            Cell cel = row.createCell(columnCount++);
            cel.setCellValue(fieldName);            
        }
        Class<? extends Object> classz = data.get(0).getClass();
        for (T t : data) {
            row = sheet.createRow(rowCount++);
            columnCount = 0;
            for (String fieldName : fieldNames) {
                Cell cel = row.createCell(columnCount);
                Method method = hasMethod(classz, fieldName)
                        ? classz.getMethod("get"+ capitalize(fieldName))
                        : classz.getMethod("is"+ capitalize(fieldName));
                Object value = method.invoke(t, (Object[]) null);
                if (value != null) {
                    if (value instanceof String) {
                        cel.setCellValue((String) value);
                    } else if (value instanceof Long) {
                        cel.setCellValue((Long) value);
                    } else if (value instanceof Integer) {
                        cel.setCellValue((Integer) value);
                    } else if (value instanceof Double) {
                        cel.setCellValue((Double) value);
                    }else if (value instanceof Date){
                        cel.setCellValue((Date) value);
                        CellStyle styleDate = workbook.createCellStyle();
                        DataFormat dataFormatDate = workbook.createDataFormat();
                        styleDate.setDataFormat(dataFormatDate.getFormat("m/d/yy"));
                        cel.setCellStyle(styleDate);
                    }else if (value instanceof Boolean){
                        cel.setCellValue((Boolean) value);
                    }
                }
                columnCount++;
            }
        }

        for(int i=0 ; i< fieldNames.size(); i++)
            sheet.autoSizeColumn(i);

            FileOutputStream out = new FileOutputStream(new File(workbookName));
            workbook.write(out);
            out.close();
            workbook.close();
        }catch (Exception e){
            System.out.println("Error:" + e);
        }
       }

    @SuppressWarnings({"unchecked", "rawtypes"})

    private boolean hasMethod(Class classz, String fieldName) {
        try{
            classz.getMethod("get" + capitalize(fieldName));
            return true;
        } catch (Exception e){
            return false;
        }
    }

    public String capitalize(String string) {
        String capital = string.substring(0,1).toUpperCase();
        return capital + string.substring(1);
    }
    public String getWorkbookName() {
        return workbookName;

    }
    public void setWorkbookName(String workbookName) {
        this.workbookName = workbookName;
    }

    void  setWorkbook(Workbook workbook) {
        this.workbook =  workbook;
    }
    public Workbook getWorkbook(){
         return workbook;
     }


}
包助手;
导入java.io.*;
导入java.lang.reflect.*;
导入java.util.*;
导入org.apache.poi.hssf.usermodel.HSSFDateUtil;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入org.apache.poi.ss.usermodel.*;
公共类助手{
public List fieldNames=new ArrayList();
私人工作簿=空;
私有字符串workbookName=“”;
公共ExcelHelper(字符串工作簿名){
this.workbookName=workbookName;
初始化();
}
私有void初始化(){
设置工作簿(新的HSSF工作簿());
}
公共工作表(){
FileOutputStream文件输出;
试一试{
fileOut=newfileoutputstream(getWorkbookName());
getWorkbook()。写入(文件输出);
fileOut.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有布尔setupFieldsForClass(类clazz)引发异常{
Field[]fields=clazz.getDeclaredFields();
for(int i=0;i类我发现,如果您提供的输入与预期的输入类型不同,则程序会抛出IllegalArgumentException。
示例输入应如下所示:-

id name price quatity status creationDate id名称价格数量状态创建日期 1测试10.51 20真实2016-05-05
输入类型应与产品类的getter/setter方法类型完全匹配。

“它显示参数类型不匹配。”确切位置?请上载输入excel表以确定确切问题。我清除了“参数类型不匹配错误”但现在我正在尝试更改三列输入数据的代码,这三列的单元格数据类型仅为double/long。现在我面临“index 3 size 3”异常。我想与您共享代码,但您能给我您的邮件id吗? id name price quatity status creationDate 1 test 10.51 20 TRUE 2016-05-05