java poi读写excel

来源:互联网 发布:usb禁用软件 编辑:程序博客网 时间:2024/05/22 14:17

读取excel文件 来自:zhangjp505 的博客 通过poi实现解析并读取excel文件(包含xls、xlsx后缀)

import java.io.FileInputStream;import java.io.FileNotFoundException;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;public class ReadEcxel {    public List<Student> readExcel(String path) {        if (path != null && !path.equals("")) {            String ext = path.substring(path.lastIndexOf(".") + 1, path.length());            if (ext!=null && !ext.equals("")) {            //判断下文件是那种格式                if (ext.equals("xls")) {                    return readXls(path);                } else if (ext.equals("xlsx")) {                    return readXlsx(path);                }            }        }        return new ArrayList<Student>();    }    private List<Student> readXls(String path) {        HSSFWorkbook hssfWorkbook = null;        try {            InputStream is = new FileInputStream(path);            hssfWorkbook = new HSSFWorkbook(is);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        Student student = null;        List<Student> list = new ArrayList<Student>();        if (hssfWorkbook != null) {            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {                HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);                if (hssfSheet == null) {                    continue;                }                for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {                    HSSFRow hssfRow = hssfSheet.getRow(rowNum);                    if (hssfRow != null) {                        student = new Student();                        HSSFCell name = hssfRow.getCell(0);                        HSSFCell age = hssfRow.getCell(1);                        HSSFCell addr = hssfRow.getCell(2);                        student.setName(name.getStringCellValue());                        student.setAge(Integer.parseInt(String.valueOf(age.getNumericCellValue())));                        student.setAddr(String.valueOf(addr.getStringCellValue()));                        list.add(student);                    }                }            }        }        return list;    }    private List<Student> readXlsx(String path) {        XSSFWorkbook xssfWorkbook = null;        try {            InputStream is = new FileInputStream(path);            xssfWorkbook = new XSSFWorkbook(is);        } catch (IOException e) {            e.printStackTrace();        }        Student student = null;        List<Student> list = new ArrayList<Student>();        if(xssfWorkbook!=null){            for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {                XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);                if (xssfSheet == null) {                    continue;                }                for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {                    XSSFRow xssfRow = xssfSheet.getRow(rowNum);                    if (xssfRow != null) {                        student = new Student();                        XSSFCell name = xssfRow.getCell(0);                        XSSFCell age = xssfRow.getCell(1);                        XSSFCell addr = xssfRow.getCell(2);                        student.setName(name.getStringCellValue());                        String s = String.valueOf(age.getNumericCellValue());                        double d = Double.parseDouble(s);                        student.setAge((int)d);                        student.setAddr(String.valueOf(addr.getStringCellValue()));                        list.add(student);                    }                }            }        }        return list;    }    public static void main(String[] args) {    List<Student> list = new ReadEcxel().readExcel("C:/Users/Administrator/Desktop/demo.xlsx");    for (Student s : list) {    System.out.println(s);    }    }}

导出excel文件,这里的话我是用list添加的数据 来自: sdzhangshulong 的博客 : Java 实现导出excel表 POI

import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import com.baidu.poi.readexcel.Student;public class WriteExcel {@SuppressWarnings("deprecation")public static void main(String[] args) {// 第一步,创建一个webbook,对应一个Excel文件@SuppressWarnings("resource")HSSFWorkbook wb = new HSSFWorkbook();// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet("学生表一");// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制shortHSSFRow row = sheet.createRow((int) 0);// 第四步,创建单元格,并设置值表头 设置表头居中HSSFCellStyle style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式HSSFCell cell = row.createCell((short) 0);cell.setCellValue("name");cell.setCellStyle(style);cell = row.createCell((short) 1);cell.setCellValue("age");cell.setCellStyle(style);cell = row.createCell((short) 2);cell.setCellValue("addr");cell.setCellStyle(style);// 第五步,写入实体数据 实际应用中这些数据从数据库得到,List<Student> list = getListStudent();for (int i = 0; i < list.size(); i++) {row = sheet.createRow((int) i + 1);Student stu = (Student) list.get(i);// 第四步,创建单元格,并设置值row.createCell((short) 0).setCellValue(stu.getName());row.createCell((short) 1).setCellValue((double) stu.getAge());row.createCell((short) 2).setCellValue(stu.getAddr());}// 第六步,将文件存到指定位置try {FileOutputStream fout = new FileOutputStream("C:/Users/Administrator/Desktop/students.xls");wb.write(fout);fout.close();} catch (Exception e) {e.printStackTrace();}}private static List<Student> getListStudent() {List<Student> list = new ArrayList<Student>();Student s1 = new Student();s1.setName("李白");s1.setAge(20);s1.setAddr("中国");list.add(s1);Student s2 = new Student();s2.setName("杜普");s2.setAge(21);s2.setAddr("中国");list.add(s2);Student s3 = new Student();s3.setName("屈原");s3.setAge(22);s3.setAddr("中国");list.add(s3);Student s4 = new Student();s4.setName("李清照");s4.setAge(19);s4.setAddr("中国");list.add(s4);return list;}}