上传excel表格数据到数据库
导入固定路径下的excel数据到数据库
<form id="disposeFlightDataForm" action="../upload/disposeFlightData" method="post"> <input id="disposeFlightDataButton" type="submit" value="处理航班数据" /> </form>
后台代码类
@Controller
@RequestMapping(value = "/upload")
public class UploadSaleDataAction
/** * 页面上传excel文件保存到tomcat的sams/upload目下 * * @param file * @param request * @param model */ @RequestMapping(value = "/disposeFlightData") public String disposeFlightData() { flightDataService.disposeFlightData(); return GO_UPLAODEXCEL; }
@Component
@Transactional
@Service
public class FlightDataServiceImpl implements FlightDataService类
@Override public void disposeFlightData() { // 接收的文件所在目录 String filePath = "E:/Accpet"; FileOperate.newFolder(filePath); File fileDir = new File(filePath); File[] flightDataFiles = fileDir.listFiles(); System.out.println("该目录下对象个数:" + flightDataFiles.length); // 循环目录下的文件 for (int i = 0; i < flightDataFiles.length; i++) { System.out.println("第"+i+"对象个数:"+flightDataFiles[i].getName()); File flightDataFile = flightDataFiles[i]; if (flightDataFile.exists() && flightDataFile.isFile() && flightDataFile.getName().lastIndexOf(".xls") > 0 && DateUtil.isValidDate(flightDataFile.getName().substring(0, 8))) { List<FlightData> fdList = DisposeFlightData .parseExcelToFlightData(flightDataFile); List<FlightData> addfdList = new ArrayList<FlightData>(); for (int j = 0; j < fdList.size(); j++) { System.out.println(flightDataFiles[i].getName()+j); FlightData fd = fdList.get(j); FlightData data = flightDataDao.findFlightData(fd); if (data == null) { addfdList.add(fd); }
//批量处理添加,每1000条保存一次 if(addfdList.size()%1000==0||j==fdList.size()-1){ flightDataDao.addFdList(addfdList); // flightDataDao.addFdList(fdList); addfdList.clear(); } } } else { String newPath = "E:/Reject"; FileOperate.newFolder(newPath); FileOperate.moveFile(flightDataFile.getPath(), newPath + "/" + flightDataFile.getName()); } } }
解析excel数据到实体类
package cn.com.acca.sams.sales.test; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import cn.com.acca.sams.common.utils.XlsReader; import cn.com.acca.sams.sales.domainmodel.FlightData; /** * * 航班数据解析入库. * * @author Yan jianjun * @since 2015-09-14 */ public class DisposeFlightData { /** * 解析excel航班数据设置到FlightData对象中 * * @param file * @return */ public static List<FlightData> parseExcelToFlightData(File file) { XlsReader xlsReader = XlsReader.loadXls(file.getPath()); HSSFSheet sheet = null; HSSFRow row = null; String fileName = file.getName();// forexample:20150601MUD.xls,20150601MUI.xls // 接口文件名 ,东航航班数据文件的文件名 String interfaceFile = fileName; fileName = fileName.split("\.")[0]; SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATEFORMAT2); Date date = new Date(); List<FlightData> flightDataList = new ArrayList<FlightData>(); for (int i = 0; i < xlsReader.getSheets(); i++) { sheet = xlsReader.getSheetBySheetNo(i); // System.out.println("总行数---》" + (sheet.getPhysicalNumberOfRows() - 1)); // 这里j从1开始,即从第二行起,因为第一行是属性名,不用读取。 for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) { // for (int j = 33955; j < sheet.getPhysicalNumberOfRows(); j++) { row = sheet.getRow(j); // 判断如果第一列为空不处理 if (row.getCell(0).getStringCellValue() != null && !"".equals(row.getCell(0).getStringCellValue())) { FlightData flightData = new FlightData(); // flightData.setFlightDate(Long.valueOf(flightDate)); // flightData.setMarketCarrier(marketCarrier); flightData.setFileName(interfaceFile); // flightData.setDiInd(DI); // flightData.setInterfaceDate(interfaceDate); flightData.setCreatedDate(date); setValueToFlightData(flightData, row); flightDataList.add(flightData); } } } return flightDataList; } /** * 将一行航班数据设置到FlightData对象中 * * @param row */ private static void setValueToFlightData(FlightData flightData, HSSFRow row) { HSSFCell cell = null; //设置航班日期 String flightDate = row.getCell(0).getStringCellValue().replace("-", ""); // if(flightDate.equals("20150919")&&row.getCell(2).getStringCellValue().equals("7158")){ // System.out.println(row.getCell(10).getStringCellValue()); // } flightData.setFlightDate(Long.valueOf(flightDate)); //承运人 String operateCarrier = row.getCell(1).getStringCellValue(); flightData.setOperateCarrier(operateCarrier); // 设置航班号 cell = row.getCell(2); // flightData.setMarketFlightNo(filterString(cell.getStringCellValue().replaceAll("CES", "MU"))); flightData.setOperateFlightNo(operateCarrier+cell.getStringCellValue()); // 设置飞机注册号 cell = row.getCell(3); flightData.setAircraftRegistrationNo(cell.getStringCellValue()); // 设置机型 // cell = row.getCell(2); // flightData.setAirCraft(cell.getStringCellValue()); //性质 cell = row.getCell(4); flightData.setProperty(cell.getStringCellValue()); // 设置起飞地 cell = row.getCell(6); flightData.setDepartureStation(cell.getStringCellValue()); //计划起飞站 cell = row.getCell(7); flightData.setPlannedAepartureStation(cell.getStringCellValue()); // 计算后的最终日期 Date finalDate = null; // 计飞 cell = row.getCell(8); finalDate = calDate(flightDate, cell); if (finalDate != null) { flightData.setPlannedDepartureDate(finalDate); } //关舱门日期 cell = row.getCell(9); finalDate = calDate(flightDate, cell); if (finalDate != null) { flightData.setCloseDoorDate(finalDate); } // 实飞 cell = row.getCell(10); finalDate = calDate(flightDate, cell); if (finalDate != null) { flightData.setActualDepartureDate(finalDate); } //到站 cell = row.getCell(11); flightData.setArrivalStation(cell.getStringCellValue()); // 计划到达站 cell = row.getCell(12); flightData.setPlannedArrivalStation(cell.getStringCellValue()); //计划到达日期,时间 cell = row.getCell(13); finalDate = calDate(flightDate, cell); if (finalDate != null) { flightData.setPlannedArrivalDate(finalDate); } //实际降落(到达)日期,时间 实到 cell = row.getCell(14); finalDate = calDate(flightDate, cell); if (finalDate != null) { flightData.setActualArrivalDate(finalDate); } // // 预飞 // cell = row.getCell(5); // finalDate = calDate(flightDate, cell); // if (finalDate != null) { // flightData.setEstimateUplDate(finalDate); // } //统计原因 cell = row.getCell(17); flightData.setStatiscalReason(cell.getStringCellValue()); // 备注 cell = row.getCell(22); flightData.setRemarks(cell.getStringCellValue()); } /** * 计算(计飞、预飞、实飞)日期,根据单元格中时间的后缀,计算(计飞、预飞、实飞)的日期是航班日期的前一天、后一天、当天。 * @param flightDate 航班日期 * @param cell (计飞、预飞、实飞)时间单元格 * @return */ private static Date calDate(String flightDate, HSSFCell cell) { String cellValue = cell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue)) { String time = cellValue.substring(0, 4); Date date = null; if (cellValue.lastIndexOf("-") > 0) { date = DateUtil.getBeforeDay(DateUtil.strToDate(flightDate + time, DateUtil.DATEFORMAT1)); } else if (cellValue.lastIndexOf("+") > 0) { date = DateUtil.getAfterDay((DateUtil.strToDate(flightDate + time, DateUtil.DATEFORMAT1))); } else { date = DateUtil.strToDate(flightDate + time, DateUtil.DATEFORMAT1); } return date; } return null; } /** * 过滤特殊字符 * * @param str * @return * @throws PatternSyntaxException */ private static String filterString(String str) throws PatternSyntaxException { // 只允许字母和数字 String regEx ="[^a-zA-Z0-9]"; // 清除掉所有特殊字符 String regEx = "[`~!@#$%^&*()+=|{}':;',\[\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); } public static void main(String[] args) { // long startTime = System.currentTimeMillis(); // 获取开始时间 // // // 接收的文件所在目录 // String fileDir = "F:/Accpet"; // File file = new File(fileDir); // File[] flightDataFiles = file.listFiles(); // System.out.println("该目录下对象个数:" + flightDataFiles.length); // // 循环目录下的文件 // for (int i = 0; i < flightDataFiles.length; i++) { // if (flightDataFiles[i].isFile()) { // File flightDataFile = flightDataFiles[i]; // System.out.println("文 件--》:" + flightDataFile); // List<FlightData> fdList = parseExcelToFlightData(flightDataFile); // for (int j = 0; j < fdList.size(); j++) { // FlightData fd = fdList.get(j); // System.out.println(j + "<----->" + fd.toString()); // } // } // } // // long endTime = System.currentTimeMillis(); // 获取结束时间 // long totalTime = endTime - startTime; // System.out.println("程序运行时间: " + totalTime + "ms"); } }
将session中的实体保存导数据库中
/* (non-Javadoc) * @see cn.com.acca.sams.sales.dao.FlightDataDao#addFdList(java.util.List) */ @Override public void addFdList(List<FlightData> addfdList) { // TODO Auto-generated method stub for(int i = 0;i<addfdList.size();i++){ System.out.println("添加"+i); super.add(addfdList.get(i)); } // super.add(addfdList); super.getEm().flush();//提交session中的对象到数据库中 super.getEm().clear();//清除session对象,由托管状态变为游离态
}