「javaexcel版本」java与excel

博主:adminadmin 2022-12-06 13:18:06 60

本篇文章给大家谈谈javaexcel版本,以及java与excel对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA读取EXCEL的版本错误问题

JexcelApi 的jar文件版本和你机器上的JDK版本不一致,或者说是JexcelApi的JDK版本比你机器上的JDK版本高,导致JexcelApi的class文件(jar)不能在你本机上使用,建议你下载个最新的JDK版本

java 如何 判断 读入excel文件的版本(2003?2007?) 请高手指点

apache poi

Workbook hssWB = null;

try {

//2003

hssWB = new HSSFWorkbook(new FileInputStream("excel文件"));

} catch (Exception e) {

// TODO: handle exception

//2007

hssWB = new XSSFWorkbook(new FileInputStream("excel文件"));

}

Java对Excel解析(求助)

这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx。

读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL

代码如下

/**

 * 

 */

package com.b510.common;

/**

 * @author Hongten

 * @created 2014-5-21

 */

public class Common {

    public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";

    public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";

    public static final String EMPTY = "";

    public static final String POINT = ".";

    public static final String LIB_PATH = "lib";

    public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2003_POSTFIX;

    public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2010_POSTFIX;

    public static final String NOT_EXCEL_FILE = " : Not the Excel file!";

    public static final String PROCESSING = "Processing...";

}

/**

 * 

 */

package com.b510.excel;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.b510.common.Common;

import com.b510.excel.util.Util;

import com.b510.excel.vo.Student;

/**

 * @author Hongten

 * @created 2014-5-20

 */

public class ReadExcel {

    

    /**

     * read the Excel file

     * @param path the path of the Excel file

     * @return

     * @throws IOException

     */

    public ListStudent readExcel(String path) throws IOException {

        if (path == null || Common.EMPTY.equals(path)) {

            return null;

        } else {

            String postfix = Util.getPostfix(path);

            if (!Common.EMPTY.equals(postfix)) {

                if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {

                    return readXls(path);

                } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {

                    return readXlsx(path);

                }

            } else {

                System.out.println(path + Common.NOT_EXCEL_FILE);

            }

        }

        return null;

    }

    /**

     * Read the Excel 2010

     * @param path the path of the excel file

     * @return

     * @throws IOException

     */

    public ListStudent readXlsx(String path) throws IOException {

        System.out.println(Common.PROCESSING + path);

        InputStream is = new FileInputStream(path);

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);

        Student student = null;

        ListStudent list = new ArrayListStudent();

        // Read the Sheet

        for (int numSheet = 0; numSheet  xssfWorkbook.getNumberOfSheets(); numSheet++) {

            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);

            if (xssfSheet == null) {

                continue;

            }

            // Read the Row

            for (int rowNum = 1; rowNum = xssfSheet.getLastRowNum(); rowNum++) {

                XSSFRow xssfRow = xssfSheet.getRow(rowNum);

                if (xssfRow != null) {

                    student = new Student();

                    XSSFCell no = xssfRow.getCell(0);

                    XSSFCell name = xssfRow.getCell(1);

                    XSSFCell age = xssfRow.getCell(2);

                    XSSFCell score = xssfRow.getCell(3);

                    student.setNo(getValue(no));

                    student.setName(getValue(name));

                    student.setAge(getValue(age));

                    student.setScore(Float.valueOf(getValue(score)));

                    list.add(student);

                }

            }

        }

        return list;

    }

    /**

     * Read the Excel 2003-2007

     * @param path the path of the Excel

     * @return

     * @throws IOException

     */

    public ListStudent readXls(String path) throws IOException {

        System.out.println(Common.PROCESSING + path);

        InputStream is = new FileInputStream(path);

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

        Student student = null;

        ListStudent list = new ArrayListStudent();

        // Read the Sheet

        for (int numSheet = 0; numSheet  hssfWorkbook.getNumberOfSheets(); numSheet++) {

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

            if (hssfSheet == null) {

                continue;

            }

            // Read the Row

            for (int rowNum = 1; rowNum = hssfSheet.getLastRowNum(); rowNum++) {

                HSSFRow hssfRow = hssfSheet.getRow(rowNum);

                if (hssfRow != null) {

                    student = new Student();

                    HSSFCell no = hssfRow.getCell(0);

                    HSSFCell name = hssfRow.getCell(1);

                    HSSFCell age = hssfRow.getCell(2);

                    HSSFCell score = hssfRow.getCell(3);

                    student.setNo(getValue(no));

                    student.setName(getValue(name));

                    student.setAge(getValue(age));

                    student.setScore(Float.valueOf(getValue(score)));

                    list.add(student);

                }

            }

        }

        return list;

    }

    @SuppressWarnings("static-access")

    private String getValue(XSSFCell xssfRow) {

        if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {

            return String.valueOf(xssfRow.getBooleanCellValue());

        } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {

            return String.valueOf(xssfRow.getNumericCellValue());

        } else {

            return String.valueOf(xssfRow.getStringCellValue());

        }

    }

    @SuppressWarnings("static-access")

    private String getValue(HSSFCell hssfCell) {

        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {

            return String.valueOf(hssfCell.getBooleanCellValue());

        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {

            return String.valueOf(hssfCell.getNumericCellValue());

        } else {

            return String.valueOf(hssfCell.getStringCellValue());

        }

    }

}

/**

 * 

 */

package com.b510.excel.client;

import java.io.IOException;

import java.util.List;

import com.b510.common.Common;

import com.b510.excel.ReadExcel;

import com.b510.excel.vo.Student;

/**

 * @author Hongten

 * @created 2014-5-21

 */

public class Client {

    public static void main(String[] args) throws IOException {

        String excel2003_2007 = Common.STUDENT_INFO_XLS_PATH;

        String excel2010 = Common.STUDENT_INFO_XLSX_PATH;

        // read the 2003-2007 excel

        ListStudent list = new ReadExcel().readExcel(excel2003_2007);

        if (list != null) {

            for (Student student : list) {

                System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());

            }

        }

        System.out.println("======================================");

        // read the 2010 excel

        ListStudent list1 = new ReadExcel().readExcel(excel2010);

        if (list1 != null) {

            for (Student student : list1) {

                System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());

            }

        }

    }

}

/**

 * 

 */

package com.b510.excel.util;

import com.b510.common.Common;

/**

 * @author Hongten

 * @created 2014-5-21

 */

public class Util {

    /**

     * get postfix of the path

     * @param path

     * @return

     */

    public static String getPostfix(String path) {

        if (path == null || Common.EMPTY.equals(path.trim())) {

            return Common.EMPTY;

        }

        if (path.contains(Common.POINT)) {

            return path.substring(path.lastIndexOf(Common.POINT) + 1, path.length());

        }

        return Common.EMPTY;

    }

}

/**

 * 

 */

package com.b510.excel.vo;

/**

 * Student

 * 

 * @author Hongten

 * @created 2014-5-18

 */

public class Student {

    /**

     * id   

     */

    private Integer id;

    /**

     * 学号

     */

    private String no;

    /**

     * 姓名

     */

    private String name;

    /**

     * 学院

     */

    private String age;

    /**

     * 成绩

     */

    private float score;

    public Integer getId() {

        return id;

    }

    public void setId(Integer id) {

        this.id = id;

    }

    public String getNo() {

        return no;

    }

    public void setNo(String no) {

        this.no = no;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    public float getScore() {

        return score;

    }

    public void setScore(float score) {

        this.score = score;

    }

}

java通过poi生成excel的版本问题

XSSF不能读取Excel2003以前(包括2003)的版本,

没需要就按你之前的继续,如果在读取前判断文件是2003前的版本还是2007的版本,提供个思路。XSSF和HSSF虽然在不同的包里,但却引用了同一接口Workbook,

Workbook book = null;

try {

book = new XSSFWorkbook(excelFile);

} catch (Exception ex) {

book = new HSSFWorkbook(new FileInputStream(excelFile));

}

各版本的Excel中测试,没有发生异常

javaexcel版本的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java与excel、javaexcel版本的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-06,除非注明,否则均为首码项目网原创文章,转载请注明出处。