Java tutorial
/* * Copyright (c) 2014 ????www.hrbesd.com. All rights reserved. * * HRBESD PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package esd.common; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.List; import org.apache.log4j.Logger; 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 esd.bean.Company; import esd.bean.Job; import esd.bean.Resume; import esd.common.disability.test; public class PoiCreateExcel { private static Logger log = Logger.getLogger(PoiCreateExcel.class); /** * ??? * * @param FilePath * @param jobList * @return */ public static boolean createJobExcel(String FilePath, List<Job> jobList) { // Excel Workbook,excel HSSFWorkbook wb = new HSSFWorkbook(); // Excelsheet,exceltab HSSFSheet sheet = wb.createSheet("sheet1"); // excel? sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 3500); // Excel? HSSFRow headRow = sheet.createRow(0); HSSFCell headell = headRow.createCell(0); // ??? headell = headRow.createCell(0); headell.setCellValue("????"); headell = headRow.createCell(1); headell.setCellValue("?"); headell = headRow.createCell(2); headell.setCellValue(""); headell = headRow.createCell(3); headell.setCellValue(""); headell = headRow.createCell(4); headell.setCellValue("???"); headell = headRow.createCell(5); headell.setCellValue(""); headell = headRow.createCell(6); headell.setCellValue("?"); headell = headRow.createCell(7); headell.setCellValue("???"); headell = headRow.createCell(8); headell.setCellValue("?"); headell = headRow.createCell(9); headell.setCellValue("?"); headell = headRow.createCell(10); headell.setCellValue("??"); headell = headRow.createCell(11); headell.setCellValue("?"); headell = headRow.createCell(12); headell.setCellValue("?"); headell = headRow.createCell(13); headell.setCellValue("??"); headell = headRow.createCell(14); headell.setCellValue(""); headell = headRow.createCell(15); headell.setCellValue("? "); headell = headRow.createCell(16); headell.setCellValue("?"); headell = headRow.createCell(17); headell.setCellValue(""); headell = headRow.createCell(18); headell.setCellValue("????"); headell = headRow.createCell(19); headell.setCellValue("????"); headell = headRow.createCell(20); headell.setCellValue("?/?"); headell = headRow.createCell(21); headell.setCellValue(""); headell = headRow.createCell(22); headell.setCellValue("?"); headell = headRow.createCell(23); headell.setCellValue("??"); headell = headRow.createCell(24); headell.setCellValue("??"); for (int i = 1; i <= jobList.size(); i++) { Job job = jobList.get(i - 1); // Excel? HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(0); // ??? cell = row.createCell(0); cell.setCellValue(job.getName()); cell = row.createCell(1); cell.setCellValue(job.getHireNumber()); cell = row.createCell(2); cell.setCellValue(job.getSalary()); cell = row.createCell(3); cell.setCellValue(job.getEducation()); cell = row.createCell(4); cell.setCellValue(job.getExperience()); cell = row.createCell(5); cell.setCellValue(job.getGender()); cell = row.createCell(6); cell.setCellValue(job.getAge()); cell = row.createCell(7); cell.setCellValue(job.getDescription()); cell = row.createCell(8); cell.setCellValue(job.getProvideBenefit()); cell = row.createCell(9); cell.setCellValue(job.getContactPerson()); cell = row.createCell(10); cell.setCellValue(job.getContactTel()); cell = row.createCell(11); cell.setCellValue(job.getContactEmail()); if (job.getViewCount() != null) { cell = row.createCell(12); cell.setCellValue(job.getViewCount()); } cell = row.createCell(13); cell.setCellValue(job.getNature()); cell = row.createCell(14); cell.setCellValue(job.getEffectiveDays()); cell = row.createCell(15); // cell.setCellValue(job.getIsActiveEffectiveTime()); if (job.getEffectiveTime() != null) { SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // ?? String effectiveTimeString = dateFm.format(job.getEffectiveTime()); cell = row.createCell(16); cell.setCellValue(effectiveTimeString); } if (job.getWorkPlace() != null) { if (job.getWorkPlace().getCode() != null && !"".equals(job.getWorkPlace().getCode())) { cell = row.createCell(17); cell.setCellValue(job.getWorkPlace().getName()); } } if (job.isBed() == true) { cell = row.createCell(18); cell.setCellValue(""); } else if (job.isBed() == false) { cell = row.createCell(18); cell.setCellValue("?"); } if (job.isLunch() == true) { cell = row.createCell(19); cell.setCellValue(""); } else if (job.isLunch() == false) { cell = row.createCell(19); cell.setCellValue("?"); } cell = row.createCell(20); cell.setCellValue(job.getCheckStatus()); cell = row.createCell(21); cell.setCellValue(job.getMark()); if (job.getCompany() != null) { cell = row.createCell(22); cell.setCellValue(job.getCompany().getName()); } if (job.getArea() != null) { if (job.getArea().getCode() != null && !"".equals(job.getArea().getCode())) { cell = row.createCell(23); cell.setCellValue(job.getArea().getName()); } } if (job.getJobCategory() != null) { if (job.getJobCategory().getCode() != null && !"".equals(job.getJobCategory().getCode())) { cell = row.createCell(24); cell.setCellValue(job.getJobCategory().getName()); } } } try { FileOutputStream os = new FileOutputStream(FilePath); wb.write(os); os.flush(); os.close(); jobList.clear(); jobList = null; os = null; wb = null; System.gc(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } /** * ??? * * @param FilePath * @param companyList * @return */ public static boolean createComapnyExcel(String FilePath, List<Company> companyList) { // Excel Workbook,excel HSSFWorkbook wb = new HSSFWorkbook(); // Excelsheet,exceltab HSSFSheet sheet = wb.createSheet("sheet1"); // excel? sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 3500); // Excel? HSSFRow headRow = sheet.createRow(0); HSSFCell headell = headRow.createCell(0); // ??? headell = headRow.createCell(0); headell.setCellValue("???"); headell = headRow.createCell(1); headell.setCellValue(""); headell = headRow.createCell(2); headell.setCellValue("?"); headell = headRow.createCell(3); headell.setCellValue("??"); headell = headRow.createCell(4); headell.setCellValue("?"); headell = headRow.createCell(5); headell.setCellValue(""); headell = headRow.createCell(6); headell.setCellValue("?"); headell = headRow.createCell(7); headell.setCellValue("??"); headell = headRow.createCell(8); headell.setCellValue("?"); headell = headRow.createCell(9); headell.setCellValue("?"); headell = headRow.createCell(10); headell.setCellValue("??"); headell = headRow.createCell(11); headell.setCellValue("?"); headell = headRow.createCell(12); headell.setCellValue("???"); headell = headRow.createCell(13); headell.setCellValue("ID"); headell = headRow.createCell(14); headell.setCellValue("?"); headell = headRow.createCell(15); headell.setCellValue("?"); headell = headRow.createCell(16); headell.setCellValue("?"); headell = headRow.createCell(17); headell.setCellValue(" ?"); headell = headRow.createCell(18); headell.setCellValue(""); headell = headRow.createCell(19); headell.setCellValue("?"); // headell = headRow.createCell(20); // headell.setCellValue("?"); for (int i = 1; i <= companyList.size(); i++) { Company company = companyList.get(i - 1); // Excel? HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(0); // ??? // ??? cell = row.createCell(0); cell.setCellValue(company.getName()); // cell = row.createCell(1); cell.setCellValue(company.getCorporateRepresentative()); // ? cell = row.createCell(2); cell.setCellValue(company.getContactPerson()); // ?? cell = row.createCell(3); cell.setCellValue(company.getTelephone()); // ? cell = row.createCell(4); cell.setCellValue(company.getContactDept()); // cell = row.createCell(5); cell.setCellValue(company.getFax()); // ? cell = row.createCell(6); cell.setCellValue(company.getEmail()); // ?? cell = row.createCell(7); cell.setCellValue(company.getAddress()); // ? cell = row.createCell(8); cell.setCellValue(company.getIntroduction()); // ? cell = row.createCell(9); cell.setCellValue(company.getOrganizationCode()); // ?? cell = row.createCell(10); cell.setCellValue(company.getCommercialCode()); // ? cell = row.createCell(11); cell.setCellValue(company.getTaxCode()); // ??? cell = row.createCell(12); cell.setCellValue(company.getSocialSecurityCode()); // ID cell = row.createCell(13); cell.setCellValue(company.getWebSiteId()); // ? cell = row.createCell(14); cell.setCellValue(company.getLaoWangCode()); // ? cell = row.createCell(15); cell.setCellValue(company.getScale()); // ? cell = row.createCell(16); cell.setCellValue(company.getNature()); // ? cell = row.createCell(17); cell.setCellValue(company.getEconomyType()); // cell = row.createCell(18); cell.setCellValue(company.getRemark()); // ? if (company.getViewCount() != null) { cell = row.createCell(19); cell.setCellValue(company.getViewCount()); } // cell = row.createCell(20); // cell.setCellValue(company.getCheckStatus()); } try { FileOutputStream os = new FileOutputStream(FilePath); wb.write(os); os.flush(); os.close(); companyList.clear(); companyList = null; os = null; wb = null; System.gc(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } /** * ? * * @param FilePath * @param resumeList * @return */ public static boolean createResumeExcel(String FilePath, List<Resume> resumeList) { // Excel Workbook,excel HSSFWorkbook wb = new HSSFWorkbook(); // Excelsheet,exceltab HSSFSheet sheet = wb.createSheet("sheet1"); // excel? sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 3500); // Excel? HSSFRow headRow = sheet.createRow(0); HSSFCell headell = headRow.createCell(0); // ??? headell = headRow.createCell(0); headell.setCellValue("??"); headell = headRow.createCell(1); headell.setCellValue("??"); headell = headRow.createCell(2); headell.setCellValue(""); headell = headRow.createCell(3); headell.setCellValue(""); headell = headRow.createCell(4); headell.setCellValue("??"); headell = headRow.createCell(5); headell.setCellValue("?"); headell = headRow.createCell(6); headell.setCellValue(""); headell = headRow.createCell(7); headell.setCellValue("?"); headell = headRow.createCell(8); headell.setCellValue("??"); headell = headRow.createCell(9); headell.setCellValue("?"); headell = headRow.createCell(10); headell.setCellValue("??"); headell = headRow.createCell(11); headell.setCellValue("?"); headell = headRow.createCell(12); headell.setCellValue("?"); headell = headRow.createCell(13); headell.setCellValue("?"); headell = headRow.createCell(14); headell.setCellValue("QQ??"); headell = headRow.createCell(15); headell.setCellValue(""); headell = headRow.createCell(16); headell.setCellValue("??"); headell = headRow.createCell(17); headell.setCellValue(" "); headell = headRow.createCell(18); headell.setCellValue("?"); headell = headRow.createCell(19); headell.setCellValue(""); headell = headRow.createCell(20); headell.setCellValue("?"); headell = headRow.createCell(21); headell.setCellValue("?"); headell = headRow.createCell(22); headell.setCellValue(""); headell = headRow.createCell(23); headell.setCellValue(" cm"); headell = headRow.createCell(24); headell.setCellValue("? kg"); headell = headRow.createCell(25); headell.setCellValue(""); headell = headRow.createCell(26); headell.setCellValue(""); headell = headRow.createCell(27); headell.setCellValue(""); headell = headRow.createCell(28); headell.setCellValue("?"); headell = headRow.createCell(29); headell.setCellValue("?"); headell = headRow.createCell(30); headell.setCellValue("? ?"); headell = headRow.createCell(31); headell.setCellValue(""); headell = headRow.createCell(32); headell.setCellValue(""); headell = headRow.createCell(33); headell.setCellValue("?"); headell = headRow.createCell(34); headell.setCellValue("?"); headell = headRow.createCell(35); headell.setCellValue("/?"); headell = headRow.createCell(36); headell.setCellValue(""); headell = headRow.createCell(37); headell.setCellValue(""); headell = headRow.createCell(38); headell.setCellValue("??"); headell = headRow.createCell(39); headell.setCellValue(""); headell = headRow.createCell(40); headell.setCellValue(""); headell = headRow.createCell(41); headell.setCellValue("???"); headell = headRow.createCell(42); headell.setCellValue("????"); headell = headRow.createCell(43); headell.setCellValue("???"); headell = headRow.createCell(44); headell.setCellValue("???"); headell = headRow.createCell(45); headell.setCellValue(""); headell = headRow.createCell(46); headell.setCellValue("???"); headell = headRow.createCell(47); headell.setCellValue("??"); headell = headRow.createCell(48); headell.setCellValue("??"); headell = headRow.createCell(49); headell.setCellValue(""); headell = headRow.createCell(50); headell.setCellValue("?"); headell = headRow.createCell(51); headell.setCellValue("?"); for (int i = 1; i <= resumeList.size(); i++) { Resume resume = resumeList.get(i - 1); // Excel? HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(0); // ??? cell = row.createCell(0); cell.setCellValue(resume.getTitle()); cell = row.createCell(1); cell.setCellValue(resume.getName()); cell = row.createCell(2); cell.setCellValue(resume.getGender()); cell = row.createCell(3); cell.setCellValue(resume.getBirth()); cell = row.createCell(4); cell.setCellValue(resume.getIdentityCard()); cell = row.createCell(5); cell.setCellValue(resume.getRace()); cell = row.createCell(6); cell.setCellValue(resume.getMarriage()); if (resume.getHukou() != null) { if (resume.getHukou().getCode() != null && !"".equals(resume.getHukou().getCode())) { cell = row.createCell(7); cell.setCellValue(resume.getHukou().getName()); } } cell = row.createCell(8); cell.setCellValue(resume.getHukouAddress()); cell = row.createCell(9); cell.setCellValue(resume.getHukouStatus()); cell = row.createCell(10); cell.setCellValue(resume.getAddress()); cell = row.createCell(11); cell.setCellValue(resume.getZipcode()); cell = row.createCell(12); cell.setCellValue(resume.getPhone()); cell = row.createCell(13); cell.setCellValue(resume.getEmail()); cell = row.createCell(14); cell.setCellValue(resume.getQq()); cell = row.createCell(15); cell.setCellValue(resume.getDisabilityCategory()); cell = row.createCell(16); cell.setCellValue(resume.getDisabilityCard()); cell = row.createCell(17); cell.setCellValue(resume.getDisabilityLevel()); cell = row.createCell(18); cell.setCellValue(resume.getDisabilityPart()); cell = row.createCell(19); cell.setCellValue(resume.getWorkAbility()); cell = row.createCell(20); cell.setCellValue(resume.getHomeTown()); cell = row.createCell(21); cell.setCellValue(resume.getPoliticalStatus()); cell = row.createCell(22); cell.setCellValue(resume.getAge()); cell = row.createCell(23); cell.setCellValue(resume.getHeight()); cell = row.createCell(24); cell.setCellValue(resume.getWeight()); cell = row.createCell(25); cell.setCellValue(resume.getProcessState()); cell = row.createCell(26); cell.setCellValue(resume.getEducation()); cell = row.createCell(27); cell.setCellValue(resume.getMajor()); cell = row.createCell(28); cell.setCellValue(resume.getSchool()); cell = row.createCell(29); cell.setCellValue(resume.getZhiCheng()); cell = row.createCell(30); cell.setCellValue(resume.getShiYeHao()); cell = row.createCell(31); cell.setCellValue(resume.getExperts()); cell = row.createCell(32); cell.setCellValue(resume.getTraining()); cell = row.createCell(33); cell.setCellValue(resume.getExperience()); cell = row.createCell(34); cell.setCellValue(resume.getWorkExperience()); cell = row.createCell(35); cell.setCellValue(resume.getSelfEvaluation()); cell = row.createCell(36); cell.setCellValue(resume.getAttachment()); cell = row.createCell(37); cell.setCellValue(resume.getJobNature()); if (resume.getDesireJob() != null) { if (resume.getDesireJob().getCode() != null && !"".equals(resume.getDesireJob().getCode())) { cell = row.createCell(38); log.info(resume.getDesireJob().getName() + "********8"); cell.setCellValue(resume.getDesireJob().getName()); } } if (resume.getDesireAddress() != null) { if (resume.getDesireAddress().getCode() != null && !"".equals(resume.getDesireAddress().getCode())) { cell = row.createCell(39); cell.setCellValue(resume.getDesireAddress().getName()); } } cell = row.createCell(40); cell.setCellValue(resume.getDesireSalary()); if (resume.isProvideFoodAndRoom() == true) { cell = row.createCell(41); cell.setCellValue(""); } else if (resume.isProvideFoodAndRoom() == false) { cell = row.createCell(41); cell.setCellValue("?"); } if (resume.isProvideRoom() == true) { cell = row.createCell(42); cell.setCellValue(""); } else if (resume.isProvideRoom() == false) { cell = row.createCell(42); cell.setCellValue("?"); } if (resume.isProvideFood() == true) { cell = row.createCell(43); cell.setCellValue(""); } else if (resume.isProvideFood() == false) { cell = row.createCell(43); cell.setCellValue("?"); } if (resume.isProvideInsurance() == true) { cell = row.createCell(44); cell.setCellValue(""); } else if (resume.isProvideInsurance() == false) { cell = row.createCell(44); cell.setCellValue("?"); } cell = row.createCell(45); cell.setCellValue(resume.getProvideOther()); if (resume.isWorkShift() == true) { cell = row.createCell(46); cell.setCellValue(""); } else if (resume.isWorkShift() == false) { cell = row.createCell(46); cell.setCellValue("?"); } cell = row.createCell(47); cell.setCellValue(resume.getState()); if (resume.getIsDefault() == true) { cell = row.createCell(48); cell.setCellValue(""); } else if (resume.getIsDefault() == false) { cell = row.createCell(48); cell.setCellValue("?"); } cell = row.createCell(49); cell.setCellValue(resume.getCheckStatus()); if (resume.getViewCount() != null) { cell = row.createCell(50); cell.setCellValue(resume.getViewCount()); } cell = row.createCell(51); cell.setCellValue(resume.getCareerTest()); } try { FileOutputStream os = new FileOutputStream(FilePath); wb.write(os); os.flush(); os.close(); resumeList.clear(); resumeList = null; os = null; wb = null; System.gc(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } }