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. */ package control; import entities.HangHoa; import entities.HangNhap; import entities.HoaDonBanHang; import entities.KhachHang; import entities.NhaCungCap; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; 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.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @author phamh */ public class GhiFile { private XSSFSheet sheet; private int sheetNumber; XSSFWorkbook workbook; public GhiFile() { workbook = new XSSFWorkbook(); sheet = workbook.createSheet(); } // to file v ghi d liu vo file public void taoFileHangHoa(ArrayList<HangHoa> arlHangHoa) { XSSFRow rowTitle = sheet.createRow(0); rowTitle.createCell(0).setCellValue("M sn phm"); rowTitle.createCell(1).setCellValue("Tn sn phm"); rowTitle.createCell(2).setCellValue("Nhm sn phm"); rowTitle.createCell(3).setCellValue("Gi bn"); rowTitle.createCell(4).setCellValue("Gi vn"); rowTitle.createCell(5).setCellValue("Tn kho"); rowTitle.createCell(6).setCellValue("?nh mc tn t nht"); rowTitle.createCell(7).setCellValue("?nh mc tn nhi?u nht"); int rowNum = sheet.getLastRowNum(); for (int i = 0; i < arlHangHoa.size(); i++) { rowNum++; XSSFRow row = sheet.createRow(rowNum); HangHoa hh = arlHangHoa.get(i); row.createCell(0).setCellValue(hh.getMaHangHoa()); row.createCell(1).setCellValue(hh.getTenHangHoa()); row.createCell(2).setCellValue(hh.getNhomHangHoa()); row.createCell(3).setCellValue(hh.getGiaBan()); row.createCell(4).setCellValue(hh.getGiaBan() - (hh.getGiaBan() * 40 / 100)); row.createCell(5).setCellValue(hh.getTonKho()); row.createCell(6).setCellValue(hh.getDinhMucTonItNhat()); row.createCell(7).setCellValue(hh.getDinhMucTonNhieuNhat()); } File myFile = new File(path()); FileOutputStream os = null; try { // Khi to Stream ghi file os = new FileOutputStream(myFile); //Ghi d liu t workbook vo workbook.write(os); System.out.println("Vit d liu vo file excel thnh cng"); //?ng workbook workbook.close(); // ?ng stream os.close(); } catch (FileNotFoundException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } } //to file bng gi hng ha // to file v ghi d liu vo file public void taoFileBangGiaHangHoa(ArrayList<HangHoa> arlHangHoa) { XSSFRow rowTitle = sheet.createRow(0); rowTitle.createCell(0).setCellValue("M sn phm"); rowTitle.createCell(1).setCellValue("Tn sn phm"); rowTitle.createCell(2).setCellValue("Nhm sn phm"); rowTitle.createCell(3).setCellValue("Gi vn"); rowTitle.createCell(4).setCellValue("Gi bn"); int rowNum = sheet.getLastRowNum(); for (int i = 0; i < arlHangHoa.size(); i++) { rowNum++; XSSFRow row = sheet.createRow(rowNum); HangHoa hh = arlHangHoa.get(i); row.createCell(0).setCellValue(hh.getMaHangHoa()); row.createCell(1).setCellValue(hh.getTenHangHoa()); row.createCell(2).setCellValue(hh.getNhomHangHoa()); row.createCell(3).setCellValue(hh.getGiaBan()); row.createCell(4).setCellValue(hh.mGiaVon); } File myFile = new File(path()); FileOutputStream os = null; try { // Khi to Stream ghi file os = new FileOutputStream(myFile); //Ghi d liu t workbook vo workbook.write(os); System.out.println("Vit d liu vo file excel thnh cng"); //?ng workbook workbook.close(); // ?ng stream os.close(); } catch (FileNotFoundException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } } //to danh sch cc ha n public void taoDanhSachHoaDoa(ArrayList<HoaDonBanHang> dsHoaDon) { XSSFRow rowTitle = sheet.createRow(0); rowTitle.createCell(0).setCellValue("M ha n"); rowTitle.createCell(1).setCellValue("Tng ti?n"); rowTitle.createCell(2).setCellValue("Gim gi"); rowTitle.createCell(3).setCellValue("Khch tr"); rowTitle.createCell(4).setCellValue("Th?i gian"); rowTitle.createCell(5).setCellValue("Ghi ch"); int rowNum = sheet.getLastRowNum(); for (int i = 0; i < dsHoaDon.size(); i++) { rowNum++; XSSFRow row = sheet.createRow(rowNum); HoaDonBanHang hd = dsHoaDon.get(i); row.createCell(0).setCellValue(hd.mMaHoaDonBanHang); row.createCell(1).setCellValue(hd.mTongTien); row.createCell(2).setCellValue(hd.mGiaGiam); row.createCell(3).setCellValue(hd.mKhachDaTra); row.createCell(4).setCellValue(hd.mThoiGian); row.createCell(5).setCellValue(hd.mGhiChu); } File myFile = new File(path()); FileOutputStream os = null; try { // Khi to Stream ghi file os = new FileOutputStream(myFile); //Ghi d liu t workbook vo workbook.write(os); System.out.println("Vit d liu vo file excel thnh cng"); //?ng workbook workbook.close(); // ?ng stream os.close(); } catch (FileNotFoundException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } } //M hp thoi v nhp tn file cn lu tr v? ?ng dn lu file // c g?i trong taoFileHangHoa, taoFileNhapHangHoa public String path() { //To file chooser final JFileChooser fc = new JFileChooser(); //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //M m hp thoai SAVE trn mComponent int returnVal = fc.showSaveDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); System.out.println(fc.getSelectedFile() + ".xlsx"); return fc.getSelectedFile() + ".xlsx"; } return null; } // To file cha hng ha cn nhp public void taoFileHangNhap(ArrayList<HangHoa> arlHangNhap) { // ?t tn ct XSSFRow rowTitle = sheet.createRow(0); rowTitle.createCell(0).setCellValue("M sn phm"); rowTitle.createCell(1).setCellValue("Tn sn phm"); rowTitle.createCell(2).setCellValue("Nhm sn phm"); rowTitle.createCell(3).setCellValue("Gi mua"); rowTitle.createCell(4).setCellValue("S lng"); int rowNum = sheet.getLastRowNum(); for (int i = 0; i < arlHangNhap.size(); i++) { rowNum++; XSSFRow row = sheet.createRow(rowNum); HangHoa hn = arlHangNhap.get(i); row.createCell(0).setCellValue(hn.mMaHangHoa); row.createCell(1).setCellValue(hn.mTenHangHoa); row.createCell(2).setCellValue(hn.mNhomHangHoa); row.createCell(3).setCellValue(hn.mGiaVon); row.createCell(4).setCellValue(3); // s lng cng } File myFile = new File(path()); FileOutputStream os = null; try { // Khi to Stream ghi file os = new FileOutputStream(myFile); //Ghi d liu t workbook vo workbook.write(os); System.out.println("Vit d liu vo file excel thnh cng"); //?ng workbook workbook.close(); // ?ng stream os.close(); } catch (FileNotFoundException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } } // To file cha hng ha cn nhp public void taoFileNhaCungCap(ArrayList<NhaCungCap> dsNhaCungCap) { // ?t tn ct XSSFRow rowTitle = sheet.createRow(0); rowTitle.createCell(0).setCellValue("M nh cung cp"); rowTitle.createCell(1).setCellValue("Tn nh cung cp"); rowTitle.createCell(2).setCellValue("Nhm nh cung cp"); rowTitle.createCell(3).setCellValue("Cn n"); rowTitle.createCell(4).setCellValue("Email"); rowTitle.createCell(4).setCellValue("?a ch"); int rowNum = sheet.getLastRowNum(); for (int i = 0; i < dsNhaCungCap.size(); i++) { rowNum++; XSSFRow row = sheet.createRow(rowNum); NhaCungCap ncc = dsNhaCungCap.get(i); row.createCell(0).setCellValue(ncc.mMaNhaCungCap); row.createCell(1).setCellValue(ncc.mTenNhaCungCap); row.createCell(2).setCellValue(ncc.mNhomNhaCungCap); // n hin ti row.createCell(3).setCellValue(ncc.mMaCongNoNhaCungCap); row.createCell(4).setCellValue(ncc.mEmail); row.createCell(5).setCellValue(ncc.mDiaChi); } File myFile = new File(path()); FileOutputStream os = null; try { // Khi to Stream ghi file os = new FileOutputStream(myFile); //Ghi d liu t workbook vo workbook.write(os); System.out.println("Vit d liu vo file excel thnh cng"); //?ng workbook workbook.close(); // ?ng stream os.close(); } catch (FileNotFoundException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } } // to file danh sch khch hang // To file cha hng ha cn nhp public void taoFileKhachHang(ArrayList<KhachHang> dsKhachHang) { // ?t tn ct XSSFRow rowTitle = sheet.createRow(0); rowTitle.createCell(0).setCellValue("M khch hng"); rowTitle.createCell(1).setCellValue("Tn tn khch hng"); rowTitle.createCell(2).setCellValue("N hin ti"); rowTitle.createCell(3).setCellValue("?in thoi"); rowTitle.createCell(4).setCellValue("Ngy sinh"); int rowNum = sheet.getLastRowNum(); for (int i = 0; i < dsKhachHang.size(); i++) { rowNum++; XSSFRow row = sheet.createRow(rowNum); KhachHang kh = dsKhachHang.get(i); row.createCell(0).setCellValue(kh.mMaKhachHang); row.createCell(1).setCellValue(kh.mTenKhachHang); row.createCell(2).setCellValue(kh.mMaCongNoKhachHang); // n hin ti row.createCell(3).setCellValue(kh.mDienThoai); row.createCell(4).setCellValue(kh.mNgaySinh); } File myFile = new File(path()); FileOutputStream os = null; try { // Khi to Stream ghi file os = new FileOutputStream(myFile); //Ghi d liu t workbook vo workbook.write(os); System.out.println("Vit d liu vo file excel thnh cng"); //?ng workbook workbook.close(); // ?ng stream os.close(); } catch (FileNotFoundException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GhiFile.class.getName()).log(Level.SEVERE, null, ex); } } }