java實現Excel表的導入導出


 

注意該方式需要引入 POI jar包

 

先建個 Student 類
public class Student {
 
 private String name;
    private int age;
    private int grade;

}

添加 get set 及其有參、無參構造方法;

 

准備測試數據集創建Excel表格

public class ExcelOperate {

 public static void main(String[] args) {
        // 創建Excel表格
        createExcel(getStudent());

        // 讀取Excel表格
        List<Student> list = readExcel();
        System.out.println(list.toString());
    }

    /**
     * 初始化數據
     *
     * @return 數據
     */
    private static List<Student> getStudent() {
        List<Student> list = new ArrayList<Student>();
//        Student student1 = new Student("小明", 8, "二年級");
//        Student student2 = new Student("小光", 9, "三年級");
//        Student student3 = new Student("小花", 10, "四年級");
//        list.add(student1);
//        list.add(student2);
//        list.add(student3);
        return list;
    }

    /**
     * 創建Excel
     *
     * @param list
     *            數據
     */
    private static void createExcel(List<Student> list) {
        // 創建一個Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 創建一個工作表
        HSSFSheet sheet = workbook.createSheet("學生表一");
        // 添加表頭行
        HSSFRow hssfRow = sheet.createRow(0);
        // 設置單元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 添加表頭內容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("年齡");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("年級");
        headCell.setCellStyle(cellStyle);

        // 添加數據內容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            Student student = list.get(i);

            // 創建單元格,並設置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(student.getName());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(student.getAge());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            cell.setCellValue(student.getGrade());
            cell.setCellStyle(cellStyle);
        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream("D:/students.xls");
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 讀取Excel
     *
     * @return 數據集合
     */
    private static List<Student> readExcel() {
        List<Student> list = new ArrayList<Student>();
        HSSFWorkbook workbook = null;

        try {
            // 讀取Excel文件
            InputStream inputStream = new FileInputStream("D:/students.xls");
            workbook = new HSSFWorkbook(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 循環工作表
        for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = workbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循環行
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow == null) {
                    continue;
                }

                // 將單元格中的內容存入集合
                Student student = new Student();

                HSSFCell cell = hssfRow.getCell(0);
                if (cell == null) {
                    continue;
                }
                student.setName(cell.getStringCellValue());

                cell = hssfRow.getCell(1);
                if (cell == null) {
                    continue;
                }
                student.setAge((int) cell.getNumericCellValue());

                cell = hssfRow.getCell(2);
                if (cell == null) {
                    continue;
                }
//                student.setGrade(cell.getStringCellValue());

                list.add(student);
            }
        }
        return list;
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM