Mountains

Phần 09: Hướng dẫn xuất dữ liệu sang Excel, PDF

Tổng số bài

10 bài

Tổng số bài

Thời gian học

4 giờ

Thời gian học

Kỹ năng

Cơ bản

Kỹ năng

Nguồn

teamvietdev.com

Nguồn

Xem video


Lượt xem: 1287 lượt xem


Bước 1: Đầu tiên bạn cần tải thư viện Apache POI về máy, sau đó sao chép vào dự án.

 

Lập trình Java Swing với ứng dụng quản lý học viên

 

Apache POI là một thư viện mã nguồn mở được phân phối bởi Apache. Apache POI được sử dụng phổ biến nhất nhằm giúp tạo hoặc đọc dữ liệu từ các tập tin Microsoft Office bằng ngôn ngữ Java.

Sau khi tải thư viện Apache POI về máy thì bạn cần import thư viện vào dự án.

 

Lập trình Java Swing với ứng dụng quản lý học viên

 

Bước 2: Tiếp theo chúng ta sẽ tạo lớp XuatBaoCaoController.java để xử lý

Trong ví dụ này mình sẽ xuất dữ liệu danh sách các học viên ra tập tin Excel gồm các thông tin như họ tên, số điện thoại, địa chỉ…

package com.teamvietdev.main;

import com.teamvietdev.service.HocVienService;
import com.teamvietdev.service.HocVienServiceImpl;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import test.HocVien;

public class TestExcel {

    public static void main(String[] args) {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet spreadsheet = workbook.createSheet("Học viên");

            XSSFRow row = null;
            Cell cell = null;

            row = spreadsheet.createRow((short) 2);
            row.setHeight((short) 500);
            cell = row.createCell(0, CellType.STRING);
            cell.setCellValue("DANH SÁCH HỌC VIÊN");

            row = spreadsheet.createRow((short) 3);
            row.setHeight((short) 500);
            cell = row.createCell(0, CellType.STRING);
            cell.setCellValue("STT");
            cell = row.createCell(1, CellType.STRING);
            cell.setCellValue("Họ và tên");
            cell = row.createCell(2, CellType.STRING);
            cell.setCellValue("Ngày sinh");
            cell = row.createCell(3, CellType.STRING);
            cell.setCellValue("Giới tính");
            cell = row.createCell(4, CellType.STRING);
            cell.setCellValue("Số điện thoại");
            cell = row.createCell(5, CellType.STRING);
            cell.setCellValue("Địa chỉ");

            HocVienService hocVienService = new HocVienServiceImpl();

            List<HocVien> listItem = hocVienService.getList();

            for (int i = 0; i < listItem.size(); i++) {
                HocVien hocVien = listItem.get(i);
                row = spreadsheet.createRow((short) 4 + i);
                row.setHeight((short) 400);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(hocVien.getHo_ten());
                row.createCell(2).setCellValue(hocVien.getNgay_sinh().toString());
                row.createCell(3).setCellValue(hocVien.isGioi_tinh() ? "Nam" : "Nữ");
                row.createCell(4).setCellValue(hocVien.getSo_dien_thoai());
                row.createCell(5).setCellValue(hocVien.getDia_chi());
            }

            FileOutputStream out = new FileOutputStream(new File("D:/hv.xlsx"));
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

Bước 3: Mở tập tin Excel kiểm tra dữ liệu

 

Lập trình Java Swing với ứng dụng quản lý học viên

 

Bạn cần tùy chỉnh thêm để tập tin Excel sau khi tạo đẹp mắt và dễ nhìn hơn như canh chỉnh màu sắc, độ rộng, kích thước chữ…

 

Link project tham khảo: https://mega.nz/file/o2xHlaLL#HG7RXPhG5bBTwYxnlaQx8DRUSejv-WTWnNJTZq9_H8Q

Bình luận bài học

Đăng bình luận

0 bình luận


Bạn có muốn xác nhận hoàn thành bài học này không?


Xác nhận hoàn thành bài học