Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /* Lp ny dng m file excel c sn ly d liu v ghi d liu vo file excel c sn*/ package control; import control.NhapHangHoa; import entities.HangHoa; import entities.HangNhap; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import org.apache.poi.ss.usermodel.CellType; 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; /** * * @author phamh */ public class DocFile { private final int SP_MA_HANG_HOA = 0; private final int SP_TEN_HANG_HOA = 1; private final int SP_NHOM_HANG_HOA = 2; private final int SP_GIA_BAN = 3; private final int SP_GIA_VON = 4; private final int SP_TON_KHO = 5; private final int SP_DINH_MUC_TON_IT_NHAT = 6; private final int SP_DINH_MUC_TON_NHIEU_NHAT = 7; private XSSFSheet sheet; private int sheetNumber; XSSFWorkbook workbook; FileInputStream fileInputStream; public boolean chapNhan = false; // Constructor truy?n tn file cn m public DocFile(String fileName) throws FileNotFoundException, IOException { fileInputStream = new FileInputStream(new File(fileName)); workbook = new XSSFWorkbook(fileInputStream); sheetNumber = 0; sheet = workbook.getSheetAt(sheetNumber); } // Constructor v m jFileChooser ly ?ng dn file public DocFile() throws FileNotFoundException, IOException { String path = this.getPath(); if (path != null) { fileInputStream = new FileInputStream(new File(path)); workbook = new XSSFWorkbook(fileInputStream); sheetNumber = 0; sheet = workbook.getSheetAt(sheetNumber); } } // M openDialog(jChooseFile) v ly ?ng dn public String getPath() { //To file chooser JFileChooser fileChooser = new JFileChooser(); ; //M m hp thoai OPEN trn mComponent int returnVal = fileChooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); chapNhan = true; return file.getAbsolutePath(); } return null; } // Ly s record trong file public int laySoRecordTrongSheet() { return sheet.getPhysicalNumberOfRows(); } // Ly d liu v tr v? kiu d liu HangHoa trong File excel HangHoa public HangHoa layMotHangHoa(int rowNum) { XSSFRow row = sheet.getRow(rowNum); XSSFCell maHangHoa = row.getCell(SP_MA_HANG_HOA); XSSFCell tenHangHoa = row.getCell(SP_TEN_HANG_HOA); XSSFCell nhomHangHoa = row.getCell(SP_NHOM_HANG_HOA); XSSFCell giaVon = row.getCell(SP_GIA_VON); XSSFCell giaBan = row.getCell(SP_GIA_BAN); XSSFCell tonKho = row.getCell(SP_TON_KHO); XSSFCell dinhMucTonItNhat = row.getCell(SP_DINH_MUC_TON_IT_NHAT); XSSFCell dinhMucTonNhieuNhat = row.getCell(SP_DINH_MUC_TON_NHIEU_NHAT); return new HangHoa(maHangHoa, tenHangHoa, giaBan, giaVon, tonKho, nhomHangHoa, dinhMucTonItNhat, dinhMucTonNhieuNhat); } //Ly tt c cc hng ha trong file v tr v? arrayList Hng Ha public ArrayList<HangHoa> layTatCaHangHoa() { // To arrayList danh sch hng ha ly c ArrayList<HangHoa> arlHangHoa = new ArrayList<>(); // Ly t row 1 (bi v row 0 l tn ct) for (int i = 1; i < this.laySoRecordTrongSheet(); i++) { arlHangHoa.add(this.layMotHangHoa(i)); } return arlHangHoa; } // Dua du lieu vao file excel(file excel da co san) public void setProductToFile(HangHoa hangHoa) { XSSFRow row = sheet.createRow(this.laySoRecordTrongSheet() + 2); row.createCell(SP_MA_HANG_HOA, CellType.STRING).setCellValue(hangHoa.getMaHangHoa()); row.createCell(SP_TEN_HANG_HOA, CellType.STRING).setCellValue(hangHoa.getTenHangHoa()); row.createCell(SP_NHOM_HANG_HOA, CellType.STRING).setCellValue(hangHoa.getNhomHangHoa()); row.createCell(SP_GIA_VON, CellType.STRING).setCellValue(hangHoa.getGiaVon()); row.createCell(SP_GIA_BAN, CellType.STRING).setCellValue(hangHoa.getGiaBan()); row.createCell(SP_TON_KHO, CellType.STRING).setCellValue(hangHoa.getTonKho()); row.createCell(SP_DINH_MUC_TON_IT_NHAT, CellType.STRING).setCellValue(hangHoa.getDinhMucTonItNhat()); row.createCell(SP_DINH_MUC_TON_NHIEU_NHAT, CellType.STRING).setCellValue(hangHoa.getDinhMucTonNhieuNhat()); } // Ly 1 hng ha trong file nhp hng public HangNhap layMotHangNhap(int rowNum) { XSSFRow row = sheet.getRow(rowNum); XSSFCell maHangHoa = row.getCell(0); XSSFCell tenHangHoa = row.getCell(1); XSSFCell nhomHangHoa = row.getCell(2); XSSFCell giaVon = row.getCell(3); XSSFCell soLuong = row.getCell(4); return new HangNhap(maHangHoa, tenHangHoa, giaVon, soLuong, nhomHangHoa); } // Ly danh sch cc hng ha t file nhp hng public ArrayList<HangNhap> layDanhSachHangNhap() { ArrayList<HangNhap> arlHangNhap = new ArrayList<>(); for (int i = 1; i < laySoRecordTrongSheet(); i++) { arlHangNhap.add(this.layMotHangNhap(i)); } return arlHangNhap; } // ?ng file public void closeFile() { try { //?ng workbook this.workbook.close(); System.out.print("\n Close file successfully"); // ?ng fileInputStream fileInputStream.close(); } catch (IOException ex) { System.out.print("\n Close file unsuccessfully"); } } }