Action类
ImportDataService service = (ImportDataService)ApplicationFactory.getService("ImportDataService"); String str = "ASSETNAME,ASSETINFORMATION,ASSETTYPE,BUYINGTIME,AMOUNT,USEDDEPT,LOCATION,REMARK,ASSETSTATUS,RESPONSIBLEPERSON" ; for(int i=1;i<28;i++) { str = str+"clo"+i+","; } System.out.println(str); String[] cloName = str.split(","); service.importData(0, "E://work/医药价格/test.xls", cloName);
service类
public void importMenberData(int sheetNum, String fileName, String[] cloName) throws FileNotFoundException, IOException, ParseException { List listData = readPrice(sheetNum, fileName, cloName); ContactService service = (ContactService) ApplicationFactory .getService("ContactService"); for (int i = 0; listData != null && i < listData.size(); i++) { Map dataMap = (Map) listData.get(i); Map result = new HashMap(); MapUtil.addMap(dataMap, result, cloName); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); result.put("JOINDATE",sdf.parse(result.get("JOINDATE").toString())); result.put("UPDATEDATE",sdf.parse(result.get("UPDATEDATE").toString())); service.insertMManagement(result); } } public void importData(int sheetNum, String fileName, String[] cloName) throws FileNotFoundException, IOException { List listData = readPrice(sheetNum, fileName, cloName); String[] paras = { "ASSETNAME", "ASSETINFORMATION", "ASSETTYPE", "BUYINGTIME", "AMOUNT", "USEDDEPT", "LOCATION", "REMARK", "ASSETSTATUS", "RESPONSIBLEPERSON" }; AdminManagerService service = (AdminManagerService) ApplicationFactory .getService("AdminManagerService"); for (int i = 0; listData != null && i < listData.size(); i++) { Map dataMap = (Map) listData.get(i); Map result = new HashMap(); MapUtil.addMap(dataMap, result, paras); service.insertAssetInfo(result); } } /** 读取excel信息,以List返回 */ public List readPrice(int sheetNum, String fileName, String[] cloName) throws FileNotFoundException, IOException { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(sheetNum); // 获得列,宽值 int rowNum = sheet.getLastRowNum(); int cloNum = sheet.getRow(3).getLastCellNum(); List list = new ArrayList(); // 单元个的值 String cellValue = ""; System.out.println(rowNum + "=========" + cloNum); for (int rowIndex = 2; rowIndex <= rowNum; rowIndex++) { Map mapData = new HashMap(); HSSFRow row = sheet.getRow(rowIndex); for (int cloIndex = 1; cloIndex < cloNum; cloIndex++) { cellValue = cellValue2Str(row.getCell((short) cloIndex)); mapData.put(cloName[cloIndex - 1], cellValue); } list.add(mapData); } return list; } /** 获得HSSFSheet */ private HSSFSheet getSheet(int sheetNum, String fileName) throws FileNotFoundException, IOException { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(sheetNum); return sheet; } /** 获得单元格值 */ private HSSFCell getCellValue(HSSFRow row, int cellIndex) { return row.getCell((short) cellIndex++); } /** 将单元格值转换为字符型 */ public static String cellValue2Str(HSSFCell cell) { String cellValue = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: { cellValue = checkNumNull(cell); break; } case HSSFCell.CELL_TYPE_STRING: { cellValue = checkStrNull(cell); break; } default: { cellValue = checkDateNull(cell); // System.out.println("日期是: " + cellValue); } } return cellValue; } static String oldValue = ""; /** 检查单元格是否为空 */ public static String checkNumNull(HSSFCell cell) { String cellValue = ""; if (cell.getNumericCellValue() == 0) { cellValue = oldValue; } else { cellValue = num2str(cell.getNumericCellValue()); oldValue = cellValue; } return cellValue; } public static String checkStrNull(HSSFCell cell) { String cellValue = ""; if (cell.getStringCellValue().equals(null) || cell.getStringCellValue().equals("")) { cellValue = oldValue; } else { cellValue = cell.getStringCellValue(); oldValue = cellValue; } return cellValue; } public static String checkDateNull(HSSFCell cell) { String cellValue = ""; if (cell.getDateCellValue() == null) { cellValue = oldValue; } else { cellValue = date2str(cell.getDateCellValue()); oldValue = cellValue; } return cellValue; } /** 数字转换为字符 */ public static String num2str(double cellValue) { DecimalFormat df = new DecimalFormat("0.00"); return df.format(new Double(cellValue)); } /** 日期转换为字符 */ public static String date2str(Date cellValue) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); return simpleDateFormat.format(cellValue); }
相关资源:Java 使用poi导入excel 并使用xml做数据验证